Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Kaonashie's avatar

Tracking using GA4 and Spark

Hi ! I've been trying to solve this problem for a while and I can't seem to make heads or tails of it. We have a Laravel install that uses Spark Stripe for subscription management and we want to add conversion tracking for our customers. Except the payment page is hosted on Stripe and none of the dashboard pages are directly accesible so I have to use a webhook and send custom events to GA4.

Has anyone done this in the past? Is there anything that I should know to make this work? I logged some error messages but I'm not getting anything useful from GA4. The events just don't appear in the dashboard. I've already created the custom event and the name in the condition is correct.

My code look like this (there is an event listener that send the right payload to the function. public function handleWebhook(array $payload) { $subscription = $payload['object']; $customer = $subscription['customer'];

    // GA4 Config
    $measurement_id = env('GA_MEASUREMENT_ID');
    $api_secret = env('GA_API_SECRET');

    // GA4 Event Data
    $data = [
        'client_id' => $customer,
        'events' => [
            [
                'name' => 'subscription_created',
                'params' => [
                    'currency' => strtoupper($subscription['currency']),
                    'value' => $subscription['plan']['amount'] / 100, // Converts cents to dollars
                    'subscription_id' => $subscription['id'],
                    'items' => [
                        [
                            'item_id' => $subscription['id'],
                            'item_name' => $subscription['lines']['data'][0]['plan']['nickname'] ?? 'Subscription',
                            'item_category' => 'Subscription',
                            'price' => $subscription['plan']['amount']  / 100
                        ]
                    ]
                ]
            ]
        ]
    ];
    $response = Http::asForm()->post("https://www.google-analytics.com/mp/collect?measurement_id={$measurement_id}&api_secret={$api_secret}", $data);

// Log::info('GA4 Response:', [ // 'status' => $response->status(), // 'body' => $response->body(), // 'headers' => $response->headers(), // 'request_data' => $data // ]); // // if (!$response->successful()) { // Log::error('GA4 Request Failed:', [ // 'error' => $response->body(), // 'status' => $response->status() // ]); // }

    return response()->json([
        'success' => $response->successful()
    ]);
0 likes
0 replies

Please or to participate in this conversation.