Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To transmit GA4 events via Stripe PHP webhooks, you can follow these steps:

  1. Set up a GA4 property in your Google Analytics account and obtain the measurement ID.

  2. Set up a webhook endpoint in your PHP application that can receive the Stripe events.

  3. Add the following code to your webhook endpoint to send the GA4 event:

    // Build the measurement protocol payload with the GA4 event data
    $payload = [
       'v' => 2,
       'tid' => 'GA_MEASUREMENT_ID',
       'cid' => 'STRIPE_CUSTOMER_ID',
       't' => 'event',
       'ec' => 'Stripe',
       'ea' => 'EVENT_NAME',
       'el' => 'ITEM_NAME',
       'ev' => AMOUNT,
    ];
    
    // Send the measurement protocol request to Google Analytics
    $ch = curl_init('https://www.google-analytics.com/mp/collect');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    curl_close($ch);
    

    Replace GA_MEASUREMENT_ID with your GA4 measurement ID and STRIPE_CUSTOMER_ID with the ID of the Stripe customer associated with the event. Replace EVENT_NAME with the name of the Stripe event (e.g. charge.succeeded) and ITEM_NAME with a descriptive name for the item (e.g. Premium Subscription). Replace AMOUNT with the amount of the transaction in cents (e.g. $10.00 would be 1000).

  4. Add the webhook endpoint URL to your Stripe account's webhook settings.

Now, whenever a matching Stripe event is received at your webhook endpoint, a corresponding GA4 event will be sent to Google Analytics. You can use these events to track conversions, revenue, and other metrics in your GA4 reports.