简体   繁体   English

在没有浏览器生成的用户代理的情况下,如何通过 cURL 将事件数据发送到 Google Measurement Protocol?

[英]How can I send event data to Google Measurement Protocol via cURL without a browser generated user-agent?

I am generating leads via Facebook Lead Ads.我正在通过 Facebook 潜在客户广告生成潜在客户。 My server accepts the RTU from Facebook and I am able to push the data around to my CRM as required for my needs.我的服务器接受来自 Facebook 的 RTU,我可以根据需要将数据推送到我的 CRM。

I want to send an event to GA for when the form is filled out on Facebook.我想在 Facebook 上填写表格时向 GA 发送一个事件。

Reading over the Google Measurement Protocol Reference it states:阅读谷歌测量协议参考它指出:

user_agent_string – Is a formatted user agent string that is used to compute the following dimensions: browser, platform, and mobile capabilities. user_agent_string – 是一个格式化的用户代理字符串,用于计算以下维度:浏览器、平台和移动功能。

If this value is not set, the data above will not be computed.如果未设置此值,则不会计算上述数据。

I believe that because I am trying to send the event via a PHP webhook script where no browser is involved, the request is failing.我相信因为我试图通过不涉及浏览器的 PHP webhook 脚本发送事件,所以请求失败。

Here is the relevant part of the code that I'm running (I changed from POST to GET thinking that might have been the issue, will change this back to POST once it's working):这是我正在运行的代码的相关部分(我从 POST 更改为 GET 认为这可能是问题所在,一旦它工作,将其更改回 POST):

$eventData = [
      'v' => '1',
      't' => 'event',
      'tid' => 'UA-XXXXXXX-1',
      'cid' => '98a6a970-141c-4a26-b6j2-d42a253de37e',
      'ec' => 'my-category-here',
      'ea' => 'my-action-here',
      'ev' => 'my-value-here
  ];

  //Base URL for API submission
  $googleAnalyticsApiUrl = 'https://www.google-analytics.com/collect?';

  //Add vars from $eventData object
  foreach ($eventData as $key => $value) {
    $googleAnalyticsApiUrl .= "$key=$value&";
  }

  //Remove last comma for clean URL
  $googleAnalyticsApiUrl = substr($googleAnalyticsApiUrl, 0, -1);


  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $googleAnalyticsApiUrl);
  curl_setopt($ch,CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);
  curl_close($ch);

I believe it is the user-agent that is causing the issue as if I manually put the same URL into the browser than I'm trying to hit, the event appears instantly within the Realtime tracking in GA.我相信这是导致问题的用户代理,就好像我手动将相同的 URL 放入浏览器中,而不是尝试点击,事件立即出现在 GA 的实时跟踪中。

An example of said URL is:所述 URL 的示例是:

https://www.google-analytics.com/collect?v=1&t=event&tid=UA-XXXXX-1&cid=98a6a970-141c-4a26-b6j2-d42a253de37e&ec=my-category-here&ea=my-action-here&el=my-value-here https://www.google-analytics.com/collect?v=1&t=event&tid=UA-XXXXX-1&cid=98a6a970-141c-4a26-b6j2-d42a253de37e&ec=my-category-here&ea=my-action-here&el=my-value-这里

I have used both the live endpoint and the /debug/ endpoint.我同时使用了 live 端点和 /debug/ 端点。 My code will not submit without error to either, yet if I visit the relevant URLs via browser, the debug endpoint says all is ok and then on the live endpoint the event reaches GA as expected.我的代码不会毫无错误地提交给任何一个,但是如果我通过浏览器访问相关的 URL,调试端点会说一切正常,然后在实时端点上,事件按预期到达 GA。

I'm aware that curl_setopt($ch,CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);我知道curl_setopt($ch,CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); is trying to send the user-agent of the browser, I have tried filling this option with things such as正在尝试发送浏览器的用户代理,我尝试用诸如

"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36" “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36”

but it never gets accepted by the Measurement Protocol.但它永远不会被测量协议接受。

My Questions我的问题

  1. Is it possible for me to send these events to GA without a web browser being used in the process?我是否可以在不使用 web 浏览器的情况下将这些事件发送到 GA? I used to have Zapier push these events for me, so I assume it is possible.我曾经让 Zapier 为我推送这些事件,所以我认为这是可能的。

  2. How do I send a valid user_agent_string via PHP?如何通过 PHP 发送有效的 user_agent_string? I have tried spoofing it with 'CURLOPT_USERAGENT', but never manage to get them working.我试过用'CURLOPT_USERAGENT'来欺骗它,但从来没有设法让它们工作。

I had the same problem: fetching the collect URL from my browser worked like a charm (I saw the hit in the Realtime view), but fetching with with curl or wget did not.我遇到了同样的问题:从我的浏览器中获取收集 URL 就像一个魅力(我在实时视图中看到了命中),但使用curlwget没有。 On the terminal, using httpie also wored.在终端上,用httpie也戴上了。

I sent a user agent header with curl , and that did solve the issue.我用 curl 发送了一个用户代理curl ,这确实解决了这个问题。

So I am bit puzzled by @daveidivide last comment and that his initial hypothesis was wrong (I mean, I understand that he might have had 2 problems, but sending the user-agent header seems mandatory).所以我对@daveidivide 最后的评论感到困惑,他最初的假设是错误的(我的意思是,我知道他可能有两个问题,但发送用户代理 header 似乎是强制性的)。

In my experience, Google Analytics simply refrains from tracking requests from cURL or wget (possibly others)... perhaps in an attempt to filter out unwanted noise...?根据我的经验,谷歌分析只是避免跟踪来自 cURL 或 wget(可能是其他人)的请求......也许是为了过滤掉不需要的噪音......? ♂️ ♂️

Any request with a User-Agent including the string "curl" won't get tracked.任何包含字符串"curl"的用户代理请求都不会被跟踪。 Overriding the User-Agent header to pretty much anything else , GA will track it.将 User-Agent header 覆盖到几乎所有其他内容,GA 将对其进行跟踪。

If you neglect to override the User-Agent header when using cURL, it'll include a default header identifying itself... and GA will ignore the request.如果您在使用 cURL 时忽略了覆盖 User-Agent header,它将包含一个默认的 header 标识自己......并且 GA 将忽略该请求。

This is also the case when using a package like Guzzle , which also includes its own default User-Agent string (eg "GuzzleHttp/6.5.5 curl/7.65.1 PHP/7.3.9" ).当使用像 Guzzle 这样的package时也是这种情况,它还包括自己的默认用户代理字符串(例如"GuzzleHttp/6.5.5 curl/7.65.1 PHP/7.3.9" )。

As long as you provide your own custom User-Agent header, GA should pick it up.只要您提供自己的自定义 User-Agent header,GA 应该会选择它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM