Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New snippets. #14

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
39 changes: 39 additions & 0 deletions snippets/convertVtexIoOrderPlacedItemsToGA4Items.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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
/*
[
{
"item_id": "101",
"item_name": "Sneakers",
"currency": "USD",
"item_brand": "BrandA",
"item_category": "Footwear",
"price": 59.99,
"quantity": 1
}
]
*/
```
47 changes: 47 additions & 0 deletions snippets/convertWooCommerceItemsToGa4Items.md
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coloca um exemplo do input antes do script pra mim?

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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:
{
"items": [
{
"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"
}
]
}
*/
```