我一直在努力在Wordpress网站上实施Google的测量协议。 Apache服务器,使用PHP 5.4.35。
我想要做的是将服务器端访问者数据加载到Google Analytics中。该网站在服务器日志和GA数据之间存在巨大差异,我希望这两者能够(有些)匹配。
我已经the code published by Stu Miller on his website,并进行了一些细微的编辑,希望能够反映我的用例。我已将以下内容添加到我的wp-includes / functions.php文件中:
require( ABSPATH . WPINC . '/http.php' );
require( ABSPATH . WPINC . '/class-http.php' );
require( ABSPATH . WPINC . '/general-template.php' );
// Handle the parsing of the _ga cookie or setting it to a unique identifier
// Generate UUID v4 function - needed to generate a CID when one isn't available
function gaGenUUID() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
function gaParseCookie() {
if (isset($_COOKIE['_ga'])) {
list($version,$domainDepth, $cid1, $cid2) = split('[\.]', $_COOKIE["_ga"],4);
$contents = array('version' => $version, 'domainDepth' => $domainDepth, 'cid' => $cid1.'.'.$cid2);
$cid = $contents['cid'];
}
else $cid = gaGenUUID();
return $cid;
}
function gaBuildHit( $method = null, $info = null ) {
if ( $method && $info) {
// Standard params
$v = 1;
$tid = "UA-xxxxxxx"; // Put your own Analytics ID in here
$cid = gaParseCookie();
// Register a PAGEVIEW
if ($method === 'pageview') {
// Send PageView hit
$data = array(
'v' => $v,
'tid' => $tid,
'cid' => $cid,
't' => 'pageview',
'dt' => $info['title'],
'dp' => $info['slug']
);
gaFireHit($data);
} // end pageview method
// Register an ECOMMERCE TRANSACTION (and an associated ITEM)
else if ($method === 'ecommerce') {
// Set up Transaction params
$ti = uniqid(); // Transaction ID
$ta = 'SI';
$tr = $info['price']; // transaction value (native currency)
$cu = $info['cc']; // currency code
// Send Transaction hit
$data = array(
'v' => $v,
'tid' => $tid,
'cid' => $cid,
't' => 'transaction',
'ti' => $ti,
'ta' => $ta,
'tr' => $tr,
'cu' => $cu
);
gaFireHit($data);
// Set up Item params
$in = urlencode($info['info']->product_name); // item name;
$ip = $tr;
$iq = 1;
$ic = urlencode($info['info']->product_id); // item SKU
$iv = urlencode('SI'); // Product Category - we use 'SI' in all cases, you may not want to
// Send Item hit
$data = array(
'v' => $v,
'tid' => $tid,
'cid' => $cid,
't' => 'item',
'ti' => $ti,
'in' => $in,
'ip' => $ip,
'iq' => $iq,
'ic' => $ic,
'iv' => $iv,
'cu' => $cu
);
gaFireHit($data);
} // end ecommerce method
}
}
global $post;
$data = array(
// Get the queried object and sanitize it
'title' => $title = $post->post_title,
'slug' => $slug = $post->post_name
);
gaBuildHit( 'pageview', $data);
// See https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
function gaFireHit( $data = null ) {
if ( $data ) {
$getString = 'https://ssl.google-analytics.com/collect';
$getString .= '?payload_data&';
$getString .= http_build_query($data);
$result = wp_remote_get( $getString );
#$sendlog = error_log($getString, 1, "ME@EMAIL.COM"); // comment this in and change your email to get an log sent to your email
return $result;
}
return false;
}
我一路上收到了一些错误,这些错误始于未定义wp_remote_get()函数,导致我包括:
require( ABSPATH . WPINC . '/http.php' );
此包含将错误列表向上移动到未找到的WP_Http()类,这导致我包含:
require( ABSPATH . WPINC . '/class-http.php' );
......等等。我不记得这个错误促使我加入了:
require( ABSPATH . WPINC . '/general-template.php' );
我认为这是一个PHP致命错误:调用未定义的函数get_bloginfo()。
无论如何,我认为我做错了。最新的致命错误是:致命错误:在/ home / 用户 / public_html / 域 / wp-includes / general-template中调用未定义函数home_url()第658行.php
我查了一下Wordpress Codex,看起来wp_remote_safe_post()似乎是Google recommending POST instead of GET due to the restricted payload on GET更好的选择。
不幸的是,我缺乏理解代码应该如何使用的经验,我一直希望初始代码足够好,可能会有一些调整。
我是否对包含有问题?或者wordpress 4.6.1已经向前推进了所有这些都被弃用了?或者我可能使用了错误的functions.php文件?