user756659
user756659

Reputation: 3512

GA4 with GTM - sending the items array as an event parameter without using datalayer?

Using GA4 with GTM. I'm questioning how to send an array for an event. For example, the add_to_cart event. In my situation I am triggering the tag on my purchase links. On those links I added data parameters for the id, name, and value such as :

<a href="https://example.com/sales/link" data-id="prodid" data-name="prodname" data-value="179.95">Buy Now</a>

There are multiple and the id, name, and value are the only things that change for each link.

Google requires an items array to be sent with the add_to_cart event. Can I enter the items array as shown in this picture using dot notation? I can't supply this information in the datalayer which is why I am grabbing the values that can be different from the link itself (data parameters)... the rest are static and won't change. I can't find any way to create an array variable in GTM so the dot notation is the only thing I could think of.

Is there another way to do this I am missing or not thinking of?

ga4

Upvotes: 3

Views: 7500

Answers (2)

user756659
user756659

Reputation: 3512

@Ramon Put me in the right direction. Set this up as a custom js variable. Since I trigger the tag on link click the {{Click Element}} lets me get those data-parameter values from it to create the array values that are dynamic. I suppose I could have also used the gtm variables I already created for those here too. Anyways, I use this variable as the items event parameter value which returns the array how I wish. Seems to be working fine.

function(){
  var e = {{Click Element}};

  var items = [{
    item_id: e.dataset.id,
    name : e.dataset.name,
    affiliation : 'some name',
    currency : 'USD',
    item_brand : 'some name',
    item_category : 'Software',
    price : e.dataset.value,
    quantity : 1
  }];

  return items;
}

Upvotes: 2

Ramon Ozod-Seradj
Ramon Ozod-Seradj

Reputation: 176

Unfortunately you can't. Your solution sends every value from the items object as an individual event parameter. GA4 requires you to send an array of objects, with one object for every sold item.

The good news is, you can use GTM to create the items array in the correct format using some JavaScript.

Upvotes: 1

Related Questions