[英]send a pageview event via Measurement Protocol to a GA4 property
How can I send a pageview event via Measurement Protocol to a GA4 property with PHP?如何通过 Measurement Protocol 将网页浏览事件发送到使用 PHP 的 GA4 媒体资源?
This is how I'm doing, but inside my Google Analytics 4 property I can't see any traffic.我就是这样做的,但在我的 Google Analytics 4 属性中,我看不到任何流量。
$data = array(
'api_secret' => 'XXXX-YYYYY',
'measurement_id' => 'G-12345678',
'client_id' => gen_uuid(), // generates a random id
'events' => array(
'name' => 'page_view',
'params' => array(),
)
);
$url = 'https://www.google-analytics.com/mp/collect';
$content = http_build_query($data);
$content = utf8_encode($content);
$ch = curl_init();
curl_setopt($ch,CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_POST, TRUE);
curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
I'm working on registering pageviews to track API usage right now, here's what I've found:我现在正在注册页面浏览量以跟踪 API 使用情况,这就是我发现的:
XTOTHEL is right about setting the content type to content/json above. XTOTHEL 将内容类型设置为上面的 content/json 是正确的。 In addition to specifying the content type you also have to send JSON data as the CURLOPT_POSTFIELDS data.
除了指定内容类型之外,您还必须将 JSON 数据作为 CURLOPT_POSTFIELDS 数据发送。
Also per their specification the api_secret and measurement_id need to be part of the URI: https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events?client_type=gtag#required_parameters同样根据他们的规范,api_secret 和 measure_id 需要成为 URI 的一部分: https ://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events?client_type=gtag#required_parameters
Lastly, you can use debug mode to validate your responses and figure out what's going on now by simply changing the URL to google-analytics.com/ debug/mp/collect最后,您可以使用调试模式来验证您的响应,并通过简单地将 URL 更改为google-analytics.com/debug/mp/collect来弄清楚现在发生了什么
Here's the code I'm working with right now:这是我现在正在使用的代码:
//retrieve or generate GA tracking id
if (empty($_COOKIE['_cid'])) {
setcookie('_cid', vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4)));
}
$data = '{"client_id":"'.$_COOKIE['_cid'].'","events":[{"name":"load_endpoint","params":{"page_location":"'.$request->fullUrl().'"}}]}';
echo '<pre>';
print_r($data);
$measurement_id = 'G-xxxxx';
$api_secret = 'xxxx';
$url = 'https://www.google-analytics.com/debug/mp/collect?api_secret='.$api_secret.'&measurement_id='.$measurement_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
This works to a certain extent.这在一定程度上起作用。 Currently it's registering the page view as a custom event instead of an actual pageview though.
目前,它将页面视图注册为自定义事件,而不是实际的页面视图。 I'm still trying to figure out how to get them to come through as page views.
我仍在试图弄清楚如何让它们以页面浏览量的形式出现。
Follow up After a little more debugging I figured out page views are actually working, they just weren't showing up in some of the views.跟进经过更多调试后,我发现页面视图实际上是有效的,它们只是没有出现在某些视图中。 The fix for that was to add page_title into the params:
解决方法是将 page_title 添加到参数中:
$data = '
{
"client_id": "'.$_COOKIE['_cid'].'",
"events": [
{
"name": "page_view",
"params": {
"page_location": "'.$request->fullUrl().'",
"page_title": "'.$request->path().'"
}
}
]
}
';
A few extra notes for whoever comes next:对于接下来的人来说,还有一些额外的注意事项:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.