thomaskutama

PEPETA UG Measurement Protocol | confirmation deposit placed | gtag.js

Jul 7th, 2026 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const measurementId = 'G-Q74L0TS3QB';
  2. const apiSecret = 'cVVzH6uPSBaDsyy3ZqfkHA';
  3.  
  4. fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, {
  5.   method: "POST",
  6.   headers: {
  7.     "Content-Type": "application/json"
  8.   },
  9.   body: JSON.stringify({
  10.     "client_id": clientId,               // String: Must be retrieved from the client browser cookie (_ga)
  11.     "user_id": userId,                   // String: Your internal unique user ID
  12.     "timestamp_micros": timestampMicros, // String: Unix epoch timestamp in microseconds
  13.     "events": [
  14.       {
  15.         "name": "confirmation_deposit_placed",
  16.         "params": {
  17.           "session_id": sessionId,       // String: GA4 session ID from the web client (_ga_xxxxxx)
  18.           "deposit_id": depositId,       // String: Unique identifier for the transaction
  19.           "currency": currencyCode,       // String: 3-letter ISO 4217 code (e.g., "UGX", "USD")
  20.           "value": depositValue          // Numeric: Double or Integer (do NOT wrap in quotes)
  21.         }
  22.       }
  23.     ]
  24.   })
  25. })
  26. .then(response => {
  27.   // GA4 explicitly returns a 204 No Content status on successful delivery
  28.   if (response.status === 204) {
  29.     console.log("GA4 Web Event successfully dispatched.");
  30.   } else {
  31.     console.error(`GA4 Error Status: ${response.status}`);
  32.   }
  33. })
  34. .catch(error => console.error("Network error reporting to GA4:", error));
Advertisement
Add Comment
Please, Sign In to add comment