我有一个用php编写的网络服务,它是从PC上安装的桌面应用程序调用的。我想有一个在网络服务上调用函数的用户的注册,为此我只想向谷歌分析发送命中。
PHP 中的网络服务:
<?php
require_once('lib/nusoap.php'); // basic include.. must go at the top
$SERVICE_NAMESPACE = "urn:Service"; // create a namespace to run under.
$server = new soap_server(); // the soap object from the include above.
// this has many input parameters but we only need two: the service name and the namespace
$server->configureWSDL('Service', $SERVICE_NAMESPACE);
$server->register('test',// method name
array('name' => 'xsd:string', 'name99' => 'xsd:string'),// input parameter called name.. and it's a string.
array('return' => 'xsd:string'),// output - one string is returned called "return"
$SERVICE_NAMESPACE,// namespace
$SERVICE_NAMESPACE . '#hello1',// soapaction
'rpc',// style.. remote procedure call
'encoded',// use of the call
'Nada interesante'// documentation for people who hook into your service.
);
function test($sName,$sName99)
{
return 'TEST ';
}
//This processes the request and returns a result.
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
我想拥有谷歌分析信息,为此我想集成以下脚本:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-89356985-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
gtag('js', new Date());
gtag('config', 'UA-89356985-1');
</script>
我不知道如何集成到测试功能中。我想知道用户何时调用测试函数。
提前非常感谢。
你不能使用Javascript跟踪器,除非你的PHP脚本生成在浏览器中执行的HTML和Javascript。
但是,您可以使用测量协议对Google Analytics进行服务器端调用。这基本上是一个端点,您可以在其中发送带有自定义值的预定义参数(通过任何可以进行http调用的方法(,它们将在Google Analytics中注册。
这可以用作您需要的示例。 使用http_build_query
来构建你想要的命中 谷歌分析.更改Google_Analytics_UA_String
以匹配您的 Google Analytics(分析( ID
<?php
//some of the functions we need to make it work
function generate_serial($n) {
$c = "abcdefghijklmnopqrstuvwyxz0123456789";
$s = '';
for($i=0;$i<$n;$i++) {
$s .= substr($c,rand(0,37),1);
}
return $s;
}
function generate_uuid() {
return generate_serial(8).'-'.generate_serial(4).'-4'.generate_serial(3).'-a'.generate_serial(3).'-'.generate_serial(12);
}
function ip() {
$ip = false;
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$ip = trim(array_shift($ip));
}
elseif(isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
//define necessary variables
define('Google_Analytics_UA_String','UA-XXXXXXXX-X');
//create a UUID string for the user sending the request and store in the the session
if(isset($_COOKIE['Google_Analytics_UUID']) and !empty($_COOKIE['Google_Analytics_UUID'])) {
define('Google_Analytics_UUID',$_COOKIE['Google_Analytics_UUID']);
}
else {
define('Google_Analytics_UUID',generate_uuid());
setcookie('Google_Analytics_UUID',Google_Analytics_UUID,time()+63072000);
}
//compile the data we want to send to the API
$data = http_build_query(array(
'v' => 1, // version
'ds' => 'app', // data source
'tid' => Google_Analytics_UA_String, // Tracking ID / Web Property ID
'cid' => Google_Analytics_UUID, // Client ID
'uip' => ip(), // IP Override
't' => 'event', // Hit type
'ec' => 'site clicks', // event category
'ea' => 'click', // event action
'el' => 'button', // event label
'ev' => 'Click here!' // event value
));
//send using PHP's cURL extension
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'https://www.google-analytics.com/collect');
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$response = curl_exec($ch);
//parse the response and send back to the browser
header('Content-Type: application/json');
$status = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($status == 200) {
echo json_encode([
'success' => true
]);
}
else {
echo json_encode([
'error' => true
]);
}
?>
确切的运行示例是:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://www.google-analytics.com/collect?v=1&tid=UA-XXXXXXX-1&cid=35009a79-1a05-49d7-b876-2b884d0f825b&uid=myserid&uip=179.52.60.197&t=event&ec=Service&ea=SER&el=999999&z=54564653213',
CURLOPT_USERAGENT => 'Vanity-URL-Tracker',
));
$resp = curl_exec($curl);
curl_close($curl);
?>
我有一个页面跟踪的例子:
public function send($trackingId, $host, $page, $title)
{
$google_analytics_url = 'https://www.google-analytics.com/collect';
$google_analytics_params = http_build_query(array(
'v' => 1,
'tid' => $trackingId, // UA-xxxxxxx-x
'cid' => uniqid(),
't' => 'pageview',
'dh' => $host,
'dp' => '/'.$page,
'dt' => urlencode($title),
));
$url = $google_analytics_url.'?'.$google_analytics_params;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Vanity-URL-Tracker',
));
$resp = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if($status!= 200) {
log_message('error', json_encode(array('status' => $status, 'response' => $resp)) );
}
if (curl_errno($curl)) {
log_message('error', curl_error($curl));
}
curl_close($curl);
return $resp;
}