Against actual existing neoliberalism! (Policing Crowds)

Home » Jens Sambale: TYPO3, tt_news, Google Analytics & the missing downloads

Web, TYPO3, Typoscript, Google Analytics

TYPO3, tt_news, Google Analytics & the missing downloads

Google Analytics counts page views, not downloads. This article provides a solution to count downloads from tt_news (file_links) in TYPO3 nevertheless. Pure Typoscript approach!

Image shows event tracking in Google Analytics
Tracking downloads from TYPO3 with Google Analytics

This one eluded me for quite some time. There are some proposed javascript solutions around the web (like this one in german) and some extensions claiming to do the trick but they never worked for me.

Of course there are always the Apache logs, but it is 2012 and I do not feel like digging and grepping through them. They are nice to have for emergencies but tend to fail to impress.

When I became more and more used to Google Analytics shiny new interface late in 2011 I began digging for a simple solution: No extension and no external javascript.

The task at hand is really no rocket science or brain surgery: Add an onclick event to the download link and find a way to include the file name into it. The quite season after Christmas came in handy to do some thinking.

First: Use event tracking to count downloads

Google Analytics offers event tracking for this purpose, so this is what I wanted to achieve:

onclick ="_gaq.push(['_trackEvent', 'article', 'download','|', 100]);"

Lets walk through it:

onclick - well that is obvious, is it not? If we wanted to track interaction with, say, a form, we probably needed an onsubmit at some point, but for a download it is onclick.

Here is the syntax for event tracking (Source: Google Analytics Event Tracker Guide): 

_trackEvent(category, action, opt_label, opt_value, opt_noninteractive

  1. category (required)
    The name you supply for the group of objects you want to track
  2. action (required)
    A string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object.
  3. label (optional)
    An optional string to provide additional dimensions to the event data.
  4. value (optional)
    An integer that you can use to provide numerical data about the user event.
  5. non-interactive (optional)
    we skip that one for the time being

In my example:

onclick ="_gaq.push(['_trackEvent', 'article', 'download','|', 100]);"

article is the category since all downloads are inside articles.

download is obviously the event we wish to track.

'|' will be substituted with the file name and serves as the label

100 is an arbitrary value in my case. I may use it to distinguish between more or less valuable downloads (not all downloads are created equal). As mentioned, the value is an integer, it comes without the inverted comma and dont try 100,2 or equivalent.

Why not using virtual page view?

Because Google Analytics recommends otherwise:Virtual page views will add to your total page counts, thus distorting results.

Second: Get the data in TYPO3 

To get hold of the data, in this case the file name, we use a Content Object (cObject) (see TSRef):

lib.pdf = TEXT
lib.pdf {
           field = news_files
            wrap = onclick ="_gaq.push(['_trackEvent', 'article', 'download','|', 250]);"

Voila, news_files returns the file name!

Now we need to add this to the actual downloadlink and according to the Typoscript Reference ATagParams is our friend

plugin.tt_news {
newsFiles {
                 ATagParams < lib.pdf

The resulting link looks like this:

<a href="path/?jumpurl=uploads/lotsOfStuff/***.pdf" onclick="_gaq.push(['_trackEvent', 'article', 'download','filename.pdf', 100]);">

 That was not too hard and returns nice reports in Google Analytics about user interaction (new UI:Content | Events | Overview - drill down from there). It can also be expanded to capture other links than those from tt_news (like RTE) or measure when and where users abandon a form, say for registration or ordering.

What I already did is the segmentation of different user groups on an access restricted site using TYPO3 conditions. It is even possible to track single users by that means but I reserved that treatment for one obnoxious corporate asshole only.