Attention "dusty"!
This article is already a little outdated and may contain information that no longer corresponds to the current status of the topic.
Statistics on the number of downloads of your documents on a (web) page such as eBooks, images or PDFs provide interesting insights into user behaviour and allow conclusions to be drawn about optimization potential.
However, statistics of this kind are generally not easily accessible. Even those who already use Google Analytics are not offered automatic download tracking without additional adjustments.
Fortunately, download tracking can be implemented with little effort, which aggregates the required data and displays it in Google Analytics. In the following, we present three different ways in which such an implementation could look.
Each of these methods has advantages and disadvantages, which we will also briefly outline.
1. download tracking with the Google Tag Manager
Google Tag Manager, or simply GTM, is another tracking tool from Google that unfolds its full potential in conjunction with Google Analytics. GTM is able to add code snippets to a page using so-called tags, which enable the provision of further or more detailed information for Google Analytics. The great advantage of this solution is that it requires neither programming knowledge nor access to the code base (if the GTM is already installed) of the corresponding page. The disadvantage is that some special cases, e.g. download links that do not link directly to the file but first call a JavaScript function, which in turn initiates the download, may not be tracked.
In order to be able to use GTM, GTM (if not already installed) must be installed on the relevant page by adding a code snippet in addition to Google Analytics. You can find more detailed information on how to add GTM to your site at the following link.
Once you have successfully installed and configured GTM, you can create a new trigger of the type "All link clicks" for download tracking, which should look like this:
The RegEx command is
\.(pdf|png|jpg|doc)$
This trigger fires every time you click on a link whose URL contains the characters "pdf", "png", "jpg" or "doc", i.e. every time you click on the download link of a file with the extensions just mentioned. You can extend or change the list of files as required by separating the individual file extensions in brackets with a "|".
While a trigger represents the trigger condition, a tag determines what should happen after the trigger condition is met. Next, create a tag of the type "Universal Analytics Tag" with the following data:
Creating this tag ensures that an event with the category "Download" is sent to Google Analytics together with further information such as the URL on which the click was registered (label) and the file clicked on (action). In order for the connection between the GTM and Google Analytics to be established, the correct tracking ID of the corresponding Google Analytics property must of course also be entered. You can of course adapt the name of the category or which values you want to send to Google Analytics to suit your use case.
Last but not least, the previously created trigger must be added to the tag and the whole thing published on the corresponding page.
As soon as all this has been done and the first downloads have taken place, you will find the results in Google Analytics under Behavior > Events > Overview.
2. download tracking with custom Google Analytics code
If you do not want to install a GTM on your site or would like to have a little more control over the triggering conditions and transmitted values, you can also implement the transmission of the values using your own code. Although this requires access to the code base of the page and corresponding programming knowledge, it offers more flexibility and the possibility of intercepting special cases that the above tracking with the GTM would not recognize.
The JavaScript code that you use to send information about the download to Google Analytics must be integrated at the point where a download was successfully registered. So in addition to the code snippet, you also need a programmatic way to recognize when a download is performed. What exactly this might look like varies greatly from case to case, which is why we cannot provide a code example. However, the snippet that sends the relevant data to Google Analytics should look something like this:
This assumes that Google Analytics has already been installed on the page, as otherwise no connection to the corresponding property can be established. Of course, the values of the category, action and label can also be replaced by others here.
3. download tracking with the Drupal Google Analytics module
For Drupal users, the Google Analytics module(https://www.drupal.org/project/google_analytics) is probably the quickest and easiest option for download tracking. The module available for Drupal 7 and 8 offers this functionality by default and can be activated by simply selecting it in the module settings:
Since the module is quick to install and has a number of other functionalities (such as tracking clicks on mailto links), we would recommend using this module to all Drupal users who want to use Google Analytics and not GTM.