### Example Ecommerce Event Data Source: https://github.com/googleanalytics/ecommerce-migration-helper/blob/main/index.html An example of ecommerce event data for a 'purchase' event, including transaction details and items. This format is used for analysis. ```json { "transaction_id": "123abc", "value": 3.99, "items": [ { "item_id": "item_1", "item_name": "Item 1", "item_category": "Numbered Items", "item_variant": "1", "value": 1.99, "quantity": 1 }, { "item_id": "item_2", "item_name": "Item 2", "item_category": "Numbered Items", "item_variant": "2", "value": 2.00, "quantity": 1 } ] } ``` -------------------------------- ### gtag.js API Example for GA4 Purchase Source: https://github.com/googleanalytics/ecommerce-migration-helper/blob/main/index.html This is a default example for a GA4 purchase ecommerce event sent using the gtag.js API. Provide the event name and parameters separately. ```javascript gtag('event', 'purchase', { 'transaction_id': 'T_12345', 'affiliation': 'Google Merchandise Store', 'value': 25.42, 'tax': 4.90, 'shipping': 5.99, 'currency': 'USD', 'items': [ { 'item_id': 'SKU_12345', 'item_name': 'Stan and Friends Tee', 'affiliation': 'Google Merchandise Store', 'coupon': 'SUMMER_FUN', 'currency': 'USD', '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_PL-W_k-Y-I', 'price': 9.99, 'quantity': 1 } ] }); ``` -------------------------------- ### Data Layer Push Example for GA4 Purchase Source: https://github.com/googleanalytics/ecommerce-migration-helper/blob/main/index.html This is a default example for a GA4 purchase ecommerce event sent using the dataLayer.push API. Ensure your event object includes an 'event' key. ```javascript { 'event': 'purchase', 'ecommerce': { 'transaction_id': 'T_12345', 'affiliation': 'Google Merchandise Store', 'value': 25.42, 'tax': 4.90, 'shipping': 5.99, 'currency': 'USD', 'items': [ { 'item_id': 'SKU_12345', 'item_name': 'Stan and Friends Tee', 'affiliation': 'Google Merchandise Store', 'coupon': 'SUMMER_FUN', 'currency': 'USD', '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_PL-W_k-Y-I', 'price': 9.99, 'quantity': 1 } ] } } ``` -------------------------------- ### gtag.js Ecommerce Event Source: https://github.com/googleanalytics/ecommerce-migration-helper/blob/main/index.html Example of how to send an ecommerce 'purchase' event using gtag.js. This is used when not relying on the Data Layer for ecommerce tracking. ```javascript gtag('event', 'purchase', { "transaction_id": "123abc", "value": 3.99, "items": [ { "item_id": "item_1", "item_name": "Item 1", "item_category": "Numbered Items", "item_variant": "1", "value": 1.99, "quantity": 1 }, { "item_id": "item_2", "item_name": "Item 2", "item_category": "Numbered Items", "item_variant": "2", "value": 2.00, "quantity": 1 } ] }); ``` -------------------------------- ### Example Data Layer Update for Purchase Event Source: https://github.com/googleanalytics/ecommerce-migration-helper/blob/main/index.html This shows how a 'purchase' event and its associated ecommerce data are structured within the Data Layer. This is commonly used with Google Tag Manager. ```json { "event": "purchase", "ecommerce": { "purchase": { "actionField": { "id": "123abc", "revenue": 3.99 }, "products": [ { "id": "item_1", "name": "Item 1", "category": "Numbered Items", "variant": "1", "price": 1.99, "quantity": 1 }, { "id": "item_2", "name": "Item 2", "category": "Numbered Items", "variant": "2", "price": 2.00, "quantity": 1 } ] } } } ``` -------------------------------- ### Data Layer Push for Ecommerce Event Source: https://github.com/googleanalytics/ecommerce-migration-helper/blob/main/index.html Example of pushing an ecommerce event to the Data Layer using `dataLayer.push()`. This is typically used with Google Tag Manager. ```javascript dataLayer.push({ "event": "purchase", "ecommerce": { "purchase": { "actionField": { "id": "123abc", "revenue": 3.99 }, "products": [ { "id": "item_1", "name": "Item 1", "category": "Numbered Items", "variant": "1", "price": 1.99, "quantity": 1 }, { "id": "item_2", "name": "Item 2", "category": "Numbered Items", "variant": "2", "price": 2.00, "quantity": 1 } ] } } }); ``` -------------------------------- ### Custom Event Name Conversion Example Source: https://github.com/googleanalytics/ecommerce-migration-helper/blob/main/index.html Illustrates converting a custom event name like 'productClick' to the recommended GA4 event name 'select_item' for compatibility. ```javascript { 'event': 'select_item', 'ecommerce': {} } ``` -------------------------------- ### Fire Test Events and Observe Network Hits Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt This script demonstrates bootstrapping the full tool, including firing test events for both gtag and dataLayer APIs, and setting up a PerformanceObserver to intercept and parse outgoing GA4/UA network hits. It also shows how to identify the schema of an event payload before sending. ```javascript // Bootstrapping the full tool (simplified equivalent of main.js main()) import ga4 from "./ga4.js"; import measurementProtocol from "./measurement_protocol.js"; import schemaId from "./schema_id.js"; import schemaRecommend from "./schema_recommend.js"; // 1. Fire a test gtag event (gtag API mode) function gtag() { dataLayer.push(arguments); } gtag("event", "add_to_cart", { currency: "USD", value: 19.99, items: [{ item_id: "SKU-007", item_name: "Notebook", price: 19.99, quantity: 1 }] }); // 2. Fire a test dataLayer push (dataLayer mode) dataLayer.push({ ecommerce: null }); // clear previous ecommerce context dataLayer.push({ event: "purchase", ecommerce: { transaction_id: "TXN-001", value: 19.99, currency: "USD", items: [{ item_id: "SKU-007", item_name: "Notebook", price: 19.99, quantity: 1 }] } }); // 3. Observe outbound hits and route to the correct parser const observer = new PerformanceObserver((list) => { list.getEntries().forEach((entry) => { const url = entry.name; if (ga4.isGa4Hit(url)) { const parsed = ga4.parse(url); // Display parsed data and generate migration recommendation console.log("GA4 parsed:", parsed); console.log("Recommended command:", schemaRecommend.buildGa4GtagCommand(parsed)); } if (measurementProtocol.isUaLegacyHit(url)) { console.log("UA Legacy parsed:", measurementProtocol.parse(url)); } if (measurementProtocol.isUaGa4Hit(url)) { console.log("UA+GA4 schema parsed:", measurementProtocol.parse(url)); } }); }); observer.observe({ entryTypes: ["resource"] }); // 4. Identify the schema of an arbitrary event payload before sending const pendingParams = { currency: "USD", items: [{ id: "SKU-007", name: "Notebook" }] // UA-style keys }; const schema = schemaId.identifySchema(schemaId.Api.GTAG, pendingParams); console.log("Detected schema:", schema); // "ua-gtag" — needs migration to GA4 keys ``` -------------------------------- ### schemaRecommend.buildGa4GtagCommand(parsedHitData) Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt Generates a recommended GA4 `gtag` command string from a parsed hit data object. This function is useful for migrating existing ecommerce tracking to the GA4 format. ```APIDOC ## `schemaRecommend.buildGa4GtagCommand(parsedHitData)` — Generate a recommended GA4 gtag command Takes a parsed hit data object (as returned by `ga4.parse()`) and returns a formatted `gtag('event', ...)` string following current GA4 documentation. This output is surfaced directly in the tool's UI as the recommended migration target code. ```js import ga4 from "./ga4.js"; import schemaRecommend from "./schema_recommend.js"; // Simulate a parsed GA4 hit (e.g., from ga4.parse()) const parsedHit = { event: "purchase", params: { currency: "USD", value: "59.98", transaction_id: "T-12345" }, products: [ undefined, // index 0 unused by GA4 encoding { item_id: "SKU-001", item_name: "Widget", price: "29.99", quantity: "2" } ] }; const command = schemaRecommend.buildGa4GtagCommand(parsedHit); console.log(command); // gtag('event', 'purchase', { // 'currency': 'USD', // 'value': '59.98', // 'transaction_id': 'T-12345', // 'items': [ // { // 'item_id: 'SKU-001', // 'item_name: 'Widget', // 'price: '29.99', // 'quantity: '2', // }, // ], // }); // --- Full pipeline: intercept a live GA4 hit and generate recommendation --- const observer = new PerformanceObserver((list) => { list.getEntries().forEach((entry) => { if (ga4.isGa4Hit(entry.name)) { const parsed = ga4.parse(entry.name); const recommended = schemaRecommend.buildGa4GtagCommand(parsed); console.log("Recommended GA4 command:\n", recommended); } }); }); observer.observe({ entryTypes: ["resource"] }); ``` ``` -------------------------------- ### Parse Ecommerce Data from GA4 Hit URL with `ga4.parse()` Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt Extracts structured product data, event parameters, and event name from a GA4 Measurement Protocol URL. Products are decoded from a compact encoding, and event parameters are extracted from prefixed query parameters. ```javascript import ga4 from "./ga4.js"; const ga4HitUrl = "https://www.google-analytics.com/g/collect?v=2" + "&en=purchase" + "&epn.value=29.99" + "&epn.tax=2.50" + "&ep.currency=USD" + "&pr1=id123~nmWidget Pro~pr19.99~qt1~br1Acme~ca1Tools"; const result = ga4.parse(ga4HitUrl); console.log(result); // { // event: "purchase", // products: [ // undefined, // index 0 unused // { // item_id: "123", // item_name: "Widget Pro", // price: "19.99", // quantity: "1", // item_brand: "Acme", // item_cateogry: "Tools" // } // ], // params: { // value: "29.99", // tax: "2.50", // currency: "USD" // } // } ``` -------------------------------- ### measurementProtocol.parse(url) Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt Parses a Universal Analytics Measurement Protocol URL to extract ecommerce data such as products, impressions, promotions, and transaction parameters. It maps the two-character field codes to human-readable GA4-aligned field names. ```APIDOC ## `measurementProtocol.parse(url)` — Parse ecommerce data from a UA Measurement Protocol hit Extracts product fields, impression lists, promotions, and action-level parameters from a Universal Analytics Measurement Protocol URL, mapping all two-character field codes (e.g., `id`, `nm`, `pr`, `qt`) to human-readable GA4-aligned field names. ```js import measurementProtocol from "./measurement_protocol.js"; const uaUrl = "https://www.google-analytics.com/collect?v=1&t=event&el=GTM" + "&pr1id=SKU-001&pr1nm=Running+Shoe&pr1pr=89.99&pr1qt=2&pr1br=Nike&pr1ca=Footwear" + "&il1nm=Search+Results&il1pi1id=SKU-002&il1pi1nm=Trail+Boot&il1pi1pr=129.99" + "&promo1id=PROMO-10&promo1nm=Summer+Sale&promo1cr=banner_top" + "&pa=purchase&ti=TXN-42&tr=179.98&tt=14.40&ts=5.99&cu=USD"; const result = measurementProtocol.parse(uaUrl); console.log(result); // { // products: [ // undefined, // { item_id: "SKU-001", item_name: "Running+Shoe", price: "89.99", // quantity: "2", item_brand: "Nike", item_category: "Footwear" } // ], // impressions: [ // undefined, // { name: "Search+Results", impressions: [ // undefined, // { item_id: "SKU-002", item_name: "Trail+Boot", price: "129.99" } // ] // } // ], // promos: [ // undefined, // { item_id: "PROMO-10", item_name: "Summer+Sale", creative_name: "banner_top" } // ], // params: { // product_action: "purchase", // transaction_id: "TXN-42", // value: "179.98", // tax: "14.40", // shipping: "5.99", // currency: "USD" // } // } ``` ``` -------------------------------- ### `ga4.parse(url)` — Parse ecommerce data from a GA4 hit URL Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt Extracts structured product data, event parameters, and event name from a GA4 Measurement Protocol URL. Products are decoded from the compact `pr=~` encoding; event parameters are extracted from `ep.` and `epn.` prefixed query params. ```APIDOC ## `ga4.parse(url)` — Parse ecommerce data from a GA4 hit URL ### Description Extracts structured product data, event parameters, and event name from a GA4 Measurement Protocol URL. Products are decoded from the compact `pr=~` encoding; event parameters are extracted from `ep.` and `epn.` prefixed query params. ### Method ```javascript ga4.parse(url: string): object ``` ### Parameters #### Path Parameters - **url** (string) - Required - The GA4 Measurement Protocol URL to parse. ### Response #### Success Response - **event** (string) - The name of the event. - **products** (array) - An array of product objects, where each object contains details like `item_id`, `item_name`, `price`, `quantity`, etc. - **params** (object) - An object containing additional event parameters like `value`, `tax`, `currency`. ### Request Example ```javascript import ga4 from "./ga4.js"; const ga4HitUrl = "https://www.google-analytics.com/g/collect?v=2" + "&en=purchase" + "&epn.value=29.99" + "&epn.tax=2.50" + "&ep.currency=USD" + "&pr1=id123~nmWidget Pro~pr19.99~qt1~br1Acme~ca1Tools"; const result = ga4.parse(ga4HitUrl); console.log(result); // { // event: "purchase", // products: [ // undefined, // index 0 unused // { // item_id: "123", // item_name: "Widget Pro", // price: "19.99", // quantity: "1", // item_brand: "Acme", // item_cateogry: "Tools" // } // ], // params: { // value: "29.99", // tax: "2.50", // currency: "USD" // } // } ``` ``` -------------------------------- ### Generate GA4 gtag Command from Parsed Hit Data Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt This function takes a parsed GA4 hit data object and generates a `gtag('event', ...)` command string that conforms to current GA4 documentation. This is useful for displaying recommended migration code. ```javascript import ga4 from "./ga4.js"; import schemaRecommend from "./schema_recommend.js"; // Simulate a parsed GA4 hit (e.g., from ga4.parse()) const parsedHit = { event: "purchase", params: { currency: "USD", value: "59.98", transaction_id: "T-12345" }, products: [ undefined, // index 0 unused by GA4 encoding { item_id: "SKU-001", item_name: "Widget", price: "29.99", quantity: "2" } ] }; const command = schemaRecommend.buildGa4GtagCommand(parsedHit); console.log(command); // gtag('event', 'purchase', { // 'currency': 'USD', // 'value': '59.98', // 'transaction_id': 'T-12345', // 'items': [ // { // 'item_id: 'SKU-001', // 'item_name: 'Widget', // 'price: '29.99', // 'quantity: '2', // }, // ], // }); // --- Full pipeline: intercept a live GA4 hit and generate recommendation --- const observer = new PerformanceObserver((list) => { list.getEntries().forEach((entry) => { if (ga4.isGa4Hit(entry.name)) { const parsed = ga4.parse(entry.name); const recommended = schemaRecommend.buildGa4GtagCommand(parsed); console.log("Recommended GA4 command:\n", recommended); } }); }); observer.observe({ entryTypes: ["resource"] }); ``` -------------------------------- ### Parse UA Measurement Protocol URL to GA4-aligned fields Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt Use this function to extract product, impression, promotion, and transaction data from a Universal Analytics Measurement Protocol URL. It maps UA's two-character field codes to human-readable GA4 field names. ```javascript import measurementProtocol from "./measurement_protocol.js"; const uaUrl = "https://www.google-analytics.com/collect?v=1&t=event&el=GTM" + "&pr1id=SKU-001&pr1nm=Running+Shoe&pr1pr=89.99&pr1qt=2&pr1br=Nike&pr1ca=Footwear" + "&il1nm=Search+Results&il1pi1id=SKU-002&il1pi1nm=Trail+Boot&il1pi1pr=129.99" + "&promo1id=PROMO-10&promo1nm=Summer+Sale&promo1cr=banner_top" + "&pa=purchase&ti=TXN-42&tr=179.98&tt=14.40&ts=5.99&cu=USD"; const result = measurementProtocol.parse(uaUrl); console.log(result); // { // products: [ // undefined, // { item_id: "SKU-001", item_name: "Running+Shoe", price: "89.99", // quantity: "2", item_brand: "Nike", item_category: "Footwear" } // ], // impressions: [ // undefined, // { name: "Search+Results", impressions: [ // undefined, // { item_id: "SKU-002", item_name: "Trail+Boot", price: "129.99" } // ] // } // ], // promos: [ // undefined, // { item_id: "PROMO-10", item_name: "Summer+Sale", creative_name: "banner_top" } // ], // params: { // product_action: "purchase", // transaction_id: "TXN-42", // value: "179.98", // tax: "14.40", // shipping: "5.99", // currency: "USD" // } // } ``` -------------------------------- ### Detect UA Legacy-Schema GTM Hit with `measurementProtocol.isUaLegacyHit()` Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt Identifies Universal Analytics hits sent by a GTM UA tag configured in legacy-only mode. This is determined by checking for the event label `el=GTM`. ```javascript import measurementProtocol from "./measurement_protocol.js"; const legacyUrl = "https://www.google-analytics.com/collect?v=1&t=event&el=GTM&pr1idSKU-001&pr1nmSneaker"; console.log(measurementProtocol.isUaLegacyHit(legacyUrl)); // true console.log(measurementProtocol.isUaLegacyHit( "https://www.google-analytics.com/collect?v=1&t=event&el=GA4" )); // false ``` -------------------------------- ### `measurementProtocol.isUaLegacyHit(url)` — Detect a UA legacy-schema GTM hit Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt Returns `true` if the URL is a Universal Analytics hit fired by the GTM UA tag configured in legacy-only mode (event label `el=GTM`). Used to differentiate legacy UA traffic from GA4-aware UA traffic. ```APIDOC ## `measurementProtocol.isUaLegacyHit(url)` — Detect a UA legacy-schema GTM hit ### Description Returns `true` if the URL is a Universal Analytics hit fired by the GTM UA tag configured in legacy-only mode (event label `el=GTM`). Used to differentiate legacy UA traffic from GA4-aware UA traffic. ### Method ```javascript measurementProtocol.isUaLegacyHit(url: string): boolean ``` ### Parameters #### Path Parameters - **url** (string) - Required - The URL to check for legacy UA GTM hit pattern. ### Request Example ```javascript import measurementProtocol from "./measurement_protocol.js"; const legacyUrl = "https://www.google-analytics.com/collect?v=1&t=event&el=GTM&pr1idSKU-001&pr1nmSneaker"; console.log(measurementProtocol.isUaLegacyHit(legacyUrl)); // true console.log(measurementProtocol.isUaLegacyHit( "https://www.google-analytics.com/collect?v=1&t=event&el=GA4" )); // false ``` ``` -------------------------------- ### `measurementProtocol.isUaGa4Hit(url)` — Detect a UA hit with GA4 schema support Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt Returns `true` if the URL is a Universal Analytics hit fired by the GTM UA tag with GA4 schema reading enabled (event label `el=GA4`). This distinguishes the tag configuration that can read GA4-style `dataLayer` pushes. ```APIDOC ## `measurementProtocol.isUaGa4Hit(url)` — Detect a UA hit with GA4 schema support ### Description Returns `true` if the URL is a Universal Analytics hit fired by the GTM UA tag with GA4 schema reading enabled (event label `el=GA4`). This distinguishes the tag configuration that can read GA4-style `dataLayer` pushes. ### Method ```javascript measurementProtocol.isUaGa4Hit(url: string): boolean ``` ### Parameters #### Path Parameters - **url** (string) - Required - The URL to check for UA hit with GA4 schema support. ### Request Example ```javascript import measurementProtocol from "./measurement_protocol.js"; const ga4SchemaUaUrl = "https://www.google-analytics.com/collect?v=1&t=event&el=GA4&pr1idSKU-001"; console.log(measurementProtocol.isUaGa4Hit(ga4SchemaUaUrl)); // true console.log(measurementProtocol.isUaGa4Hit( "https://www.google-analytics.com/collect?v=1&t=event&el=GTM" )); // false ``` ``` -------------------------------- ### Identify Ecommerce Schema from gtag or dataLayer Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt This function inspects a `gtag` event or `dataLayer` push object to determine which ecommerce schema is being used. It returns an enum value indicating GA4 unified, UA via gtag, UA via GTM, or GTM with GA4 schema. ```javascript import schemaId from "./schema_id.js"; // --- gtag API with GA4-style items (KnownSchema.UNIFIED = "ga4-gtag") --- const ga4GtagParams = { currency: "USD", value: 49.99, items: [{ item_id: "SKU-001", item_name: "Gadget", price: 49.99, quantity: 1 }] }; console.log(schemaId.identifySchema(schemaId.Api.GTAG, ga4GtagParams)); // "ga4-gtag" // --- gtag API with UA-style items (KnownSchema.GTAG_UA = "ua-gtag") --- const uaGtagParams = { currency: "USD", items: [{ id: "SKU-001", name: "Gadget", price: 49.99, quantity: 1 }] }; console.log(schemaId.identifySchema(schemaId.Api.GTAG, uaGtagParams)); // "ua-gtag" // --- dataLayer push with UA schema (KnownSchema.GTM_UA = "ua-gtm") --- const uaDataLayerPush = { event: "productClick", ecommerce: { click: { actionField: { list: "Search Results" }, products: [{ id: "SKU-001", name: "Gadget", position: 1 }] } } }; console.log(schemaId.identifySchema(schemaId.Api.DATA_LAYER, uaDataLayerPush)); // "ua-gtm" // --- dataLayer push with GA4 schema (KnownSchema.GTM_LEGACY_GA4 = "ga4-gtm") --- const ga4DataLayerPush = { event: "purchase", ecommerce: { transaction_id: "TXN-99", value: 99.99, items: [{ item_id: "SKU-002", item_name: "Widget", price: 99.99 }] } }; console.log(schemaId.identifySchema(schemaId.Api.DATA_LAYER, ga4DataLayerPush)); // "ga4-gtm" ``` -------------------------------- ### Detect UA Hit with GA4 Schema Support with `measurementProtocol.isUaGa4Hit()` Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt Determines if a Universal Analytics hit was sent by a GTM UA tag with GA4 schema reading enabled. This is indicated by the event label `el=GA4` and is crucial for identifying tags that can process GA4-style `dataLayer` pushes. ```javascript import measurementProtocol from "./measurement_protocol.js"; const ga4SchemaUaUrl = "https://www.google-analytics.com/collect?v=1&t=event&el=GA4&pr1idSKU-001"; console.log(measurementProtocol.isUaGa4Hit(ga4SchemaUaUrl)); // true console.log(measurementProtocol.isUaGa4Hit( "https://www.google-analytics.com/collect?v=1&t=event&el=GTM" )); // false ``` -------------------------------- ### Detect GA4 Network Hit with `ga4.isGa4Hit()` Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt Use this function to filter network requests and identify those that are GA4 data collection hits. It checks if the URL matches the GA4 collection path pattern. ```javascript import ga4 from "./ga4.js"; const url = "https://www.google-analytics.com/g/collect?v=2&en=purchase&pr1=id1234~nmWidget~pr9.99~qt1"; if (ga4.isGa4Hit(url)) { console.log("GA4 hit detected"); // Output: "GA4 hit detected" } // Non-GA4 URL returns false console.log(ga4.isGa4Hit("https://www.google-analytics.com/collect?v=1&t=event")); // false ``` -------------------------------- ### schemaId.identifySchema(api, paramsObject) Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt Identifies the ecommerce schema used in a `gtag` event or `dataLayer` push object. It returns a `KnownSchema` enum value indicating whether the schema is GA4 unified, UA via gtag, UA via GTM data layer, GTM with GA4 schema, or unknown. ```APIDOC ## `schemaId.identifySchema(api, paramsObject)` — Identify the ecommerce schema in use Inspects a `gtag` event params object or a `dataLayer` push object and returns a `KnownSchema` enum value identifying which ecommerce schema it follows: GA4 unified (`ga4-gtag`), UA via gtag (`ua-gtag`), UA via GTM data layer (`ua-gtm`), GTM with GA4 schema (`ga4-gtm`), or `unknown`/`gtag-unknown` when identification fails. ```js import schemaId from "./schema_id.js"; // --- gtag API with GA4-style items (KnownSchema.UNIFIED = "ga4-gtag") --- const ga4GtagParams = { currency: "USD", value: 49.99, items: [{ item_id: "SKU-001", item_name: "Gadget", price: 49.99, quantity: 1 }] }; console.log(schemaId.identifySchema(schemaId.Api.GTAG, ga4GtagParams)); // "ga4-gtag" // --- gtag API with UA-style items (KnownSchema.GTAG_UA = "ua-gtag") --- const uaGtagParams = { currency: "USD", items: [{ id: "SKU-001", name: "Gadget", price: 49.99, quantity: 1 }] }; console.log(schemaId.identifySchema(schemaId.Api.GTAG, uaGtagParams)); // "ua-gtag" // --- dataLayer push with UA schema (KnownSchema.GTM_UA = "ua-gtm") --- const uaDataLayerPush = { event: "productClick", ecommerce: { click: { actionField: { list: "Search Results" }, products: [{ id: "SKU-001", name: "Gadget", position: 1 }] } } }; console.log(schemaId.identifySchema(schemaId.Api.DATA_LAYER, uaDataLayerPush)); // "ua-gtm" // --- dataLayer push with GA4 schema (KnownSchema.GTM_LEGACY_GA4 = "ga4-gtm") --- const ga4DataLayerPush = { event: "purchase", ecommerce: { transaction_id: "TXN-99", value: 99.99, items: [{ item_id: "SKU-002", item_name: "Widget", price: 99.99 }] } }; console.log(schemaId.identifySchema(schemaId.Api.DATA_LAYER, ga4DataLayerPush)); // "ga4-gtm" ``` ``` -------------------------------- ### `ga4.isGa4Hit(url)` — Detect a GA4 network hit Source: https://context7.com/googleanalytics/ecommerce-migration-helper/llms.txt Returns `true` if the given URL corresponds to a GA4 data collection hit (matches the `/g/collect?` path pattern). Used to filter the stream of observed network requests down to only GA4 traffic. ```APIDOC ## `ga4.isGa4Hit(url)` — Detect a GA4 network hit ### Description Returns `true` if the given URL corresponds to a GA4 data collection hit (matches the `/g/collect?` path pattern). Used to filter the stream of observed network requests down to only GA4 traffic. ### Method ```javascript ga4.isGa4Hit(url: string): boolean ``` ### Parameters #### Path Parameters - **url** (string) - Required - The URL to check for GA4 hit pattern. ### Request Example ```javascript import ga4 from "./ga4.js"; const url = "https://www.google-analytics.com/g/collect?v=2&en=purchase&pr1=id1234~nmWidget~pr9.99~qt1"; if (ga4.isGa4Hit(url)) { console.log("GA4 hit detected"); } // Non-GA4 URL returns false console.log(ga4.isGa4Hit("https://www.google-analytics.com/collect?v=1&t=event")); // false ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.