Solved

Using GA4 datalyer


Badge

Hi all,

I would like to set up Amplitude and migrate from GA4 to Amplitude. One of our prerequisites would be that Amplitude supports the GA4 datalayer for e-commerce events. As far as I have seen the datalayer is not supported, is this correct?

 

https://developers.google.com/analytics/devguides/collection/ga4/ecommerce?hl=en&client_type=gtm#view_item_details

 

Our shop automatically pushes a datalayer object once an action has been triggered by the user. For example, a view_item event when a product detail page is viewed.

dataLayer.push({ ecommerce: null });  // Clear the previous ecommerce object.
dataLayer.push({
event: "view_item",
ecommerce: {
currency: "USD",
value: 7.77,
items: [
{
item_id: "SKU_12345",
item_name: "Stan and Friends Tee",
affiliation: "Google Merchandise Store",
coupon: "SUMMER_FUN",
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: 9.99,
quantity: 1
}
]
}
});

In other GTM templates, the datalayer is supported so we can use a datalayer variable to retreive data like this “ecommerce.currency (for the currency), ecommerce.value (to get the price) and ecommerce.items (to get the item scoped dimensions). 

How can I pass that information to Amplitude? If it is only one product I can easily use a datalayer variable to pass information like that ecommerce.items.0.item_id but we also have events like view_cart where we have multiple items to capture.

icon

Best answer by dangrainger 16 March 2023, 17:21

View original

11 replies

Userlevel 5
Badge +9

Hi @Marius. We’ve done a similar migration, moving from GA Universal Analytics to Amplitude. When it comes to replicating GA’s Enhanced Ecommerce feature you’ll want to use Amplitude’s object arrays:

https://help.amplitude.com/hc/en-us/articles/9623000954907-Cart-analysis-Use-object-arrays-to-drive-behavioral-insights

You can define whatever object array you like for splitting, pass whatever data points you like in it, and pass it with any event you like…so it’s way more powerful and flexible than Enhanced Ecom over in GA. As far as I’m aware the object arrays can’t just use GA’s ecommerce array from the data layer, so you’ll need to do a bit of work to convert/ freshly implement, but it’s certainly worth the effort.

Badge

Hi @dangrainger thanks for your answer. I read the article but I am not sure if I got it correctly.
Is there any documentation to use Custom HTML tags instead of the Amplitude GTM template because with the template we are unable to use the ecommerce.items object?

Maybe using Custom HTML tags would be an option so we can use Javascript to break down the ecommerce.items datalayer and push data to Amplitude. Would that be an option?

I found the following code in their documentation:

// Track a basic event
amplitude.track('add_to_cart');

// Track events with optional properties
const eventProperties = {
item_id: '{{dlayer - ecommerce.item_id}}',
item_name: '{{dlayer - ecommerce.item_name}}',
item_brand: '{{dlayer - ecommerce.item_brand}}',
item_category: '{{dlayer - ecommerce.item_category}}',
// ....
};
amplitude.track('add_to_cart', eventProperties);

I don’t know if it is possible but to loop through the items array to push all relevant data to Amplitude. In the begin_checkout or purchase datalayer push we can have multiple items but I don’t find any examples of how Amplitude needs this kind of data.

Userlevel 5
Badge +9

I must have been low on coffee yesterday, ignore what I said…

Re-reading the above, you can create a data layer variable in GTM (or whatever TMS you’re using) that captures ecommerce.items from your data layer pushes. Then in the Amplitude tags for these ecom events, add an event property called products (or similar, choose what works for you) and assign the ecommerce.items variable as the value. Once you’ve got this published, Amplitude will take in the ecommerce.items array….head to that event property in the Data module of Amplitude and you’ll see a splitting option which you need to activate (https://help.amplitude.com/hc/en-us/articles/9623000954907-Cart-analysis-Use-object-arrays-to-drive-behavioral-insights#h_01GGX3KMYSJX6EBW6WHRC7PEMH).

If you’ve got this working, you’ll see a bunch of new event properties appear in your Amplitude data schema which will match the elements of your GA items array (so you’ll see products.item_id, products.item_name….products.quantity). You’ll also see the full array retained as just the products variable.

Should work, and should be the quickest/ easiest way for you to get your GA4 items array into Amplitude. Note that I don’t think you can change the display name of the split out properties (yet)….so if you want the UI to display something like products.colour rather than products.item_variant you’re best requesting this feature from Amplitude for them to add to their list.

Badge

Hi @dangrainger , thanks for the update. Just to make sure. I will set up the GTM tag as seen in the screenshot and Amplitude will populate automatically key-values from the ecommerce.items array in the Amplitude dashboard after using the split option? Renaming the key-values is not important for us but thanks for letting me know.

 

Userlevel 5
Badge +9

Yeah that should work. Given you’ve called the variable for splitting “items” you’ll get items.item_id, items.item_name...etc as the variables it creates after the splitting

Badge

Thanks @dangrainger : I created all ecomm events and will check out these options once the data is populated in the dashboard. 

One of the most important tags is of course the revenue tracking. In the GTM template Amplitude offeres a tag type “revenue”. 

I am not really sure how to set up this one. GA4 uses a transcation_id to deduplicate purchases. How does Amplitude do this? Is it also possible to use the ecommerce.items array as event properties here? Should “Product Price” be the sum of all products? Same for quantity?

 

Badge

Hi @dangrainger , I tried to follow the documentation but I am not able to find the splitting option. Maybe I need coffee as well..

 

Userlevel 5
Badge +9

Might be this…

 

Badge

Thanks, I reached out to Amplitude. Do you have an idea about the revenue tracking?

Userlevel 5
Badge +9

Yes, we use it lots. And during implementation discovered from Amplitude solution consultants that you don’t need to fire a separate “revenue” tag type. Instead, we pass the revenue in our “booking confirmation” event tags as event properties like this…

 

 

...and yes you do need the dollar signs there for it to work. FYI that Amplitude currently only displays $ in the UI, but I’ve been assured that other currency symbols are being launched this quarter if you’re not using dollars.

In the above it’s passing a revenue amount for the booking, while the quantity field is 1 (for the transaction, NOT the number of items in the transaction). Once that’s in Amplitude you can configure custom metrics….things like (in our case) “Bookings", “Revenue”, “Average Booking Value”, and so on. You can build segmentation in to these custom metrics too….if for example you wanted a metric “Purchase Type X Revenue”, you can insert Purchase Type X as a condition in the metric builder.

https://help.amplitude.com/hc/en-us/articles/10249480038043-Create-a-metric

Badge

Hi, I have written a javascript function because the GTM template does not work for me (or I just do not understand it). 

 

  // Get the purchase data from the datalayer
var purchaseData = dataLayer.find(function(data) {
return data.event === 'purchase';
});

// Check if there is purchase data
if (purchaseData) {
// Create an array of product objects from the purchase data
var products = purchaseData.ecommerce.items.map(function(item) {
// Create a product object with the required properties
var product = {
product_id: item.item_id,
quantity: item.quantity,
price: item.price
};
// Add any additional product properties you want to track
product.item_name = item.item_name;
product.item_brand = item.item_brand;
product.item_category = item.item_category;
product.item_catogory2 = item.item_category2;
product.item_catogory3 = item.item_category3;
product.item_catogory4 = item.item_category4;
product.item_catogory5 = item.item_category5;
return product;
});

// Create a new Revenue instance with the purchase data
var revenue = new amplitude.Revenue()
.setProductId(purchaseData.ecommerce.transaction_id)
.setPrice(purchaseData.ecommerce.value)
.setQuantity(products.reduce(function(total, product) {
return total + product.quantity;
}, 0));

// Add any additional revenue properties you want to track
revenue.revenueType = 'purchase';
revenue.properties = {
coupon: purchaseData.ecommerce.coupon,
tax: purchaseData.ecommerce.tax,
shipping: purchaseData.ecommerce.shipping
};

// Send the revenue event to Amplitude
amplitude.revenue(revenue);

// Loop through the products and track each one as a separate event
products.forEach(function(product) {
amplitude.logEvent('purchase', product);
});
}

We are testing this at the moment. It basically uses the ecommerce.items object and loops through every item in the object. I don’t know if this is the proper way to do it. 

Reply