最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin11浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论