Outbound Clicks Not Being Tracked By Google Analyt

2019-08-30 02:24发布

问题:

I am trying to track the number of outbound clicks by Google Analytic on an advertisement banner on my website.

The way this works is, if you are logged in, you see no banner. If you are not logged in, you see the banner. I have successfully done this on my wordpress site using the following code:

    <?php
if (is_user_logged_in()){
    echo "No Ads - Thank You for Supporting Us";
}
else {
echo "

<a href=\"http://www.advertiser.org/\" onClick=\"pageTracker._trackEvent('Outbound', 'CuteDog', '300sb');\" target=\"_blank\" rel=\"nofollow\"><img src=\"http://www.mydomain.com/box/cp/cuteDog.gif\" width=300 height=250 alt=\"Cute Dog\" style=\"border:none;\" /></a>

";
};
?>

Now, the banner and everything works, but it is not being tracked by GA. I am quite sure I have the onclick function correct:

onClick="pageTracker._trackEvent('Outbound', 'CuteDog', '300sb');"

My GA tracking code is located in the header of my page, and it is the most latest code:

   <script type="text/javascript">var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-8441420-2']);
  _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> 

What is it that I'm doing wrong ???

回答1:

You are using two different versions of the GA code. Here is the async version which you should be using:

_gaq.push(['_trackEvent','Outbound', 'CuteDog', '300sb']);

You can put this directly in your onclick or make a wrapper function to call it.

edit per OP comment about not understanding what to do with this

You put this bit of code in your link's onclick...

Change this:

<a href=\"http://www.advertiser.org/\" onClick=\"pageTracker._trackEvent('Outbound', 'CuteDog', '300sb');\" target=\"_blank\" rel=\"nofollow\"><img src=\"http://www.mydomain.com/box/cp/cuteDog.gif\" width=300 height=250 alt=\"Cute Dog\" style=\"border:none;\" /></a>

To this:

<a href=\"http://www.advertiser.org/\" onClick=\"_gaq.push(['_trackEvent','Outbound', 'CuteDog', '300sb']);\" target=\"_blank\" rel=\"nofollow\"><img src=\"http://www.mydomain.com/box/cp/cuteDog.gif\" width=300 height=250 alt=\"Cute Dog\" style=\"border:none;\" /></a>


回答2:

There's two problems here:

  1. Your using the pageTracker object in the onClick event but your using the asynchronous version of GA code. In this version, the object isn't directly available via the window global context so there's probably an error occurring.
  2. Even if the pageTracker object was available, the new page would load before it had a chance to send the data to GA.

You should use a function similar to the following:

    <script type="text/javascript">
        function recordOutboundLink(link, category, action, value) {
            try {
                var pageTracker=_gat._getTracker("UA-XXXXXX-XX");
                pageTracker._trackEvent(category, action, value);
                window.setTimeout("window.location = '"+link.href+"';", 100);
            }catch(err){}
        }
    </script>

Then, call it in your onClick event:

onClick="recordOutboundLink(this, 'Outbound', 'CuteDog', '300sb'); return false;"