简体   繁体   English

来自服务器端的 GA4 自定义事件,有人可以告诉我如何在 php 中执行以下代码吗?

[英]GA4 custom event from server side, can someone tell me how i can do the following code in php?

const measurement_id = `G-XXXXXXXXXX`;
const api_secret = `<secret_value>`;

fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurement_id}&`api_secret`=${api_secret}`, {
  method: "POST",
  body: JSON.stringify({
    client_id: 'XXXXXXXXXX.YYYYYYYYYY',
    events: [{
      name: 'tutorial_begin',
      params: {},
    }]
  })
});

can someone tell me how i can do the above code in php?有人可以告诉我如何在 php 中执行上述代码吗? i have tried the following but it is not working,我尝试了以下方法,但它不起作用,

$data = array(
                'client_id' => self::ga_extract_cid_from_cookies(),
                'user_id' => 123,
                'timestamp_micros' => time(),
                'non_perosnalised_ads' => false,
                'events' => array(
                    'name' => 'login'
                )
            );
$dataString = json_encode($data);

$post_url = 'https://www.google-analytics.com/mp/collect?api_secret=xxxxxxx&measurement_id=xxxxxxxxx';
        $ch = curl_init($post_url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $datastring);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
        curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);

When i run the above its not sending any event to google analytics account.当我运行上述内容时,它不会向谷歌分析帐户发送任何事件。 I don't know what is the error and how to find it.我不知道错误是什么以及如何找到它。 Need help on this pls.需要帮助。

I believe the main problem in your example, apart from the typo in the $datastring variable, was that the user id was sent as an int, not as a string.我相信您的示例中的主要问题,除了 $datastring 变量中的拼写错误外,用户 id 是作为 int 发送的,而不是作为字符串发送的。 My example here works:我的例子在这里有效:

$ip = str_replace('.', '', $_SERVER['REMOTE_ADDR']);
$data = array(
                'client_id' => $ip,
                'user_id' => '123',
                'events' => array(
                    'name' => 'event_serverside'
                )
            );
$datastring = json_encode($data);
$post_url = 'https://www.google-analytics.com/mp/collect?api_secret=xxxxxxxxxx&measurement_id=xxxxxxxxx';
$ch = curl_init($post_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datastring);
curl_setopt($ch, CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POST, TRUE);
$result = curl_exec($ch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以告诉我如何删除或隐藏此 JavaScript 代码吗? - Can someone please tell me how to remove or hide this JavaScript code? 任何人都可以告诉我他们是否在以下PHP代码中看到错误? - Can anyone tell me if they see an error in the following PHP code? 有人可以告诉我如何在PHP中转义“&”符号吗? - Can someone please tell me how to escape an ampersand in PHP? 有人能告诉我 DateTimeZone::getOffset PHP function 是如何工作的吗? - Can someone tell me how the DateTimeZone::getOffset PHP function works? 如何在我的php重定向代码中添加ga事件? - how do I add a ga event to my php redirect code? 有人可以为我解释这个示例 PHP 代码吗? - Can someone explain this sample PHP code for me? 有人可以向我解释一个PHP代码 - Can someone explain me a php code 有人可以告诉我为什么我的$ .ajax调用不从我的PHP页面返回数组吗? - Can someone tell me why my $.ajax call isn't returning the array from my PHP page? 谁能告诉我为什么在以下php代码中不执行查询? - Can anyone tell me why isn't the query executing in the following php code? 有人可以告诉我点击交叉后如何修改下面的脚本以从数据库中删除图像? - can someone tell me how would i modify this below script to delete the image from database after a click on cross?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM