将文件下载事件从服务器发送到Google Analytics(分析) [英] Send file download event from server to Google Analytics

查看:71
本文介绍了将文件下载事件从服务器发送到Google Analytics(分析)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下载链接通过电子邮件发送给客户 :

A download link is sent to a customer via email:

你好,
请在此处找到您的产品: https://www.example.com/files/yourfile.zip

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

我想作为Goal转换在Google Analytics(分析)中跟踪此下载.

I'd like to track this download in Google Analytics, as a Goal conversion.

不幸的是,当用户单击链接时,文件将直接由网络服务器传送,而不会进入.html页面.

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

如何在Analytics(分析)中跟踪此类直接文件下载?

  1. 我应该在中间"添加一个虚拟HTML页面,该页面将使用analytics.js跟踪代码段,并使用ga.send(...)将下载事件发送到GA,然后在500毫秒后重定向到实际文件和setTimeout(redirect, 500)吗?真的是一种干净安全的解决方案吗?我看到许多潜在的小问题:500毫秒可以吗?应该使用哪种重定向?同样,禁用了JS的用户将永远不会获取其文件...或者如果使用<noscript>则无法记录目标转换.

  1. Should I add a dummy HTML page "in the middle", that would use the analytics.js tracking snippet and send a download event to GA with ga.send(...), and then redirect to the actual file after 500 milliseconds with setTimeout(redirect, 500)? Is it really a clean and safe solution? I see many little potential issues: is 500 ms ok? what kind of redirection should be used? Also a user with JS disabled will never get his file... or if using <noscript> no Goal conversion can be recorded.

有没有一种方法可以让Apache(谁为客户端提供yourfile.zip服务)或PHP在提供此文件后向GoogleAnalytics发送跟踪事件?

Is there a way to ask Apache (who serves the yourfile.zip to the client) or PHP to send a tracking event to GoogleAnalytics when this file is served?

另一种解决方案?

无论客户端是否启用JS,解决方案2似乎都具有100%可靠的优势.

It seems that solution 2. would have the advantage of being 100% reliable, no matter the client has JS enabled or not.

但是,另一方面,我不想使用很少使用的hack.对于这种非常常见的情况,通常的解决方案是什么?

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

推荐答案

Google Analytics(分析)实际上具有用于从任意来源发送分析数据的协议.请参阅此处: https://developers.google.com/analytics/devguides/集合/协议/v1/

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

因此,让您的网络服务器向Google发送分析事件并不像看起来那样困难.我不确定是否可以直接挂接到Apache来生成这些事件.但是,我确实至少看到了两种解决方案.

So having your webserver send an analytics event to Google is not as hacky as it might seem. I'm not sure if you can hook into Apache directly to generate these events. However, I do see at least two solutions.

1)将所有下载重定向到服务器端脚本,该脚本发送数据并可以生成所需的分析事件.
2)解析服务器日志并从中生成分析事件.

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

解决方案1的编辑示例:
确保确保标签之前或之后没有空格,因为这将是发送给客户端的实际响应的一部分.

EDIT Example for solution 1:
Do make sure there are no spaces before or after the tags because this would would be part of the actual response sent to the client.

download.php:

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规则:

.htaccess/mod_rewrite rules:

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

自从我编写了最后一个PHP代码以来,就已经很久了,而且我没有对此进行测试.但这应该为如何实施选项1提供一个很好的要点.

Not it's been ages since I wrote my last PHP code and I didn't test this. But it should give a pretty good gist on how to implement option 1)

如果您将跟踪请求发送到www.google-analytics.com/debug/collect,您将收到一些验证信息,告诉您请求是否有效(不过,它不会跟踪事件).

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

好的,所以我检查了使用analytics.js的页面.该脚本设置以下cookie:

EDIT 3: Okay, so I've checked with a page which uses analytics.js. The script sets the following cookies:

_ga=GA1.3.1788966449.1501761573
_gid=GA1.3.1010429060.1501761573

稍后在它设置的收集请求中

Later on in the collect requests it sets

cid:1788966449.1501761573
_gid:1010429060.1501761573

因此,似乎您需要对_ga cookie中的内容进行一些字符串拆分. (我已经更新了上面的代码)

So it seems like you need to do a little string splitting with what you find in the _ga cookie. (I've updated the code above)

如果有人想知道,这是analytics.js脚本使用上述cookie值生成的请求.

EDIT 4: In case anyone's wondering, this is the request the analytics.js script generates with the cookie values mentioned above.

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

这篇关于将文件下载事件从服务器发送到Google Analytics(分析)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆