Very simple scenario: I simply wish to track, using google analytics, every instance that a visitor of my site clicks on a link to an external website. I've done this using event tracking to the best of my understanding, but when I then click on the link, I don't see any tracking in the google analytics report.
I'm wondering if:
(a) there is something wrong with my code;
(b) I'm not looking in the right place in my google analytics account;
(c) both
Below is my entire page in its entirety, subsitituting 111111111 for the actual account id:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
<script>
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-1111111111-1', 'mysite.com');
ga('send', 'pageview');
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1111111111-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> <a target="_blank" onClick="_gaq.push(['_trackEvent', 'Links', 'Click', 'CNN']);"
href="http://www.cnn.com">CNN</a>
<br/>
</body>
</html>
There are few things wrong with your code:
Analytics version
You're mixing up the old ga.js and the newer analytics.js
Just use only the newer analytics.js
Race condition
You're tracking the event onClick, but at this point, the browser's already leaving the page and the request may not go through.
Basically, opening cnn.com and submitting the event are competing in the browser window.
Use a hit callback, and set window.location.href, and you'll be fine:
ga('send', 'event', {
'eventCategory': 'Links',
'eventAction': 'Click',
'hitCallback': function() {
window.location.href = "http://www.cnn.com";
}
});
This way, the browser will wait for the event to be submitted, and then access cnn.com.
Related
I've got the following code in the head which isn't tracking outbound links:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxxx-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>
<script>
/**
* Function that tracks a click on an outbound link in Google Analytics.
* This function takes a valid URL string as an argument, and uses that URL string
* as the event label.
*/
var trackOutboundLink = function(url) {
ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
function () {
document.location = url;
}
});
}
</script>
And have added the event tracking on outbound links as stated on the analytics site:
onclick="trackOutboundLink('/WEBSITE/www.something.com')
What am I doing wrong? No outbound links are being tracked.
Update - changed the tracking code to universal; which event outbound tracking do I use?
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxx-Y', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
uname12, the issue is that you are mixing "old" Google Analytics tracking code and "new" Universal Analytics tracking code syntax.
UPDATE to my original answer to reflect changes to original question:
I would try using the correct address, so instead of:
onclick="trackOutboundLink('/WEBSITE/www.something.com')
try this:
onclick="trackOutboundLink('http://www.something.com')
ORIGINAL answer:
That's the reason why the tracking doesn't work. Here is the correct documentation for event tracking (always look GA.js).
If you change your code to something like this:
var trackOutboundLink = function(url) {
_gaq.push(['_trackEvent', 'Outbound', 'Click', this.href]);
setTimeout('document.location = "' + this.href + '"', 100);
return false;
}
That should do the trick.
Hope this helps :-)
This is my GA Code:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_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>
This code reports to GA Event tracking:
<li>Get Started</li>
But this code doesn't:
<li>Get Started</li>
Any idea what I'm doing wrong?
It's not that the GA tracking isn't firing, but that the data isn't being recorded.
_trackEvent records data by requesting a tracking pixel from analytics servers. Because the link visits a new page right after the _trackEvent call, the tracking pixel request can get canceled, resulting in no data being recorded.
The most common pattern is to add a slight delay (150ms works) before following the link to the new page. Do a search for "_trackEvent delay"
I fixed the issue.
I created this function with a small delay:
<script type="text/javascript">
function trackOutboundLink(link, category, action) {
try {
_gaq.push(['_trackEvent', category , action]);
} catch(err){}
setTimeout(function() {
document.location.href = link.href;
}, 100);
}
</script>
And I call the links like this:
<a href="http://www.example.com" onClick="trackOutboundLink(this, 'Outbound Links', 'example.com'); return false;">
Works like a charm
here your answer from ga team!
I've modified this to get a more easy way:
trackBeforeHref : function(element, category, event, label) {
ga('send', 'event', category, event, label, {'hitCallback':
function () {
document.location = element.href;
}
});
},
then you can use in this way:
<a onclick="trackBeforeHref(this, 'a category', 'a event', 'a label'); return false;"> </a>
Taking advantage of the facts that
_gaq executes the tasks pushed sequentially.
functions can be pushed into _gaq : link to google developers article
so the solution to the problem is
$("a").on("click", function(){
// stop opening the link using return false at end of this event callback function
var href = $(this).attr("href");
// pushing the track event task first and the call to open the href link so that both these tasks happen sequencially.
_gaq.push(['_trackEvent', 'click', 'some_link' ], function(){
window.location = href;
});
return false;
});
on our site we would like to track with Google Analytics user actions in iframe content. The iframes are hosted on a different and 3rd level domain. For some reason my setup doesn't work.
Situation:
In the iframe I want to use two google trackers: one from the parent site, and a separate just for the iframe. I also want to use Events and VPVs. When browsed the page can be seen in GA account's "real-time overview" section. But I don't see a VPV which is attached to Submit button click.
I also don't really understand how function "_setDomainName" works and what should be there. These are separate issues and any help would be much appreciated.
The setup:
On parent site:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-1']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setDomainName', 'parent.com']);
_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);
})();
$(document).ready(function() {
var pageTracker = _gat._getTrackerByName();
var iframe = document.getElementById('myIFrame');
iframe.src = pageTracker._getLinkerUrl('http://mydomain.iframedomain.net/page.aspx');
});
</script>
ON Iframe page:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['a._setAccount', 'UA-XXXXXX-1']);
_gaq.push(['a._setDomainName', 'parent.com']);
_gaq.push(['a._setAllowLinker', true]);
_gaq.push(['a._trackPageview']);
/* this is second tracking account */
_gaq.push(['b._setAccount', 'UA-XXXXXX-2']);
_gaq.push(['b._setDomainName', 'iframedomain.net']);
_gaq.push(['b._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>
Then on a button click event I call this function:
function track() {
_gaq.push(['_trackPageview', '/search-retailer-clicked']);
_gaq.push(['_trackEvent', 'Search - Event', 'Click - Event', 'Search Retailer Button clicked']);
}
what am I doing wrong?
You've named both trackers in the iframe (a._setAccount & b._setAccount), but in the button click event, you're using a default, unnamed tracker.
The outcome is that the virtual page view and event are getting sent to a tracker that hasn't had the account set. If you look at the tracking pixel requests (via ga_debug.js, firebug, chrome developer tools, or fiddler), they'll show a UA-XXXXX-X accountId.
You probably want something more like:
function track() {
_gaq.push(['a._trackPageview', '/search-retailer-clicked']);
_gaq.push(['a._trackEvent', 'Search - Event', 'Click - Event', 'Search Retailer Button clicked']);
_gaq.push(['b._trackPageview', '/search-retailer-clicked']);
_gaq.push(['b._trackEvent', 'Search - Event', 'Click - Event', 'Search Retailer Button clicked']);
}
Here is my problem:
We have page contained in iframe (url of iframe page - www.iframepage.com). That page has link UPGRADE on it. When user clicks on that link, he gets directed to billing page, which is located on different domain (www.billingsite.com/cc.html).
That page should be open on top (not in iframe).
If I use _link, GA cookie values will get passed to target page and cross domain tracking will work, BUT target page will open in iframe.
UPGRADE
https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiDomainDirectory#_gat.GA_Tracker_._link
I would need a solution that will provide both: target page should open on top (not in iframe) and GA _utm parameters should be passed to target url so cross domain tracking could work.
Any help will be appreciated, thanks.
The _gaq.push(['_link', url]); function targets the current window with the 'url' you send it. It ignores target. What you need to do is call the Google function but then update the parent location.
Your page within the iFrame should look something link this
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx-xx']);
_gaq.push(['_setDomainName', 'example.com']);
_gaq.push(['_setAllowLinker', true]);
_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>
</head>
<body>
Link
<script>
function your_GA_track_link(url){
_gaq.push(['_link', url]);
self.parent.location.href = url;
return false;
}
</script>
</body>
</html>
I think I have found solution. Here is code that should be put in body:
<script type="text/javascript">
function openWindow(link) {
_gaq.push(function() {
var tracker = _gaq._getAsyncTracker(); //add name param if needed
window.open(tracker._getLinkerUrl(link.href),"_parent");
});
return false;
}
</script>
UPGRADE</br>
I have been trying to setup a custom variable for the past few days and it hasn't been working.
My Google tracking code is part of a master page (asp.net concept) so I can't set the custom variable inside the second script block labeled "Async Google Code" because it is shared by many other sections.
Below is my code and the order it appears in my page. Is there any way I can set it outside the "Async Google Code" script?
<head>
<!-- Setting Custom Var -->
<script type="text/javascript">
$(function () {
_gaq.push(['_setCustomVar', 1, 'Account', 'UserType', 2]);
}
</script>
<!-- Async Google Code -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_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>
</head>
wrap your custom variable stuff in a function to be called and insert a call to that function between the var _gaq... and _trackPageview lines in the bottom piece.
Your "setting custom var" code is missing an end bracket.
You need to set the custom variable before track pageview. You have the custom var in the jQuery document ready shortcut, making it so that it happens after the trackpageview.