3

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?

Questioner
  • 7,133
  • 16
  • 61
  • 94

2 Answers2

4

All you need to do is to call

_gaq.push(['_trackEvent','Install','exe','http://mysite.com/downloads/installer.exe']);
or
_gaq.push(['_trackEvent','Install','pkg','http://mysite.com/downloads/installer.pkg']);

in your code before redirect the user

UPDATE:

To ensure that the event is actually tracked you have to postpone redirect and wrap into the callback for the .push() method, check similar question

How about this?

...
switch (OSchoice) {

    case 'win':
        _url = 'http://' + top.location.host + '/+download/Windows_Installer.exe';
        _ext = 'exe';
        break;

    case 'mac':
        _url = 'http://' + top.location.host + '/+download/Mac_Installer.pkg';
        _ext = 'pkg';
        break;

    case 'linux':
        _url = 'http://' + top.location.host + '/+download/Linux_Installer.tar.gz';
        _ext = 'tar.gz';
        break;
}

if (_url) {
    _gaq.push(['_set', 'hitCallback', function(){window.location.href = _url;}]);
    _gaq.push(['_trackEvent', 'Install', _ext, _url]);
}
Community
  • 1
  • 1
Jura Khrapunov
  • 1,024
  • 6
  • 14
  • Thank you for responding. Are you saying I only need these two lines you provided, and not the rest of the code on the page I linked to? Do I need the jQuery library for this, or does Google Analytics pick it up? – Questioner May 10 '13 at 04:11
  • You have to enter respective line before redirect, first line for your 'win' option and second one for the 'mac'. The only thing to keep in mind is to paste GA tracking code before your script. And yes, you don't need anything except the GA code in order to process this – Jura Khrapunov May 10 '13 at 06:18
  • Thank you for your help. I have adjusted my code to what I think it is you are suggesting I do, but I am not seeing any information about downloads in my Google Analytics interface. I've added my new code to my question. Did I implement your suggestion correctly? – Questioner May 21 '13 at 09:26
  • Thank you for the code. I'm not sure yet if I'm connecting to Google Analytics yet, but one odd thing is that after implementing the code you provided, it seems to cause the selected download to be activated over and over again. Not sure why that would happen. – Questioner May 23 '13 at 03:03
  • I've gone back to my `if` statement syntax for the time being. I've also slightly changed my labelling, after what I hope is a better understanding of the Google documentation. I've just tried to make my labels a little more human readable for the humans other than me who will be tracking the Google Analytics data. Changes are reflected in my question. – Questioner May 23 '13 at 03:23
2

What you are looking for is event tracking. Here is the documentation for that: eventTrackerGuide

jmiraglia
  • 671
  • 6
  • 11