On my HTML page I have some Javascript code that drives a drop down menu. The user makes a selection of which file to download, then presses a button to download it.
This is a portion of the Javascript code:
var dd = document.getElementById("OSselectDropdown");
var OSchoice = dd.options[dd.selectedIndex].value;
if (OSchoice == "win")
{
window.location.href = "http://mysite.com/downloads/installer.exe";
}
if (OSchoice == "mac")
{
window.location.href = "http://mysite.com/downloads/installer.pkg";
}
I want to be able to track how many times files were downloaded. I found this code, which uses jQuery, which is supposed to enable download counts in Google Analytics.
However, the code seems to act only on <a>
tags. I did a bit of testing, and it doesn't seem to be working in my case, I think because I'm using Javascript and window.location.href
to connect to the downloadable file.
Is there a way I can leverage this Javascript code to get Google Analytics to track the number of downloads I'm getting with my dropdown?
Or is there another or better way to be tracking the downloads from my Javascript dropdown?
Update:
Based on an answer provided, and also from looking at Google's documentation, I have changed my code to this:
var dd = document.getElementById("OSselectDropdown");
var OSchoice = dd.options[dd.selectedIndex].value;
if (OSchoice == "win")
{
_gaq.push(['_trackEvent','Installer','Download', 'Windows']);
window.location.href = "https://" + top.location.host + "/+download/Windows_Installer.exe";
}
if (OSchoice == "mac")
{
_gaq.push(['_trackEvent','Installer','Download','Mac']);
window.location.href = "https://" + top.location.host + "/+download/Mac_Installer.pkg";
}
if (OSchoice == "linux")
{
_gaq.push(['_trackEvent','Installer','Download','Linux']);
window.location.href = "https://" + top.location.host + "/+download/Linux_Installer.tar.gz";
}
However, I'm not seeing any change in my Google Analytics interface. Is the newly adjusted code correct, and if so, where should I be seeing downloads tracked in Google Analytics?