Reputation: 53
I have a problem with Google Analytics
.
I want to track clicks on an <a>
tag which links to a PDF file, for which I'm using a function which will be called when user clicks on download link.
Here is my code:
$('.dl-tracking').on('click', function (){
ga('send', 'event', 'download', 'click', 'link');
});
But it doesn't work.
Upvotes: 0
Views: 2555
Reputation: 2525
Here's the code:
1st Method:
<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);
})();
$(document).ready(function(){
$('.dl-tracking').on('click', function (){
_gaq.push(['_trackEvent', 'download']);
});
});
</script>
2nd Method:
<script type="text/javascript">
(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');
$(document).ready(function(){
$('.dl-tracking').on('click', function (){
ga('send', 'event', 'download', 'click');
});
});
</script>
NOTE
Replace "UA-XXXXX-X" or "UA-XXXX-Y" with your own GA Tracking Code.
Upvotes: 2