Google Analytics and tracking unique downloads

I have a resource page with .pdfs and .ppts
What do I need to do to track if a user clicks on these links?
Is there also an extra step I need to take to track if they successfully downloaded the target completely?

I heard one solution is to make a single sub-web page for each download item and stick an analytics snippet in to the page as one way to track, but this sounds tedious and not very efficient. Is there something that can track this on the fly?

Thanks!

Posted on May 4, 2010 8:40 AM

Reply
52 replies

May 16, 2010 12:51 AM in response to Yoga_D

I tested it locally.

Added an alert(hrefs.length); in the code of the gatag.js file and notice that it did not count the "a" links.

Then it occured to me that the code loads before the page is completed.

I added the gatag.js file add the end of the page and the alert showed the number of "a".

Let's hope that will work.

The analytics code is not affected. Those stats are still correct.

May 16, 2010 6:58 AM in response to Wyodor

Thanks for the explanation on how you tested it. I would of loved to look over your shoulder when you did it. One thing that confused me though is after you explained how you did it, it seemed like you since then re-edited the HTML snippet calling analytics.js first and then gatag.js
Is that true? example:

Wyodor wrote:
Then it occured to me that the code loads before the page is completed.

I added the gatag.js file add the end of the page and the alert showed the number of "a".

Let's hope that will work.


Does it really take 24 hours for Analytics to update?

Btw, what do you mean by:
Wyodor wrote:
The analytics code is not affected. Those stats are still correct.


Does it mean you are commenting that the gatag.js stats aren't working but the analytics.js is?

Message was edited with more clarity in mind by: Yoga_D

May 16, 2010 7:04 AM in response to Yoga_D

It doesn't really take 24 hours.

Google only displays the result next calendar day.

I looked at the result 1 minute after midnight (CET).

Then did the testing and at 1:15 i published my findings.

To speed up things I changed the Time zone to Kiribati, +14 hours into the future, but Google would not cooparate.

It's now 16:05 CET, so I have to wait 8 hours to see the next result.

The analytics code is not affected. Those stats are still correct.


The position of the analytics script, either in the <head> or at the end of the <body> does not change the results.

Only the gatag.js code has to be at the end of the page, and before the google code. So I had to change the location for both in the page.

May 16, 2010 6:46 PM in response to Cyclosaurus

Cyclosaurus,

I assume you are suggesting to do this via the HTML Snippet..yes? How would one structure it in an HTML Snippet:

{code}
<script language="JavaScript" type="text/javascript">
<!--

var headID = parent.document.getElementsByTagName("head")[0];
var newScript = parent.document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = '/js/gatag.js';
headID.appendChild(newScript);


var headID = parent.document.getElementsByTagName("head")[0];
var newScript = parent.document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = '/js/analytics.js';
headID.appendChild(newScript);

// -->
</script>{/code}

Message was edited by: Yoga_D

May 17, 2010 1:16 AM in response to Cyclosaurus

My code first adds gatag.js to the end of the <body> and then analytics.js.

In the Safari inspector you see :

<gatag.js>
<analytics.je>
</body>

Exactly as instructed.

The only difference is that I use an external js file while Google assumes that you paste the code before</body>.

If this means one still has no control over what loads first, then it's always a gamble that it will work.

The analytics code works. Only the download code does not. The purpose of my solution is the avoid having to edit the pages after publishing.

May 17, 2010 7:16 AM in response to Wyodor

Wyodor wrote:
If this means one still has no control over what loads first, then it's always a gamble that it will work.

The analytics code works. Only the download code does not. The purpose of my solution is the avoid having to edit the pages after publishing.


I'm curious if following what Cyclosaurus suggests, one implemented the settimeout() function on the analytics.js that it would give gatag.js or taglinks.js the window of time to load first?

May 17, 2010 7:46 AM in response to Yoga_D

I did some other testing this morning.

I found that the gatag.js does not respond to clicks. At least not in iWeb pages. In custom pages it did respond.

I then used the other js file I linked to earlier. That one does respond to clicks. Here's the link to the page again :

http://www.iqcontent.com/blog/2006/11/tracking-document-downloads-in-google-anal ytics/

And this is the file : http://www.iqcontent.com/blog/files/taglinks.js

If you add this alert(file_path); code in this routine you can test the response :

function trackfiles(array_element) {


I added jpg and png to the list of files to track.

Now I have to wait till midnight again to see if it works.

The code I use in the HTML Snippet to load both JavaScript files has no effect. It simply works. No need for timeouts.

And again, the analytics code itself does work. With or without the download code and regardless where it is in the page, in the <head> or in the <body>.

May 17, 2010 3:13 PM in response to Yoga_D

Well, it's 10 minutes after midnight CET.

Just checked the results of yesterday, 16 may.

I do not see any downloaded files.

I do see pages I added for the purpose. With only one link to a zip file.

As noted in a previous post, the download code alerts that it catched the click and sends it to the tracker but there is nothing in the result.

I think I leave it here. Perhaps some other time I will delve deeper into it.

Goodnight from Yurp.

May 17, 2010 6:12 PM in response to Yoga_D

there are many thing going on, you need to look at the code:

1) both gatag.js and taglinks.js required pageTracker to be defined.
2) the older google code _gat has pageTracker, but uses document.write method; which is destructive when you use it in html snippet (in this case).
3) the newer google code _gaq does not define pageTracker, wyodor did not have it defined.
you have to add one line of code for it.
4) lastly, either gatag.js or taglinks.js has to be linked after ga.js, this is where pageTracker is defined (before gatag.js or taglinks.js).

----------

here is what should work: http://temp.cyclosaurus.com/AccessTracking/Test.html
I use my own widget to add GA code with pageTracker and gatag.js, I had to mod gatag.js to ensure pageTracker is valid (otherwise nothing will work) and also match file extension regardless it's upper or lower-case.

May 18, 2010 12:44 AM in response to Cyclosaurus

4) lastly, either gatag.js or taglinks.js has to be linked after ga.js, this is where pageTracker is defined (before gatag.js or taglinks.js).


Then the instruction is wrong :

paste the following code directly above your standard Google Analytics tracking code (which by the way, should be as near the end of the <body> tag as possible).

<script src=”/js/taglinks.js” type=”text/javascript”></script>


http://www.iqcontent.com/blog/2006/11/tracking-document-downloads-in-google-anal ytics/

And it is clearly stated in the taglinks.js file :

// Javascript to tag file downloads and external links in Google Analytics
// To use, place reference to this file should be placed at the bottom of all pages,
// just above the Google Analytics tracking code.


Anyway, I changed the order to see what will happen.

I also have an idea how to solve it my way.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Google Analytics and tracking unique downloads

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.