diff --git a/snippets/convertVtexIoOrderPlacedItemsToGA4Items.md b/snippets/convertVtexIoOrderPlacedItemsToGA4Items.md new file mode 100644 index 0000000..c11b581 --- /dev/null +++ b/snippets/convertVtexIoOrderPlacedItemsToGA4Items.md @@ -0,0 +1,38 @@ +Convert a list of products from the Vtex `orderPlaced` event into the format recommended for Google Analytics 4 (GA4) using a custom JavaScript function in Google Tag Manager. In Vtex, the `orderPlaced` event is utilized instead of `purchase`. The provided code snippet facilitates the conversion of the `orderPlaced` event model to the `purchase` model recommended by GA4. It processes the products array within the ecommerce object, which is part of the `orderPlaced` event in Vtex. The function maps each product to a new format that aligns with GA4's e-commerce data model requirements, ensuring compatibility with GA4's enhanced e-commerce tracking features. + +> You must create the variable `{{ecommerceV2}}` in Google Tag Manager to capture the object containing transaction data in the `orderPlaced` event. + +- Return `items` list with a recomended GA4 pattern. + +* Remember to check if the event parameters match the structure we prepared. Parameters may change over time. + +```js +function () { + return {{ecommerceV2}}.ecommerce.purchase.products.map(function(item){ + return { + 'item_id': item.id.toString(), + 'item_name': item.name, + 'currency': item.currency || 'BRL', + 'item_brand': item.brand, + 'item_category': item.item_category || item.category, + 'price': parseFloat(item.price), + 'quantity': parseInt(item.quantity) || 1 + } + }) +} +``` + +```js +// output: +[ + { + "item_id": "101", + "item_name": "Sneakers", + "currency": "USD", + "item_brand": "BrandA", + "item_category": "Footwear", + "price": 59.99, + "quantity": 1 + } +] +``` \ No newline at end of file diff --git a/snippets/convertWooCommerceItemsToGa4Items.md b/snippets/convertWooCommerceItemsToGa4Items.md new file mode 100644 index 0000000..8e6c9dd --- /dev/null +++ b/snippets/convertWooCommerceItemsToGa4Items.md @@ -0,0 +1,44 @@ +Convert a list of items from a `WooCommerce` event into the recommended format for `Google Analytics 4 (GA4)` using a custom JavaScript function in `Google Tag Manager`. The provided code snippet processes the `items` array, which originates from an `ecommerce` event, mapping each item to a new format that aligns with `GA4's e-commerce data model` requirements. This transformation ensures compatibility with `GA4's enhanced e-commerce` tracking features. + +> You have must be created `{{ecommerce}}` dataLayer variable on Google Tag Manager. + +- Return `items` list with a recomended GA4 pattern. + +* Remember to check if the event parameters match the structure we prepared. Parameters may change due to WooCommerce updates. + +```js +function() { + return {{ecommerce}}.items.map(function(item) { + return { + item_id: item.item_id.toString(), + price: parseFloat(item.price), + quantity: parseInt(item.quantity) || 1, + item_name: item.item_name, + item_variant: item.item_variant || item.variant, + item_category: item.item_category + }; + }); +} +``` + +```js +// output: +[ + { + "item_id": "001", + "price": "19.99", + "quantity": "2", + "item_name": "Graphic Tee", + "item_variant": "Medium", + "item_category": "T-shirts" + }, + { + "item_id": "002", + "price": "29.99", + "quantity": "1", + "item_name": "Hoodie", + "item_variant": "Large", + "item_category": "Outerwear" + } +] +``` \ No newline at end of file