1

I'm trying to send a purchase event to Google Analytics 4 using the Measurement Protocol from my Laravel backend. I'm not using gtag.js or Firebase — only server-side requests.

Here’s what I’m doing:

  1. On the frontend, I extract the client_id from the _ga cookie using JavaScript and send it to the backend (e.g., via Ajax or form data).
  2. I store that client_id in the database (associated with the user or session).
  3. Later, when the user completes a purchase (which could be minutes or even days after the initial visit), I use the previously saved client_id to send a purchase event via Measurement Protocol.

Here's a simplified version of the code I use:

public function gtmPaidOrder() {
    $clientId = $this->client_id; // previously saved from frontend _ga cookie

    if (!$clientId) return false;

    $measurement_id = 'G-XXXXXXX';
    $api_secret = MY_SECRET;

    $payload = [
        'client_id' => $clientId,
        'events' => [
            [
                'name'   => 'purchase',
                'params' => [
                    'transaction_id' => $this->id,
                    'value'          => $this->getTotalPrice(),
                    'currency'       => $this->currency,
                    'items'          => [
                        [
                            'item_id' => '123',
                            'item_name' => 'Test Product',
                            'price' => 20,
                            'quantity' => 1
                        ]
                    ],
                    'debug_mode' => true
                ]
            ]
        ]
    ];

    $url = "https://www.google-analytics.com/debug/mp/collect?measurement_id={$measurement_id}&api_secret={$api_secret}";

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    \Log::info('GA4 MP response', ['response' => $response]);
}

Google returns a 200 OK response with the following body:

{
  "validationMessages": [ ]
}

All other events sent from the frontend appear in DebugView, but this event does not. Why?

2 Answers 2

1

To see the event in DebugView, you must also send a frontend event (like page_view) using the same client_id shortly before the server-side event, so you create an active session that DebugView can associate with your server-side event.

Try to send a frontend event with debug_mode: true and the same client_id before your backend call, checking that the client_id format is correct (i.e. 1234567890.1234567890, and not the full cookie value).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestion! Just to clarify — when I say: "On the frontend, I extract the client_id from the _ga cookie using JavaScript and send it to the backend." — I mean I extract the client_id right when the user completes the order. So it's coming from an active session of a real user — I'm not using a stale ID saved days ago. Even when the user pays immediately after I send the event from the backend with the client_id, the purchase event still doesn’t appear in DebugView. Any idea why?
1

For it to appear on debugview, you cannot use the debug url, ironically. Use the live url with debug_mode = true parameter. It worked for me.

https://www.google-analytics.com/mp/collect

1 Comment

Special thanks — you were absolutely right about needing a live session and using the correct URL! he key was to use the correct URL: ✅ google-analytics.com/mp/collect ❌ instead of google-analytics.com/debug/mp/collect With debug_mode: true in the payload, events now show up correctly in DebugView. Appreciate the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.