GA4MeasurementProtocolClient.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * GA4MeasurementProtocolClient.php
  4. *
  5. * 一个用于通过GA4 Measurement Protocol发送事件的PHP类。
  6. */
  7. class GA4MeasurementProtocolClient
  8. {
  9. private $measurement_id;
  10. private $api_secret;
  11. private $firebase_app_id;
  12. private $endpoint = 'https://www.google-analytics.com/mp/collect';
  13. /**
  14. * 构造函数
  15. *
  16. * @param string $measurement_id GA4的测量ID (G-XXXXXXX)
  17. * @param string $api_secret API秘密
  18. * @param string|null $firebase_app_id Firebase应用ID (可选)
  19. */
  20. public function __construct($measurement_id, $api_secret, $firebase_app_id = null)
  21. {
  22. $this->measurement_id = $measurement_id;
  23. $this->api_secret = $api_secret;
  24. $this->firebase_app_id = $firebase_app_id;
  25. }
  26. /**
  27. * 发送事件到GA4
  28. *
  29. * @param string $client_id 客户端ID (通常是UUID)
  30. * @param string $event_name 事件名称
  31. * @param array $event_params 事件参数(键值对)
  32. * @param int|null $timestamp_ms 自定义时间戳(可选)
  33. *
  34. * @return bool 返回true表示发送成功,false表示失败
  35. */
  36. public function sendEvent($client_id, $event_name, $event_params = [], $timestamp_ms = null)
  37. {
  38. $payload = [
  39. 'client_id' => $client_id,
  40. 'events' => [
  41. [
  42. 'name' => $event_name,
  43. 'params' => $event_params
  44. ]
  45. ]
  46. ];
  47. if ($timestamp_ms) {
  48. $payload['events'][0]['timestamp_micros'] = $timestamp_ms * 1000; // 转换为微秒
  49. }
  50. $url = $this->endpoint . '?measurement_id=' . $this->measurement_id . '&api_secret=' . $this->api_secret;
  51. $response = $this->postRequest($url, $payload);
  52. if ($response !== false) {
  53. // GA4的响应状态码
  54. $http_code = $response['http_code'];
  55. return in_array($http_code, [200, 204]);
  56. }
  57. return false;
  58. }
  59. /**
  60. * 发送POST请求
  61. *
  62. * @param string $url 请求URL
  63. * @param array $data 请求数据
  64. *
  65. * @return array|false 返回包含响应的数组,或者false失败
  66. */
  67. private function postRequest($url, $data)
  68. {
  69. $ch = curl_init($url);
  70. $payload = json_encode($data);
  71. curl_setopt($ch, CURLOPT_POST, true);
  72. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  73. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  74. 'Content-Type: application/json',
  75. 'Content-Length: ' . strlen($payload)
  76. ]);
  77. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  78. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 确保SSL安全
  79. $response = curl_exec($ch);
  80. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  81. if ($response === false) {
  82. error_log('cURL error: ' . curl_error($ch));
  83. curl_close($ch);
  84. return false;
  85. }
  86. curl_close($ch);
  87. return [
  88. 'body' => $response,
  89. 'http_code' => $http_code
  90. ];
  91. }
  92. }