通过PHP脚本将值发送到Google Analytics

时间:2012-11-08 18:43:53

标签: php google-analytics

我使用以下脚本将表单中的数据发送到Google Analytics:

if ($result){
        $var_utmac = 'UA-0000000-0';
        $var_utmhn = 'my-site.com'; // domain
        $var_utmn = rand(1000000000,9999999999); // random number
        $var_cookie = rand(10000000,99999999); //random cookie number
        $var_random = rand(1000000000,2147483647); //number under 2147483647
        $var_today = time();
        $var_referer = $_SERVER['HTTP_REFERER']; //referer url
        if ($var_referer == '') { $var_referer = '-'; }
        $var_uservar='-'; // no user-defined
        $var_utmp= $_POST['REQUEST_URI'].'data_'.  htmlentities($_POST['dataone']).'_'.htmlentities($_POST['datatwo']); // folder called no_jstracker to segment nojavascript visitors
        $urchinUrl='http://www.google-analytics.com/__utm.gif?utmwv=3&utmn=' . $var_utmn . '&utme=&utmcs=-&utmsr=-&utmsc=-&utmul=-&utmje=0&utmfl=-&utmdt=-&utmhn=' . $var_utmhn . '&utmhid=' . $var_utmn . '&utmr=' . $var_referer . '&utmp=' . $var_utmp . '&utmac=' . $var_utmac . '&utmcc=__utma%3D' . $var_cookie . '.' . $var_random . '.' . $var_today . '.' . $var_today . '.' . $var_today . '.2%3B%2B__utmz%3D' . $var_cookie . '.' . $var_today . '.2.2.utmcsr%3D_SOURCE_%7Cutmccn%3D_CAMPAIGN_%7Cutmcmd%3D_MEDIUM_%7Cutmctr%3D_KEYWORD_%7Cutmcct%3D_CONTENT_%3B%2B__utmv%3D' . $var_cookie . '.' . $var_uservar . '%3B';
        echo ' <img src="' . $urchinUrl . '" border="0" />';
    }

虽然成功发送数据有一个问题,那就是分析没有正确显示某些数据,即对于广告系列数据而言只是“ CAMPAIGN ”关键字显示“ KEYWORD ”。很明显脚本中发生了这种情况,但我不确定如何解决它。理想情况下,分析当然应该用自己的数据来填充。

是否有可能提出任何建议?

1 个答案:

答案 0 :(得分:0)

我认为这是因为您硬编码“CAMPAIGN”值。而不是为广告系列分配值。

$CAMPAIGN='facebook';
$KEYWORD='testing';
$urchinUrl='http://www.google-analytics.com/__utm.gif?utmwv=3&utmn=' . $var_utmn . '&utme=&utmcs=-&utmsr=-&utmsc=-&utmul=-&utmje=0&utmfl=-&utmdt=-&utmhn=' . $var_utmhn . '&utmhid=' . $var_utmn . '&utmr=' . $var_referer . '&utmp=' . $var_utmp . '&utmac=' . $var_utmac . '&utmcc=__utma%3D' . $var_cookie . '.' . $var_random . '.' . $var_today . '.' . $var_today . '.' . $var_today . '.2%3B%2B__utmz%3D' . $var_cookie . '.' . $var_today . '.2.2.utmcsr%3D_SOURCE_%7Cutmccn%3D'.$CAMPAIGN.'%7Cutmcmd%3D_MEDIUM_%7Cutmctr%3D'.$KEYWORD.'%7Cutmcct%3D_CONTENT_%3B%2B__utmv%3D' . $var_cookie . '.' . $var_uservar . '%3B';

了解有关Google Analytics Cookies

的更多详情