试图跟踪用户电子邮件中的点击次数

时间:2021-11-16 15:22:58

My issue is as follow:

我的问题如下:

I'm trying to count the number of clicks on ads in our newsletter. The thing is I can't include js in emails - it's understandable. So I found a way around this by writing this small piece of code:

我正在尝试计算新闻稿中广告的点击次数。问题是我不能在电子邮件中包含js - 这是可以理解的。所以我通过编写这一小段代码找到了解决方法:

<script type="text/javascript" src="http://www.factmag.com/wp-content/themes/modularity_/js/jquery-1.3.2.min.js"></script>

<script type="text/javascript">

    function getUrlVars()
    {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');

            if($.inArray(hash[0], vars)>-1)
            {
                vars[hash[0]]+=","+hash[1];
            }
            else
            {
                vars.push(hash[0]);
                vars[hash[0]] = hash[1];
            }
        }

        return vars;
    }

                function redirect()
                {
                    var link = getUrlVars()["page_url"];
                    setTimeout('document.location = "' + link + '"', 100)
                }
</script>
<body onload="javascript:redirect();"></body>


<script type="text/javascript">

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-4340871-1']);
 _gaq.push(['_trackPageview']);

 (function() {
 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();

</script>

It's hosted on our servers and in the newsletter the ad code has the following format:

它托管在我们的服务器和新闻通讯中,广告代码具有以下格式:

<a href="http://www.example.com/example_ad_counter/?utm_source=banner6&utm_medium=banner&utm_campaign=banner&page_url=ad_url"><img src="ad_img_url" style="border:1px solid #000;"></a>

So what I want to do here:

那么我想在这里做什么:

  1. User clicks on ad in the email.
  2. 用户点击了电子邮件中的广告。

  3. He goes to the page with this script.
  4. 他带着这个脚本进入页面。

  5. Google Analytics counts the number it needs in order to track it.
  6. Google Analytics会计算跟踪它所需的数量。

  7. Script redirects user to the advertiser's page.
  8. 脚本将用户重定向到广告商的页面。

Now here's the deal - google analytics is not doing the count here. My guess is that I need to add something in google js in order to do so but have no clue what. Could someone help me with this one? Thanks.

现在这是交易 - 谷歌分析在这里没有计算。我的猜测是我需要在google js中添加一些内容才能这样做,但不知道是什么。有人可以帮我这个吗?谢谢。

1 个解决方案

#1


1  

You want to make sure your redirect is called after google's code is called. Currently, you have it running onLoad. yC is correct in his comment to the question that it's likely a race condition, but pushing your setTimeout won't completely resolve that race condition.

您希望确保在调用Google代码后调用重定向。目前,您已在onLoad上运行它。 yC对于它可能是竞争条件的问题的评论是正确的,但是推动你的setTimeout将无法完全解决竞争条件。

You could try calling your redirect function after google inserts itself into the page.

谷歌将自己插入页面后,您可以尝试调用重定向功能。

The code here:

这里的代码:

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

...is literally google adding a new script tag to your page, and then loading the analytics url into it. Brendon was right to suggest turning off async, but then I'd suggest adding your redirect call after the var s = document line (inside the function), and hoping that will give google enough time to do its dirty work and before you redirect.

...实际上是google为您的网页添加新的脚本标记,然后将分析网址加载到其中。 Brendon建议关闭异步是正确的,但后来我建议在var s =文档行之后(在函数内部)添加重定向调用,并希望这将给google足够的时间来完成其脏工作并在重定向之前。

If that doesn't work, then stick with yC's suggestion and push the timeout out further. Just know that the redirect delay is racing with the time it takes for google's script to load and then run, which isn't predictable. I would probably put that setTimeout in the same place I recommended calling the redirect function directly. This means that it wont even start counting the delay until google's analytics script has been added to the page.

如果这不起作用,那么坚持使用yC的建议并进一步推迟超时。只要知道重定向延迟与谷歌的脚本加载然后运行所花费的时间竞争,这是不可预测的。我可能会把setTimeout放在我建议直接调用重定向函数的地方。这意味着它甚至不会开始计算延迟,直到谷歌的分析脚本添加到页面。

#1


1  

You want to make sure your redirect is called after google's code is called. Currently, you have it running onLoad. yC is correct in his comment to the question that it's likely a race condition, but pushing your setTimeout won't completely resolve that race condition.

您希望确保在调用Google代码后调用重定向。目前,您已在onLoad上运行它。 yC对于它可能是竞争条件的问题的评论是正确的,但是推动你的setTimeout将无法完全解决竞争条件。

You could try calling your redirect function after google inserts itself into the page.

谷歌将自己插入页面后,您可以尝试调用重定向功能。

The code here:

这里的代码:

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

...is literally google adding a new script tag to your page, and then loading the analytics url into it. Brendon was right to suggest turning off async, but then I'd suggest adding your redirect call after the var s = document line (inside the function), and hoping that will give google enough time to do its dirty work and before you redirect.

...实际上是google为您的网页添加新的脚本标记,然后将分析网址加载到其中。 Brendon建议关闭异步是正确的,但后来我建议在var s =文档行之后(在函数内部)添加重定向调用,并希望这将给google足够的时间来完成其脏工作并在重定向之前。

If that doesn't work, then stick with yC's suggestion and push the timeout out further. Just know that the redirect delay is racing with the time it takes for google's script to load and then run, which isn't predictable. I would probably put that setTimeout in the same place I recommended calling the redirect function directly. This means that it wont even start counting the delay until google's analytics script has been added to the page.

如果这不起作用,那么坚持使用yC的建议并进一步推迟超时。只要知道重定向延迟与谷歌的脚本加载然后运行所花费的时间竞争,这是不可预测的。我可能会把setTimeout放在我建议直接调用重定向函数的地方。这意味着它甚至不会开始计算延迟,直到谷歌的分析脚本添加到页面。