从perl脚本发布谷歌分析指标

时间:2014-11-23 16:54:51

标签: perl google-analytics

我需要从perl cron作业生成一个谷歌分析事件。

基于https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide 发布到www.google-analytics.com/collect会接受指标。

我尝试使用以下代码发布指标,但我没有在Google Analytics实时页面中看到它们。

use LWP::UserAgent; 
$ua=LWP::UserAgent->new; 
$ua->default_header("Content-type" => "application/x-rm-urlencoded"); 
my $response = $ua->post("http://www.google-analytics.com/collect", 
"v" => 1, 
"tid" => "UA-XXXXXXXX-Y", 
"cid" => 125, 
"t" => "pageview", 
"dh" => "Cron", 
"dp" => "Cron.php", 
"dt" => "CronTitle"); 
print $response->status_line;'

响应是“200 OK”,但我没有在Google Analytics分析网页中看到这些数据。

1 个答案:

答案 0 :(得分:0)

我能够从我的php脚本发布分析,但无法从perl脚本发布分析。

我解决了我的问题如下。 创建了一个实用程序php脚本,并从我的perl脚本调用该php脚本。然后它奏效了。

<?php 
    $ec = $_GET['ec'];
    $ea = $_GET['ea'];
    $el = $_GET['el'];
    $ev = $_GET['ev'];

    // Send event tracking as well.
    $url = 'http://www.google-analytics.com/collect';
    $event_data = array(
            'v' => '1', 
            'tid' => 'UA-XXXXXX-Y', 
            'cid' => $_SERVER["REMOTE_ADDR"], 
            't' => 'event', 
            'ec' => $ec,
            'ea' => $ea,
            'el' => $el,
            'ev' => $ev);
    $event_options = array(
            'http' => array(
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                'method'  => 'POST',
                'content' => http_build_query($event_data),
            ),
        );
    $event_context  = stream_context_create($event_options);
    $result = file_get_contents($url, false, $event_context);

    echo "XXX";
?>