### tutorial_begin Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This event signifies the start of the on-boarding process. Use this in a funnel with tutorial_complete to understand how many users complete the tutorial. ```APIDOC ## POST /mp/collect ### Description This event signifies the start of the on-boarding process. Use this in a funnel with tutorial_complete to understand how many users complete the tutorial. ### Method POST ### Endpoint https://www.google-analytics.com/mp/collect?measurement_id=G-XXXXXXXXXX&api_secret= ### Parameters #### Request Body - **client_id** (string) - Required - The unique identifier for the client. - **events** (array) - Required - An array of event objects. - **name** (string) - Required - The name of the event, should be "tutorial_begin". - **params** (object) - Required - Parameters for the event. (No parameters are suggested for this event.) ### Request Example ```json { "client_id": "client_id", "events": [{ "name": "tutorial_begin", "params": {} }] } ``` ### Response #### Success Response (200) (No specific response schema provided in source) #### Response Example (No specific response example provided in source) ``` -------------------------------- ### Send Tutorial Begin Event Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This event signifies the start of an on-boarding process. No parameters are suggested for this event. ```javascript const measurementId = 'G-XXXXXXXXXX'; const apiSecret = ''; fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, { method: "POST", body: JSON.stringify({ "client_id": "client_id", "events": [{ "name": "tutorial_begin", "params": {} }] }) }); ``` -------------------------------- ### Example Device Information Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference Shows how to populate the 'device' field with details such as category, language, screen resolution, operating system, model, brand, and browser. ```json "device": { "category": "mobile", "language": "en", "screen_resolution": "1280x2856", "operating_system": "Android", "operating_system_version": "14", "model": "Pixel 9 Pro", "brand": "Google", "browser": "Chrome", "browser_version": "136.0.7103.60" } ``` -------------------------------- ### Example User Location Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference Demonstrates the structure for specifying user location details, including city, region, country, subcontinent, and continent. ```json "user_location": { "city": "Mountain View", "region_id": "US-CA", "country_id": "US", "subcontinent_id": "021", "continent_id": "019" } ``` -------------------------------- ### add_shipping_info Event Example Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events An example of how to send an add_shipping_info event with item parameters to Google Analytics. ```APIDOC ## Send Event This endpoint allows you to send event data to Google Analytics. ### Description This section details the parameters available for the `add_shipping_info` event, focusing on item-level details. ### Method POST ### Endpoint `/mp/collect?measurement_id=&api_secret=` ### Parameters #### Request Body - **client_id** (string) - Required - The client ID for the user. - **events** (array) - Required - An array of events to send. - **name** (string) - Required - The name of the event (e.g., `add_shipping_info`). - **params** (object) - Required - Parameters for the event. - **currency** (string) - Optional - The currency of the event value. - **value** (number) - Optional - The total value of the event. - **coupon** (string) - Optional - The coupon code used for the event. - **shipping_tier** (string) - Optional - The shipping tier selected. - **items** (array) - Optional - An array of item objects associated with the event. - **item_id** (string) - Required* - The ID of the item. *One of `item_id` or `item_name` is required.* - **item_name** (string) - Required* - The name of the item. *One of `item_id` or `item_name` is required.* - **affiliation** (string) - Optional - A product affiliation. - **coupon** (string) - Optional - The coupon name/code associated with the item. - **currency** (string) - Optional - The currency of the item price. - **discount** (number) - Optional - The unit monetary discount value associated with the item. - **index** (number) - Optional - The index/position of the item in a list. - **item_brand** (string) - Optional - The brand of the item. - **item_category** (string) - Optional - The category of the item. - **item_category2** (string) - Optional - The second category hierarchy for the item. - **item_category3** (string) - Optional - The third category hierarchy for the item. - **item_category4** (string) - Optional - The fourth category hierarchy for the item. - **item_category5** (string) - Optional - The fifth category hierarchy for the item. - **item_list_id** (string) - Optional - The ID of the list in which the item was presented. - **item_list_name** (string) - Optional - The name of the list in which the item was presented. - **item_variant** (string) - Optional - The item variant or unique code. - **location_id** (string) - Optional - The physical location associated with the item. - **price** (number) - Optional - The monetary unit price of the item. - **google_business_vertical** (string) - Optional - The business vertical. - **quantity** (number) - Optional - Item quantity. ### Request Example ```json { "client_id": "client_id", "events": [ { "name": "add_shipping_info", "params": { "currency": "USD", "value": 30.03, "coupon": "SUMMER_FUN", "shipping_tier": "Ground", "items": [ { "item_id": "SKU_12345", "item_name": "Stan and Friends Tee", "affiliation": "Google Merchandise Store", "coupon": "SUMMER_FUN", "currency": "USD", "discount": 2.22, "index": 0, "item_brand": "Google", "item_category": "Apparel", "item_category2": "Adult", "item_category3": "Shirts", "item_category4": "Crew", "item_category5": "Short sleeve", "item_list_id": "related_products", "item_list_name": "Related Products", "item_variant": "green", "location_id": "ChIJIQBpAG2ahYAR_6128GcTUEo", "price": 10.01, "google_business_vertical": "retail", "quantity": 3 } ] } } ] } ``` ### Response #### Success Response (200) An empty response body indicates success. #### Response Example (No response body) ``` -------------------------------- ### Send Event (General) Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This example demonstrates how to send a general event, 'view_promotion', to Google Analytics 4 using the Measurement Protocol. It includes details about the promotion and associated items. ```APIDOC ## POST /mp/collect ### Description Sends a POST request to the Google Analytics Measurement Protocol endpoint to collect event data. ### Method POST ### Endpoint `https://www.google-analytics.com/mp/collect?measurement_id=&api_secret=` ### Parameters #### Request Body - **client_id** (string) - Required - The client ID for the user. - **events** (array) - Required - An array of event objects. - **name** (string) - Required - The name of the event. - **params** (object) - Optional - Parameters associated with the event. - **creative_name** (string) - Optional - The name of the creative. - **creative_slot** (string) - Optional - The slot where the creative was displayed. - **promotion_id** (string) - Optional - The ID of the promotion. - **promotion_name** (string) - Optional - The name of the promotion. - **items** (array) - Optional - An array of item objects associated with the event. - **item_id** (string) - Optional - The ID of the item. - **item_name** (string) - Optional - The name of the item. - **affiliation** (string) - Optional - The affiliation of the item. - **coupon** (string) - Optional - The coupon code used for the item. - **currency** (string) - Optional - The currency of the item price. - **discount** (number) - Optional - The discount amount for the item. - **index** (number) - Optional - The index of the item in a list. - **item_brand** (string) - Optional - The brand of the item. - **item_category** (string) - Optional - The category of the item. - **item_category2** (string) - Optional - A secondary category of the item. - **item_category3** (string) - Optional - A tertiary category of the item. - **item_category4** (string) - Optional - A quaternary category of the item. - **item_category5** (string) - Optional - A quinary category of the item. - **item_list_id** (string) - Optional - The ID of the item list. - **item_list_name** (string) - Optional - The name of the item list. - **item_variant** (string) - Optional - The variant of the item. - **location_id** (string) - Optional - The location ID associated with the item. - **price** (number) - Optional - The price of the item. - **google_business_vertical** (string) - Optional - The Google Business Vertical for the item. - **quantity** (number) - Optional - The quantity of the item. ### Request Example ```json { "client_id": "client_id", "events": [ { "name": "view_promotion", "params": { "creative_name": "Summer Banner", "creative_slot": "featured_app_1", "promotion_id": "P_12345", "promotion_name": "Summer Sale", "items": [ { "item_id": "SKU_12345", "item_name": "Stan and Friends Tee", "affiliation": "Google Merchandise Store", "coupon": "SUMMER_FUN", "creative_name": "summer_banner2", "creative_slot": "featured_app_1", "currency": "USD", "discount": 2.22, "index": 0, "item_brand": "Google", "item_category": "Apparel", "item_category2": "Adult", "item_category3": "Shirts", "item_category4": "Crew", "item_category5": "Short sleeve", "item_list_id": "related_products", "item_list_name": "Related Products", "item_variant": "green", "location_id": "ChIJIQBpAG2ahYAR_6128GcTUEo", "price": 10.01, "promotion_id": "P_12345", "promotion_name": "Summer Sale", "google_business_vertical": "retail", "quantity": 3 } ] } } ] } ``` ### Response #### Success Response (200) This endpoint does not return a response body for successful requests. #### Response Example (No response body) ``` -------------------------------- ### HTTPS POST Request Example Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference All data must be sent securely using HTTPS POST requests to the specified endpoint. Replace PAYLOAD_DATA with the actual request payload. ```http POST /mp/collect HTTP/1.1 HOST: www.google-analytics.com Content-Type: application/json PAYLOAD_DATA ``` -------------------------------- ### Measurement Protocol JSON Payload Example Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events This JSON structure represents the data payload for a Measurement Protocol request. It includes the app_instance_id to identify a unique app installation and an events array containing event details like name and parameters. ```json { "app_instance_id": "APP_INSTANCE_ID", "events": [ { "name": "login", "params": { "method": "Google", "session_id": "SESSION_ID", "engagement_time_msec": 100 } } ] } ``` -------------------------------- ### Set Event-Level and User Property-Level Timestamps Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/sending-events This example shows how to specify individual timestamps for specific events and user properties within a request, alongside a request-level timestamp. This allows for precise control over when each data point is considered to have occurred. ```json { "timestamp_micros": requestUnixEpochTimeInMicros, "events": [ { "name": "tutorial_begin", "timestamp_micros": tutorialBeginUnixEpochTimeInMicros }, { "name": "join_group", "params": { "group_id": "G_12345" } } ], "user_properties": { "customer_tier": { "value": "PREMIUM", "timestamp_micros": customerTierUnixEpochTimeInMicros }, "newsletter_reader": { "value": "true" } } } ``` -------------------------------- ### Send a Valid Event for Validation Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/validating-events This example shows how to send a correctly formatted event to the Measurement Protocol validation server. A valid event, like this one with the `level_up` name and parameters, will result in an empty `validationMessages` array in the response. ```javascript const firebaseAppId = "FIREBASE_APP_ID"; const apiSecret = "API_SECRET"; fetch(`https://www.google-analytics.com/debug/mp/collect?firebase_app_id=${firebaseAppId}&api_secret=${apiSecret}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ app_instance_id: "APP_INSTANCE_ID", validation_behavior: "ENFORCE_RECOMMENDATIONS", events: [{ name: "level_up", params: { level: 2, character: "MyHero" }, }] }) }); ``` -------------------------------- ### Firebase SDK: Get App Instance ID Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/troubleshooting Retrieve the app_instance_id for Firebase on Android. This identifier is specific to an installation of a Firebase app. ```java getAppInstanceId() ``` -------------------------------- ### Firebase SDK: Get App Instance ID (Unity) Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/troubleshooting Retrieve the app_instance_id for Firebase using Unity. This identifier is specific to an installation of a Firebase app. ```csharp GetAnalyticsInstanceIdAsync() ``` -------------------------------- ### Firebase SDK: Get App Instance ID (C++) Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/troubleshooting Retrieve the app_instance_id for Firebase using C++. This identifier is specific to an installation of a Firebase app. ```cpp GetAnalyticsInstanceId() ``` -------------------------------- ### Send tutorial_begin Event Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events Use this JavaScript snippet to send the 'tutorial_begin' event. Ensure you replace placeholder values for measurement ID and API secret. ```javascript const measurementId = 'G-XXXXXXXXXX'; const apiSecret = ''; fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, { method: "POST", body: JSON.stringify({ "client_id": "client_id", "events": [{ "name": "tutorial_begin" }] }) }); ``` -------------------------------- ### Firebase SDK: Get App Instance ID (Objective-C) Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/troubleshooting Retrieve the app_instance_id for Firebase on iOS using Objective-C. This identifier is specific to an installation of a Firebase app. ```objectivec appInstanceID ``` -------------------------------- ### Send tutorial_complete Event Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This JavaScript code sends the 'tutorial_complete' event, useful for tracking onboarding funnel completion. Replace placeholders for measurement ID and API secret. ```javascript const measurementId = 'G-XXXXXXXXXX'; const apiSecret = ''; fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, { method: "POST", body: JSON.stringify({ "client_id": "client_id", "events": [{ "name": "tutorial_complete" }] }) }); ``` -------------------------------- ### Firebase SDK: Get App Instance ID (Swift) Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/troubleshooting Retrieve the app_instance_id for Firebase on iOS using Swift. This identifier is specific to an installation of a Firebase app. ```swift appInstanceID() ``` -------------------------------- ### Send Sign Up Event Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This snippet sends a 'sign_up' event, optionally including the sign-up method. ```javascript const measurementId = 'G-XXXXXXXXXX'; const apiSecret = ''; fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, { method: "POST", body: JSON.stringify({ "client_id": "client_id", "events": [{ "name": "sign_up", "params": { "method": "Google" } }] }) }); ``` -------------------------------- ### sign_up Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This event indicates that a user has signed up for an account. It helps understand the behaviors of logged-in vs. logged-out users. ```APIDOC ## POST /mp/collect ### Description This event indicates that a user has signed up for an account. Use this event to understand the different behaviors of logged in and logged out users. ### Method POST ### Endpoint https://www.google-analytics.com/mp/collect?measurement_id=G-XXXXXXXXXX&api_secret= ### Parameters #### Request Body - **client_id** (string) - Required - The unique identifier for the client. - **events** (array) - Required - An array of event objects. - **name** (string) - Required - The name of the event, should be "sign_up". - **params** (object) - Required - Parameters for the event. - **method** (string) - Optional - The method used for sign up. ### Request Example ```json { "client_id": "client_id", "events": [{ "name": "sign_up", "params": { "method": "Google" } }] } ``` ### Response #### Success Response (200) (No specific response schema provided in source) #### Response Example (No specific response example provided in source) ``` -------------------------------- ### screen_view Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This event is available only for App streams. Use this event to signify a screen transition has occurred. ```APIDOC ## POST /mp/collect ### Description This event is available only for App streams. Use this event to signify a screen transition has occurred. ### Method POST ### Endpoint /mp/collect ### Parameters #### Query Parameters - **measurement_id** (string) - Required - Your Measurement ID. - **api_secret** (string) - Required - Your API Secret. #### Request Body - **app_instance_id** (string) - Required - Unique identifier for an app instance. - **events** (array) - Required - An array of event objects. - **name** (string) - Required - The name of the event. Should be `screen_view`. - **params** (object) - Optional - Parameters for the event. - **screen_class** (string) - Optional - The class of the screen. - **screen_name** (string) - Optional - The name of the screen. ### Request Example ```json { "app_instance_id": "app_instance_id", "events": [ { "name": "screen_view", "params": { "screen_class": "MainActivity", "screen_name": "About" } } ] } ``` ### Response #### Success Response (204) No content is returned on success. #### Error Response (4xx/5xx) See [API reference](https://developers.google.com/analytics/devguides/collection/protocol/ga4/exceptions) for details on error responses. ``` -------------------------------- ### Send Event: begin_checkout Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This snippet demonstrates how to send a 'begin_checkout' event. This event is typically used to track when a user initiates the checkout process, often including details about the items in their cart. ```APIDOC ## POST /mp/collect ### Description This endpoint is used to send events to Google Analytics 4. ### Method POST ### Endpoint `https://www.google-analytics.com/mp/collect?measurement_id=&api_secret=` ### Parameters #### Request Body - **client_id** (string) - Required - The ID for the client. - **events** (array) - Required - A list of events to send. - **name** (string) - Required - The name of the event (e.g., `begin_checkout`). - **params** (object) - Required - Parameters associated with the event. - **currency** (string) - Optional - Currency of the `value` of the event, in 3-letter ISO 4217 format. - **value** (number) - Optional - The monetary value of the event. - **coupon** (string) - Optional - The coupon code used for the purchase. - **items** (array) - Optional - A list of items associated with the event. - **item_id** (string) - Optional - The ID of the item. - **item_name** (string) - Optional - The name of the item. - **affiliation** (string) - Optional - The affiliation of the item. - **currency** (string) - Optional - Currency of the `price` of the item, in 3-letter ISO 4217 format. - **discount** (number) - Optional - The discount amount for the item. - **index** (number) - Optional - The index of the item in a list. - **item_brand** (string) - Optional - The brand of the item. - **item_category** (string) - Optional - The category of the item. - **item_category2** (string) - Optional - A secondary category for the item. - **item_category3** (string) - Optional - A tertiary category for the item. - **item_category4** (string) - Optional - A quaternary category for the item. - **item_category5** (string) - Optional - A quinary category for the item. - **item_list_id** (string) - Optional - The ID of the list the item belongs to. - **item_list_name** (string) - Optional - The name of the list the item belongs to. - **item_variant** (string) - Optional - The variant of the item. - **location_id** (string) - Optional - The location ID associated with the item. - **price** (number) - Optional - The price of the item. - **google_business_vertical** (string) - Optional - The business vertical of the item. - **quantity** (number) - Optional - The quantity of the item. ### Request Example { "client_id": "client_id", "events": [ { "name": "begin_checkout", "params": { "currency": "USD", "value": 30.03, "coupon": "SUMMER_FUN", "items": [ { "item_id": "SKU_12345", "item_name": "Stan and Friends Tee", "affiliation": "Google Merchandise Store", "coupon": "SUMMER_FUN", "currency": "USD", "discount": 2.22, "index": 0, "item_brand": "Google", "item_category": "Apparel", "item_category2": "Adult", "item_category3": "Shirts", "item_category4": "Crew", "item_category5": "Short sleeve", "item_list_id": "related_products", "item_list_name": "Related Products", "item_variant": "green", "location_id": "ChIJIQBpAG2ahYAR_6128GcTUEo", "price": 10.01, "google_business_vertical": "retail", "quantity": 3 } ] } } ] } ### Response #### Success Response (200) This endpoint does not return a response body on success. #### Response Example (No response body) ``` -------------------------------- ### generate_lead Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This event measures when a lead has been generated, for example, through a form. It helps understand marketing campaign effectiveness and customer re-engagement. Supports currency, value, and lead source. ```APIDOC ## POST /mp/collect ### Description This event measures when a lead has been generated, for example, through a form. It helps understand marketing campaign effectiveness and customer re-engagement. Supports currency, value, and lead source. ### Method POST ### Endpoint `https://www.google-analytics.com/mp/collect?measurement_id=G-XXXXXXXXXX&api_secret=` ### Parameters #### Request Body - **client_id** (string) - Required - The client ID for the user. - **events** (array) - Required - An array of events to send. - **name** (string) - Required - The name of the event. For this endpoint, it is `generate_lead`. - **params** (object) - Optional - Parameters for the event. - **currency** (string) - Required* - Currency of the `value` of the event, in 3-letter ISO 4217 format. Required if `value` is set. - **value** (number) - Required* - The monetary value of the event. `currency` is required if `value` is set. - **lead_source** (string) - Optional - The source of the lead. ### Request Example ```json { "client_id": "client_id", "events": [ { "name": "generate_lead", "params": { "currency": "USD", "value": 30.03, "lead_source": "Trade show" } } ] } ``` ### Response #### Success Response (200) An empty response body indicates success. ``` -------------------------------- ### Send Event Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This snippet demonstrates how to send a POST request to the Google Analytics Measurement Protocol to log an event. It includes an example of the 'add_to_cart' event with item details. ```APIDOC ## POST /mp/collect ### Description Sends event data to Google Analytics for GA4 via the Measurement Protocol. ### Method POST ### Endpoint `https://www.google-analytics.com/mp/collect?measurement_id=&api_secret=` ### Parameters #### Query Parameters - **measurement_id** (string) - Required - Your Google Analytics Measurement ID. - **api_secret** (string) - Required - Your Google Analytics API secret. #### Request Body - **client_id** (string) - Required - A unique identifier for the client. - **events** (Array) - Required - An array of event objects to be sent. - **name** (string) - Required - The name of the event (e.g., "add_to_cart"). - **params** (object) - Optional - Parameters associated with the event. - **currency** (string) - Optional - The currency of the event value in 3-letter ISO 4217 format. Required if `value` is set. - **value** (number) - Optional - The monetary value of the event. Required for meaningful reporting and if marked as a key event. `currency` is required if `value` is set. - **items** (Array) - Optional - The items associated with the event. ### Request Example ```json { "client_id": "client_id", "events": [ { "name": "add_to_cart", "params": { "currency": "USD", "value": 30.03, "items": [ { "item_id": "SKU_12345", "item_name": "Stan and Friends Tee", "affiliation": "Google Merchandise Store", "coupon": "SUMMER_FUN", "currency": "USD", "discount": 2.22, "index": 0, "item_brand": "Google", "item_category": "Apparel", "item_category2": "Adult", "item_category3": "Shirts", "item_category4": "Crew", "item_category5": "Short sleeve", "item_list_id": "related_products", "item_list_name": "Related Products", "item_variant": "green", "location_id": "ChIJIQBpAG2ahYAR_6128GcTUEo", "price": 10.01, "google_business_vertical": "retail", "quantity": 3 } ] } } ] } ``` ### Response #### Success Response (200) An empty response body indicates success. ### `add_to_wishlist` #### Description The event signifies that an item was added to a wishlist. Use this event to identify popular gift items in your app. #### Parameters ##### Event Parameters - **currency** (string) - Required* - USD - Currency of the items associated with the event, in 3-letter ISO 4217 format. * Value metrics on the `view_item` event to not contribute to revenue * If you set `value` then `currency` is required for revenue metrics to be computed accurately. - **value** (number) - Required* - 30.03 - The monetary value of the event. * Set `value` to the sum of `(price * quantity)` for all items in `items`. Don't include `shipping` or `tax`. * `value` is typically required for meaningful reporting. If you mark the event as a key event then it's recommended you set `value`. * `currency` is required if you set `value`. - **items** (Array) - Required - The items for the event. ``` -------------------------------- ### Send 'qualify_lead' Event Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This code sends a 'qualify_lead' event, indicating a user meets lead criteria. It requires currency and value parameters for accurate revenue reporting. ```javascript const measurementId = 'G-XXXXXXXXXX'; const apiSecret = ''; fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, { method: "POST", body: JSON.stringify({ "client_id": "client_id", "events": [{ "name": "qualify_lead", "params": { "currency": "USD", "value": 30.03 } }] }) }); ``` -------------------------------- ### Send view_promotion Event Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This snippet demonstrates how to send a 'view_promotion' event, which is useful for tracking user interactions with promotions. It includes detailed item parameters for comprehensive e-commerce tracking. ```javascript const measurementId = 'G-XXXXXXXXXX'; const apiSecret = ''; fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, { method: "POST", body: JSON.stringify({ "client_id": "client_id", "events": [{ "name": "view_promotion", "params": { "creative_name": "Summer Banner", "creative_slot": "featured_app_1", "promotion_id": "P_12345", "promotion_name": "Summer Sale", "items": [ { "item_id": "SKU_12345", "item_name": "Stan and Friends Tee", "affiliation": "Google Merchandise Store", "coupon": "SUMMER_FUN", "creative_name": "summer_banner2", "creative_slot": "featured_app_1", "currency": "USD", "discount": 2.22, "index": 0, "item_brand": "Google", "item_category": "Apparel", "item_category2": "Adult", "item_category3": "Shirts", "item_category4": "Crew", "item_category5": "Short sleeve", "item_list_id": "related_products", "item_list_name": "Related Products", "item_variant": "green", "location_id": "ChIJIQBpAG2ahYAR_6128GcTUEo", "price": 10.01, "promotion_id": "P_12345", "promotion_name": "Summer Sale", "google_business_vertical": "retail", "quantity": 3 } ] } }] }) }); ``` -------------------------------- ### Send an Invalid Event for Validation Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/validating-events This example demonstrates sending an event with an invalid name to the Measurement Protocol validation server. The `validation_behavior` parameter is set to `ENFORCE_RECOMMENDATIONS` to trigger detailed validation messages. ```javascript const firebaseAppId = "FIREBASE_APP_ID"; const apiSecret = "API_SECRET"; fetch(`https://www.google-analytics.com/debug/mp/collect?firebase_app_id=${firebaseAppId}&api_secret=${apiSecret}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ app_instance_id: "APP_INSTANCE_ID", validation_behavior: "ENFORCE_RECOMMENDATIONS", events: [{ // Event names must start with an alphabetic character. name: "_badEventName", params: {}, }] }) }); ``` -------------------------------- ### Send Event Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This code snippet demonstrates how to send an event to Google Analytics 4 using the Measurement Protocol. It includes an example of the `remove_from_cart` event, showcasing the structure of the request body and the `items` array. ```APIDOC ## POST /mp/collect ### Description Sends events to Google Analytics 4 using the Measurement Protocol. This endpoint allows for real-time data collection from your applications. ### Method POST ### Endpoint `https://www.google-analytics.com/mp/collect?measurement_id=&api_secret=` ### Parameters #### Query Parameters - **measurement_id** (string) - Required - Your GA4 Measurement ID. - **api_secret** (string) - Required - Your GA4 API Secret. #### Request Body - **client_id** (string) - Required - A unique identifier for the client (e.g., user or device). - **events** (Array) - Required - An array of event objects to be sent. - **name** (string) - Required - The name of the event. - **params** (Object) - Optional - Parameters associated with the event. - **currency** (string) - Optional - The currency of the event's value (e.g., 'USD'). - **value** (number) - Optional - The value associated with the event. - **items** (Array) - Optional - An array of item objects associated with the event. ### Request Example ```json { "client_id": "client_id", "events": [ { "name": "remove_from_cart", "params": { "currency": "USD", "value": 30.03, "items": [ { "item_id": "SKU_12345", "item_name": "Stan and Friends Tee", "affiliation": "Google Merchandise Store", "coupon": "SUMMER_FUN", "currency": "USD", "discount": 2.22, "index": 0, "item_brand": "Google", "item_category": "Apparel", "item_category2": "Adult", "item_category3": "Shirts", "item_category4": "Crew", "item_category5": "Short sleeve", "item_list_id": "related_products", "item_list_name": "Related Products", "item_variant": "green", "location_id": "ChIJIQBpAG2ahYAR_6128GcTUEo", "price": 10.01, "google_business_vertical": "retail", "quantity": 3 } ] } } ] } ``` ### Response #### Success Response (204 No Content) Indicates that the request was successfully received and processed. No response body is returned. #### Error Response (4xx/5xx) Details of the error will be provided in the response body. ``` -------------------------------- ### Override Timestamp for a Specific User Property Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/user-properties This example demonstrates how to set a specific timestamp for the 'customer_tier' user property while using the request-level timestamp for others like 'customer_group'. This ensures 'customer_tier' uses 'customerTierUnixEpochTimeInMicros'. ```json "timestamp_micros": requestUnixEpochTimeInMicros, "user_properties": { "customer_tier": { "value": customerTierValue, "timestamp_micros": customerTierUnixEpochTimeInMicros }, "customer_group": { "value": customerGroupValue } } ``` -------------------------------- ### view_promotion Event Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events This event signifies a promotion was viewed from a list. ```APIDOC ## view_promotion Event ### Description This event signifies a promotion was viewed from a list. ``` -------------------------------- ### search Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events Logs this event to indicate when the user has performed a search. You can use this event to identify what users are searching for on your website or app. For example, you could send this event when a user views a search results page after performing a search. ```APIDOC ## POST /mp/collect ### Description Logs this event to indicate when the user has performed a search. You can use this event to identify what users are searching for on your website or app. For example, you could send this event when a user views a search results page after performing a search. ### Method POST ### Endpoint /mp/collect ### Parameters #### Query Parameters - **measurement_id** (string) - Required - Your Measurement ID. - **api_secret** (string) - Required - Your API Secret. #### Request Body - **client_id** (string) - Required - Unique identifier for a client. - **events** (array) - Required - An array of event objects. - **name** (string) - Required - The name of the event. Should be `search`. - **params** (object) - Optional - Parameters for the event. - **search_term** (string) - Required - The term that was searched for. ### Request Example ```json { "client_id": "client_id", "events": [ { "name": "search", "params": { "search_term": "example search term" } } ] } ``` ### Response #### Success Response (204) No content is returned on success. #### Error Response (4xx/5xx) See [API reference](https://developers.google.com/analytics/devguides/collection/protocol/ga4/exceptions) for details on error responses. ``` -------------------------------- ### Send begin_checkout Event Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events Use this snippet to send a 'begin_checkout' event. Ensure you replace placeholder values for measurement ID and API secret. ```javascript const measurementId = 'G-XXXXXXXXXX'; const apiSecret = ''; fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, { method: "POST", body: JSON.stringify({ "client_id": "client_id", "events": [{ "name": "begin_checkout", "params": { "currency": "USD", "value": 30.03, "coupon": "SUMMER_FUN", "items": [ { "item_id": "SKU_12345", "item_name": "Stan and Friends Tee", "affiliation": "Google Merchandise Store", "coupon": "SUMMER_FUN", "currency": "USD", "discount": 2.22, "index": 0, "item_brand": "Google", "item_category": "Apparel", "item_category2": "Adult", "item_category3": "Shirts", "item_category4": "Crew", "item_category5": "Short sleeve", "item_list_id": "related_products", "item_list_name": "Related Products", "item_variant": "green", "location_id": "ChIJIQBpAG2ahYAR_6128GcTUEo", "price": 10.01, "google_business_vertical": "retail", "quantity": 3 } ] } }] }) }); ``` -------------------------------- ### Send screen_view Event Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events Log a screen view event, available only for App streams, to signify a screen transition. You can specify the screen class and name. ```javascript const measurementId = 'G-XXXXXXXXXX'; const apiSecret = ''; fetch(`https://www.google-analytics.com/mp/collect?measurement_id=${measurementId}&api_secret=${apiSecret}`, { method: "POST", body: JSON.stringify({ "app_instance_id": "app_instance_id", "events": [{ "name": "screen_view", "params": { "screen_class": "MainActivity", "screen_name": "About" } }] }) }); ``` -------------------------------- ### login Source: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events Sends this event to signify that a user has logged in to your website or app. ```APIDOC ## POST /mp/collect ### Description Sends this event to signify that a user has logged in to your website or app. ### Method POST ### Endpoint /mp/collect ### Parameters #### Query Parameters - **measurement_id** (string) - Required - Your Measurement ID. - **api_secret** (string) - Required - Your API Secret. #### Request Body - **client_id** (string) - Required - Unique identifier for a client. - **events** (array) - Required - An array of event objects. - **name** (string) - Required - The name of the event. Should be `login`. - **params** (object) - Optional - Parameters for the event. - **method** (string) - Optional - The method used to login. ### Request Example ```json { "client_id": "client_id", "events": [ { "name": "login", "params": { "method": "Google" } } ] } ``` ### Response #### Success Response (204) No content is returned on success. #### Error Response (4xx/5xx) See [API reference](https://developers.google.com/analytics/devguides/collection/protocol/ga4/exceptions) for details on error responses. ```