Send file upload event from server to Google Analytics

The download link is sent to the client by email:

Hello,
Please find your product here: https://www.example.com/files/yourfile.zip

I would like to track this download in Google Analytics as a conversion Goal

.

Unfortunately, when the user clicks on the link, the file is directly delivered by the web server , without going through the .html page.

How to track such a direct file upload inside Google Analytics?

  • Should I add a dummy HTML "in the middle" page that will use the tracking snippet analytics.js

    and send a load event to GA with ga.send(...)

    , then redirect to the actual file after 500 milliseconds s setTimeout(redirect, 500)

    ? Is this a clean and safe solution? I see a lot of small potential problems: 500ms ok? which redirect should i use? Also a user with disabled JS will never get their file ... or when using <noscript>

    it is not possible to transform the target.

  • Is there a way to ask Apache (who serves the yourfile.zip

    client) or PHP to send a tracking event to GoogleAnalytics when this file is served?

  • Another solution?

It looks like solution 2. will have the advantage of being 100% reliable, whether the client is JS enabled or not.

But on the other hand, I don't want to use very few used hacks. What is the usual solution for this very common situation?

+3


source to share


1 answer


Google Analytics has a protocol for sending analytics data from arbitrary sources. See here: https://developers.google.com/analytics/devguides/collection/protocol/v1/

So, if your web server sends an analytic event to Google, it's not as difficult as it might seem. I'm not sure if you can directly connect to Apache to generate these events. However, I see at least two solutions.

1) Redirect all downloads to a server side script that sends data and can generate the required analytics event.
2) Parse server logs and generate analytical events from them.

EDIT Solution example 1:
Make sure there are no spaces before or after tags, because this will be part of the actual response sent to the client.

download.php:

<?php
    // Read ?file=xxx URL parameter
    $requestedFile = $_GET["file"];

    // Read Google Analytics cookie
    $rawCookie = $_COOKIE["_ga"];
    $splitCookie = explode('.', $rawCookie);
    $trackingId = $splitCookie[2] . '.' . $splitCookie[3];

    // Create Google Analytics request data (see here https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide)
    $data = array('v' => 1, 
                  'tid' => 'UA-XXXXX-Y', 
                  'cid' => $trackingId, 
                  't' => 'event', 
                  'ec' => 'download', 
                  'ea' => 'download', 
                  'el' => $requestedFile);

    // Create the request options
    $options = array(
        'http' => array(
            'method' => 'POST',
            'content' => http_build_query($data)
        )
    );

    $context = stream_context_create($options);

    // Send GA request
    $result = file_get_contents('https://www.google-analytics.com/collect', false, $context);

    // GA request failed
    if($result === FALSE) { /* Error */ }

    // Requested file does not exist
    if(!file_exists($requestedFile)) { /* Error */ }

    // Set response headers for binary data
    header('Content-Type: application/octet-stream');
    header('Content-Length: ' . filesize($requestedFile));

    // Open the requested file
    $fileHandle = fopen($requestedFile, 'r');

    // Write the requested file to stdout (which is what the client receives)
    print fread($fileHandle, filesize($requestedFile));
    flush();

    // Close the requested file again
    fclose($fileHandle);

    exit;
?>

      

.htaccess / mod_rewrite rules:

RewriteEngine on
RewriteUrl ^/download/(.*)$ download.php?file=$1 [L]

      

There hasn't been a time since I wrote my last PHP code and I haven't tested this. But he should give some good information on how to implement option 1)



EDIT 2: If you submit a tracking request to www.google-analytics.com/debug/collect, you will get some validation information, telling you if your request is valid or not (it will not track the event though).

EDIT 3: Ok, so I checked the page that uses analytics.js. The script sets the following cookies:

_ga=GA1.3.1788966449.1501761573
_gid=GA1.3.1010429060.1501761573

      

Later in collection requests it sets

cid:1788966449.1501761573
_gid:1010429060.1501761573

      

So, it seems to you that you need to split the string a bit according to what you find in the cookie _ga. (I have updated the code above)

EDIT 4: In case anyone is wondering, this is the request that the .js script analyst generates with the above cookie values.

GET https://www.google-analytics.com/collect?v=1&_v=j56&a=1178408574&t=pageview&_s=1&dl=https%3A%2F%2Fdevelopers.google.com%2Fanalytics%2Fdevguides%2Fcollection%2Fanalyticsjs%2Fcommand-queue-reference&ul=de&de=UTF-8&dt=The%20ga%20Command%20Queue%20Reference%20%C2%A0%7C%C2%A0%20Analytics%20for%20Web%20(analytics.js)%20%C2%A0%7C%C2%A0%20Google%20Developers&sd=24-bit&sr=1920x1200&vp=1899x1072&je=0&_u=QDCAAAIhI~&jid=&gjid=&cid=1788966449.1501761573&tid=UA-41425441-2&_gid=1010429060.1501761573&z=1116872044

      

+2


source







All Articles