On pageload does I load google analytics through he following code snippet:
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
pageTracker._trackPageview();
} catch(err) {}
</script>
And I use the following code to fire an event (after page load):
_gaq.push(['_trackEvent', 'test category', 'test action']);
Standard information get tracked, but NO CUSTOM EVENTS. I know all events get sent to Google Analytics because each time an event if fired, does an HTTP GET request get sent to the following url:
http://www.google-analytics.com/__utm.gif?.....more data here
What does I need to do to get custom events in google analytics to work?
Thanks in advance.
It seems you are running an old, "traditional", implementation of your Google Analytics.
I would strongly suggest that you upgrade to atleast the asynchronous snippet, where the syntax for _gaq.push is supported. As it stands now, you are trying to push an event using the new syntax into an old implementation which uses another syntax.
The differences between the two are detailed here: https://developers.google.com/analytics/devguides/collection/gajs/asyncMigrationExamples
In short, you are using a syntax for gat/pageTracker, where the newer asynchronous one uses gaq.
Please review this code that I have copied from above link:
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
...
<a onclick="_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', opt_value]);">click me</a>
To what you are using now, the older syntax:
var pageTracker = _gat._getTracker('UA-XXXXX-X');
pageTracker._trackPageview();
...
<a onclick="pageTracker._trackEvent('category', 'action', 'opt_label', opt_value);">click me</a>
As you can see, you are trying to send an event to the old ga.js using new ga.js syntax. I would highly recommend using the newer ga.js which loads asynchronously. By asynchronously, it means that the javascript loads while the webpage is loading, which the old ga.js does not.
This is an example implementation of the newer ga.js, and it should be implemented right before closing </head>:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-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>
If you implement the above suggestion, then you are using the newer ga.js and the _gaq.push-syntax should work. Try it out!
And if you do not know what the asynchronous part means, a brief explanation:
The latest version of the Analytics tracking code offers an improved way to track website visitors with Google Analytics. It takes advantage of browser support for asynchronous JavaScript to enhance the speed in which the tracking code is loaded. With the latest version of the tracking code, you can also place the Analytics snippet higher in the page without delaying subsequent content from rendering.
It continues to load other elements of your webpage aside from eachother, instead of waiting for 1 to finish before loading 2, then 3, etc.
Related
What is the differnce between the below two code in tracking the webpage.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_setDomainName', 'right.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);
})();
</script>
And
<script language="javascript" src="http://www.google-analytics.com/ga.js" type="text/javascript"></script>
<script type="text/javascript">var pageTracker = _gat._getTracker("UA-XXXXXXXX-X"); pageTracker._initData(); pageTracker._trackPageview();</script>
1) The first was was deprecated a few month ago, the second one has been deprecated many years ago (you should use neither of them)
2) The second version is very old, synchronously executed code. The downside was that loading the GA code synchronously meant that page load might under some circumstances be blocked until the ga.js file finished loading (which in turn meant that users sometimes saw an empty screen until the file had finished loading). Older browsers where not got with loading multiple files simultaneously, especially if the files content might affect the way the page was rendered.
3) The first example (asychronous code) stores all instructions for the analytics code in an javascript array. Since this uses native javascript code (the push method) this will work before the ga file has been loaded. Once the code is in place (it is injected into the pageheader via the short bootstrap script below the _gaq.push calls) it can read the array and process the instructions contained therein.
Again I'd like to point out that this information is of purely historical value since both versions of the tracking code are deprecated. You should use the new Universal Analytics code only.
Me and a friend we are trying to get Event Tracking working since days, but we just cant make it work. It would mean the world to me if you could take a look and try to help. Thanks.
You can take a look in the code yourself, the URL is http://www.meinAuslandssemester.com .
What we are trying to do is to track the outgoing Amazon links as an event (Blue buttons in sidebar, footer and one below on the landing page).
The code in the header goes like this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-37237070-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/u/ga_debug.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript">
function recordOutboundLink(link, category, action) {
try {
var myTracker=_gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', category , action ]);
setTimeout('document.location = "' + link.href + '"', 100)
}catch(err){}
}
</script>
The first part is just the regular tracking code, while the second part is suggested by Google to track outbound links.
The links on the landing page go:
<”a class="mainbutton" style="padding: 6px 60px 6px 60px;" onclick="recordOutboundLink(this, 'RekordOutbndLnks', 'ausland.com');return false;" href="http://www.amazon.de/gp/reader/1481174193/?tag=simmenfl-21">Das erste Kapitel gratis lesen"
And since all of this didnt work, we also tried a different version, currently live in the sidebar and footer: <"a class="sidebarbutton" href="http://www.amazon.de/dp/B00ARLDGD0/?tag=simmenfl-21" onclick="_gaq.push(['_trackEvent', 'ExternalLinks', 'Amazon','SidebarKindle']);return false;">Kindle Edition: 9,99€
Unfortunately neither version 1 or 2 are working and we just cant find the problem. Any ideas? Forget the " before the a in the final links, I just added these so the code can be displayed. Couldnt do any better sorry! :)
Thank you so much for trying to help, we eventually gave up :(
Relevant links:
http://support.google.com/analytics/bin/answer.py?hl=en&answer=1136920
Version 2 won't work because the GA async code won't be executed before the link is followed, which is the reason for the setTimeout delay in the google example.
As for version 1, unfortunately, the google example needs a bit of tweaking before it can be used - see Why is Google Analytics not tracking any events? - i.e. try replacing
var myTracker=_gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', category , action ]);
with
_gaq.push(['_trackEvent', category , action ]);
I have set up GA event tracking on a test site I have. I use the asynch call in the page and then send events on click. the code I use is here:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXX-1']); // This is where my real Analytics Account number would be
_gaq.push(['_trackPageview']);
_gaq.push(['_trackEvent', 'AdvertImpression', 'MEA','logged In']);
_gaq.push(['_trackEvent', 'AdvertImpression', 'AN Other Advert','not logged in']);
(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);
})();
I also have an onclick event.
<IMG src="http://img.xxxyyy.com/banners/OPFME-20121211.jpg" width=468 height=60 border=0 alt="MEA" onclick="_gaq.push(['_trackEvent', 'AdvertClicked', 'MEA','Logged In'])")>
I see the site visits appear and my account says that events are being generated. When I select "Unique Events" I even see quantities but I cannot see any of the specific category information in the drill down.
I have remove all filters on the account and have waited for 48 hrs. Is there something I'm missing.
Google Analytics records data by making tracking image requests. If you're loading a new page in the same window before the tracking request has completed, no data will be recorded. I've had good luck with a slight delay before following the link.
Try the following code:
<script type="text/javascript">
function trackLink(link, category, action, value) {
_gaq.push(['_trackEvent', category , action, value ]);
if (link.target == "_blank") return true;
setTimeout(function(){ location.href = link.href; }, 150)
return false;
}
</script>
Add it the link as follows -- the return in the onlick is required.
<a href="http://www.xxxyyy.com/ad/click/taHR" onclick="return trackLink('AdvertClicked', 'MEA','Logged In')">...
Add target="_blank" to your <a> tag.
If the links you're adding tracking to could be added in a new window (e.g. if they are adverts or external links), then you can fix this by simply adding target="_blank" to your <a> tag to make the external link open in a new window.
This way you don't need to do any kind of delay (the original page keeps running and the javascript gets plenty time to complete) and you will always track your click - depending on network-speed/google load/various other things, 150 milliseconds may not be long enough to track the click resulting in fewer clicks.
Mostly, these types of clicks should be to external links anyway, otherwise you'd already have tracking in Google Analytics for these clicks since a new page would be loaded with your Analytics code on it.
<img src="http://img.xxxyyy.com/banners/OPFME-20121211.jpg" width=468 height=60 border=0 alt="MEA" onclick="_gaq.push(['_trackEvent', 'AdvertClicked', 'MEA','Logged In'])")>
If this still doesn't help, open your browser's javascript console - in Chrome hit CTRL+SHIFT+I then click the console tab - and check for any errors.
A problem I've seen elsewhere is _gaq is undefined which can happen if the Google Analytics script is not included properly on the page.
I made a small little Tic Tac Toe game that plays perfectly at http://tic.cwoebker.com.
The actual tic tac toe game is loaded from (/tic) and embedded in an iframe.
Therefore others could easily embed the game on their own site if they wanted.
I am doing some event tracking inside of the iframe.
Right now I have it setup so it fires a page view on both the main page
and the actual game code inside the iframe.
I was wondering whether I could somehow only fire the page view for the iframe if its not
embedded on http://tic.cwoebker.com but on another site.
So that everything thats tracked under root (/) is traffic on my site and everything tracked in the i frame (/tic) traffic generated by embedding on another site.
Right now my analytics code in the iframe looks like this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxxx-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>
Thanks a lot in advance.
As you're in an iFrame on a separate domain, you can't access the parent window to get the location. Check out this demo and/or google it a bit: http://jsfiddle.net/TomFuertes/RRB52/2/
_gaq.push(['tictactoe._setAccount', 'UA-xxxxxxxxx-x']);
// Line below causes security issues, thus won't work
_gaq.push(['_setCustomVar', 1, 'Domain', window.parent.location.host, 3]);
_gaq.push(['tictactoe._trackPageview']);
You can pass the domain using a querystring on your iFrame page, you'd need to modify the include code to look like this: https://stackoverflow.com/a/5697801/94668
<script type="text/javascript">
document.write('<iframe src="//tic.cwoebker.com/?url=' + window.location + '"></iframe>');
</script>
Then you'd filter out your Google Analytics appropriately.
You can use Google Analytics campaigns to track the traffic. You will need to distribute the embed code that already has the campaign parameters as part of the embed iframes src.
Hope that helps.
Interested to hear from others.
I am using Google analytic on my web site.I have the following script in the head section of my page -
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-17301453-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>
i am using the following method to track the number of clicks -
Grab This Deal
But the google anayltics website does not seem to be reporting any events/clicks. Please help.
When debugging you may like to either use the Firefox Live HTTP Headers plug-in or Wireshark. These will tell you about requests to Google Analytics.
Depending on the browser and the way the site loads it may be worth using the onload handler of the page to call the Google Anayltics code (with async set to false in the GA snippet) and dynamically assign the onclick event (an id on the tag will help find it). This will ensure the code can run at the appropriate time.
The link may be processed before the tracking event is sent to Google Analytics as you do not return false to the onclick event. AFAIK there is no callback on GA events to let you know when to proceed with the next page.
In short this looks like a race condition between an async GA request and the browser finding the linked site.
The only way to have this functionality would be to use an iframe or new window for the link's target.
does it work if you do this?
Grab This Deal
If so, you may need to put something in to wait for the google tracking code to complete before allowing the browser to navigate away.
have you tried adding return true; to the end..
Grab This Deal
Dunno if it will help, just a suggestion.