GA4 - Trigger events via PHP

305 views Asked by At

I use a Google Tag Manager (GTM) account to send events to Google Analytics 4 (GA4). I have a tag configured in my GTM that triggers when the page URL contains a specific parameter. This tag gets the parameter in the URL and sends a custom event called 'custom_purchase_event' to GA4. GA4 receives it, and the value is visible on the Engagement -> Events page.

A few days ago, we noticed that for some particular users, the tag was not being triggered, and therefore the event didn't reach GA4. I thought that a better solution would be to trigger the events via PHP. I tried to use the Measurement Protocol for GA4 but I can't get it to work. I get a 204 status, but the event doesn't show on GA4. I'm using Guzzle to make the request:

// Create a Guzzle HTTP client
$client = new Client();

$payload = [
     'client_id' => '12345_XXX',
     'events' => [
          [
               'name' => 'custom_purchase_event',
               'params' => [
                    'value' => $value,
                    'discount' => $discount,
                    'currency' => $currency,
                    'items' => [
                         "item": "1",
                         "item_id": "1_123456XXX",
                         "item_name": "Chair",
                    ]
               ]
          ]
      ]
];

// Send POST request to GA4 Measurement Protocol API using Guzzle
$response = $client->post("https://www.google-analytics.com/mp/collect", [
     'query' => [
          'api_secret' => $this->api_secret,
          'measurement_id' => $this->measurement_id
     ],
     'json' => json_encode($payload),
     'headers' => [
          'Content-Type' => 'application/json',
     ]
]);

Is there a way of triggering events in GA4 via PHP?

0

There are 0 answers