Reputation: 14727
I read this piece online:
Tracking Site Activity https://developers.google.com/analytics/devguides/collection/gajs/asyncTracking
According to this article, the tracking code should look like:
<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>
I went to my Google Analytics account, and the tracking code suggested there is:
<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-XXXXX-X', 'auto');
ga('send', 'pageview');
</script>
Which one should I use in order to be able to track the number of clicks on a link? Please note that I still want to be able to track visits to each page of the website.
Thanks!
Upvotes: 2
Views: 216
Reputation: 8907
Both snippets of code can enable link click tracking. The first snippet is the older classic GA code that will eventually be phased out and replaced by the second snippet, commonly known as Universal Analytics. Note that the analog to the link you posted is this: https://developers.google.com/analytics/devguides/collection/analyticsjs/
The basic snippet as it is will automatically track page views. To track link clicks, you will either need to manually add the code in, or use Google Tag Manager. I would recommend using GTM because you can tag as many links as you like, without needing to add code directly on to your website. GTM also allows for scalability of your website - as you add more links, forms, buttons, etc. to your site, GTM can easily accomodate tracking of them all. That said, if you only have, for example, 2 links on your site, and you hardly ever update the links or add more, then just hand code it. You can add link tracking to a link like so, for example:
<a href="http://www.anothersite.com/page.html" onclick="ga('send','event','your_category','your_action', 'your_label');">Click this link</a>
You can customise the category, action, and label to suit your reporting needs.
Upvotes: 1