Eliseo D'Annunzio
Eliseo D'Annunzio

Reputation: 592

Google Analytics for File Downloads not firing, placement issues?

I'm not sure if I have the placement correct or not... and this is the part I need help with...

I have a web application which will dynamically generate a list of the available resources, and the resolution of the file information variables works perfectly... I'm getting anchor tags fitted out with code such as this:

<ul id="ul_4" class="resources_list fa-ul h_4">
    <li>
        <a onclick="var that=this;_gaq.push(['_trackEvent','Resources: Tools','Download','Policy template: Access to confidential information']);setTimeout(function(){location.href=that.href;},200);return false;" href="/file.cfm?f=402&type=resource">Policy template: Access to confidential information</a>
    </li>
    <li>
        <a onclick="var that=this;_gaq.push(['_trackEvent','Resources: Tools','Download','Policy template: Client records']);setTimeout(function(){location.href=that.href;},200);return false;" href="/file.cfm?f=407&type=resource">Policy template: Client records</a>
    </li>
    <li>
        <a onclick="var that=this;_gaq.push(['_trackEvent','Resources: Tools','Download','Policy template: Privacy']);setTimeout(function(){location.href=that.href;},200);return false;" href="/file.cfm?f=391&type=resource">Policy template: Privacy</a>
    </li>
</ul>

...followed with my standard code for Google Analytics which is the following:

<script>
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-12345678-1']);
    _gaq.push(['_trackPageview']);
    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
</script>

Unfortunately, regardless of the number of clicks I've been using on these links, the triggers don't appear to be recorded in Google Analytics... even after a 24 hour turn-around period or more... Am I using the wrong approach for populating the triggers on my links, or am I using the wrong code to reference the trigger? Should I be placing the initial Google Analytics urchin code at the top of my page?

I know for fact the standard GA code works as I'm seeing activity for the various other pages... but with respect to the file downloads available from these links is the problem... I'm quite the n00b when it comes to GA, so I'm open to any advice here... What am I missing?

Thanks in advance...

Upvotes: 1

Views: 110

Answers (1)

sushain97
sushain97

Reputation: 2802

The problem is that the onclick attribute does not necessarily execute before the page has already changed to the resource; the setTimeout can execute before the GA request goes through. You would see this by looking at the Network tab in your developer console; the GA request might never complete since the user is sent to the resource before the onclick executes.

You also seem to be using _gaq (used in the old version of Analytics) instead of ga.

The proper way to do this is summarized on Google's docs. Here it is adapted for you:

<script>
var trackFileDownload = function(url, page, description) {
   ga('send', 'event', 'fileDownload', url, page + " " + description, {'hitCallback':
     function () {
       document.location = url;
     }
   });
}
</script>

<a onclick="trackFileDownload(this.href, 'Resources: Tools', 'Policy template: Access to confidential information'); return false;" href="/file.cfm?f=402&type=resource">Policy template: Access to confidential information</a>

Upvotes: 1

Related Questions