How can I get Google Analytics to track a Javascript window.location.href redirect? - Stack Overflow

On my HTML page I have some Javascript code that drives a drop down menu. The user makes a selection of

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 = ".exe";
        }
        if (OSchoice == "mac")
        {
           window.location.href = ".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?

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./downloads/installer.exe";
        }
        if (OSchoice == "mac")
        {
           window.location.href = "http://mysite./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?

Share Improve this question edited May 23, 2013 at 3:20 Questioner asked May 9, 2013 at 16:00 QuestionerQuestioner 7,47317 gold badges68 silver badges98 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

All you need to do is to call

_gaq.push(['_trackEvent','Install','exe','http://mysite./downloads/installer.exe']);
or
_gaq.push(['_trackEvent','Install','pkg','http://mysite./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]);
}

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745418593a4626865.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信