Reputation: 33
I have a website with a download button which will let you download your self-generated image (which has a generate button too). I want to track the download clicks. Here's my download button code:
<input class="form-row form-box blue-bg" type="button" id="download" value="Download Screensaver" disabled />
I've managed to track the page views with the Google Analytics standard code but I don't know how to put event tracking and somebody told me to also use Google Tag Manager which is not really familiar to me. Here's my GA code:
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
I'm wondering if what do I need to add to the js code and the download button too, if ever. Hope someone will help me, will really appreciate this as I am new to back-end stuff and I have no superior to guide me. Thanks!
Upvotes: 0
Views: 497
Reputation: 353
You can use this code for counting your downloads
function trackDownload(link) {
_gaq.push(['_trackEvent', 'Downloads', 'Click', 'Setup executable']);
_gaq.push(function() { document.location = link.href });
return false;
}
<a href="files/setup.exe" onclick="return trackDownload(this);">Download</a>
Upvotes: 2
Reputation: 2574
You can send the following on click of button:
Can add that in our javascript. eg:
document.getElementById('download').addEventListener("click", function () {
ga('send', 'event', 'Download', 'Download Screen Saver', 'self generated image');
});
https://developers.google.com/analytics/devguides/collection/analyticsjs/events#event_fields
Please make sure above code is after the GA script you have provided in your question above.
Upvotes: 1