简体   繁体   中英

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. My server accepts the RTU from Facebook and I am able to push the data around to my CRM as required for my needs.

I want to send an event to GA for when the form is filled out on Facebook.

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.

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.

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):

$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.

An example of said URL is:

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

I have used both the live endpoint and the /debug/ endpoint. 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.

I'm aware that 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"

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? I used to have Zapier push these events for me, so I assume it is possible.

  2. How do I send a valid user_agent_string via PHP? I have tried spoofing it with 'CURLOPT_USERAGENT', but never manage to get them working.

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. On the terminal, using httpie also wored.

I sent a user agent header with curl , and that did solve the issue.

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).

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...? ♂️

Any request with a User-Agent including the string "curl" won't get tracked. Overriding the User-Agent header to pretty much anything else , GA will track it.

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.

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" ).

As long as you provide your own custom User-Agent header, GA should pick it up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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