Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const measurementId = 'G-Q74L0TS3QB';
- const apiSecret = 'cVVzH6uPSBaDsyy3ZqfkHA';
- fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json"
- },
- body: JSON.stringify({
- "client_id": clientId, // String: Must be retrieved from the client browser cookie (_ga)
- "user_id": userId, // String: Your internal unique user ID
- "timestamp_micros": timestampMicros, // String: Unix epoch timestamp in microseconds
- "events": [
- {
- "name": "confirmation_deposit_placed",
- "params": {
- "session_id": sessionId, // String: GA4 session ID from the web client (_ga_xxxxxx)
- "deposit_id": depositId, // String: Unique identifier for the transaction
- "currency": currencyCode, // String: 3-letter ISO 4217 code (e.g., "UGX", "USD")
- "value": depositValue // Numeric: Double or Integer (do NOT wrap in quotes)
- }
- }
- ]
- })
- })
- .then(response => {
- // GA4 explicitly returns a 204 No Content status on successful delivery
- if (response.status === 204) {
- console.log("GA4 Web Event successfully dispatched.");
- } else {
- console.error(`GA4 Error Status: ${response.status}`);
- }
- })
- .catch(error => console.error("Network error reporting to GA4:", error));
Advertisement
Add Comment
Please, Sign In to add comment