How can I get Google Analytics to track a Javascript window.location.href redirect? -
on html page have javascript code drives drop down menu. user makes selection of file download, presses button download it.
this portion of 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 able track how many times files downloaded. found this code, uses jquery, supposed enable download counts in google analytics.
however, code seems act on <a>
tags. did bit of testing, , doesn't seem working in case, think because i'm using javascript , window.location.href
connect downloadable file.
is there way can leverage javascript code google analytics track number of downloads i'm getting dropdown?
or there or better way tracking downloads javascript dropdown?
update:
based on answer provided, , looking @ google's documentation, have changed code 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 change in google analytics interface. newly adjusted code correct, , if so, should seeing downloads tracked in google analytics?
all need 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 code before redirect user
update:
to ensure event tracked have postpone redirect , wrap callback .push() method, check similar question
how 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]); }
Comments
Post a Comment