Learn business growth with Google Analytics 4 Forums Google Analytics 4 Extracting Ecommerce Purchase Item Names for GTM Marketing Tag

  • Extracting Ecommerce Purchase Item Names for GTM Marketing Tag

    Posted by Finley on 28 June 2022 at 8:28 pm

    I’m currently trying to extract only the “item_name” data from my purchase dataLayer. I’m using the dataLayer variable, but it doesn’t seem to be working. Since “ecommerce.items” renders the entire item info, I’ve attempted to create a JavaScript Variable in Google Tag Manager. However, I’m still getting an array of values instead of the comma-separated text list that I need. So, how can I convert this array into a text format? Here’s what the JavaScript Variable looks like in GTM:

    `
    var itemslist = [];
    var products = {{dl – ecommerce – item name}};
    for(i=0; i < products.length; i++){
    itemlist.push(
    'item_name': products[i].item_name
    )
    }
    return itemlist;
    `

    I’m using the “ecommerce.items” as the dataLayer variable in GTM. Here’s an example of my complete dataLayer:

    `
    {
    “event”: “purchase”,
    “ecommerce”: {
    “transaction_id”: 0000,
    “affiliation”: “xxxx”,
    “value”: 222,
    “currency”: “USD”,
    “coupon”: “No coupon”,
    “items”: [
    {
    “item_id”: “4291”,
    “item_name”: “xyz”,
    “affiliation”: “xxx”,
    “coupon”: “No coupon”,
    “item_category”: “us”,
    “currency”: “USD”,
    “discount”: “0.00”,
    “index”: 0,
    “price”: 111,
    “quantity”: 1
    },
    {
    “item_id”: “xxx”,
    “item_name”: “abc”,
    “affiliation”: “xxx”,
    “coupon”: “No coupon”,
    “item_category”: “us”,
    “currency”: “USD”,
    “discount”: “0.00”,
    “index”: 1,
    “price”: 111,
    “quantity”: 1
    }
    ]
    }
    `

    Tried to use a For loop in JavaScript Variable, but didn’t get the desired outcome. I’m keen to get the item names in a text format. What should I do?

    Grayson replied 1 year, 5 months ago 3 Members · 2 Replies
  • 2 Replies
  • Li

    Member
    2 April 2023 at 2:02 am

    Gotcha! You aimed to extract just the names of items from your dataLayer, and you were getting an array. But what you wanted was a text list separated by commas. The idea is to convert the array into a comma-separated string, right? If that’s the case, you were on the right track!

    Your initial attempt put the entire ‘item_name’ object into your list. To correct this, you can revise your push command to grab only the item names i.e., products[i].item_name, and collect those into your itemlist array.

    The code should look like this:

    `
    var itemslist = [];
    var products = {{dl – ecommerce – item name}};
    for(i=0; i < products.length; i++){
    itemslist.push(products[i].item_name); // just the item name
    }
    return itemslist.join(','); // join the array into a string
    `

    So, by correcting the push command and also using the join method, you’ll be able to convert the array into a string where its elements are separated by commas. I hope that helps! Give it another shot!

  • Grayson

    Member
    25 June 2023 at 7:15 am

    You may be facing issues because your current variable setup doesn’t seem to be extracting the “item_name” data properly. You may need to adjust your JavaScript variable in Google Tag Manager so that it correctly accesses the “item_name” data from the dataLayer variable representing your “ecommerce.items”. After doing this, if you are still getting an array, you can use JavaScript’s join() method. This method can convert your array of “item_name” data into a single text string, with each item separated by a comma.

    Here’s a snippet of how you may adjust your code:

    `
    var itemslist = [];
    var products = {{dl – ecommerce.items}};
    for(i=0; i < products.length; i++){
    itemslist.push(products[i].item_name);
    }

    return itemslist.join(', ');
    `

    Doing so should give you the item names in a text format, separated by a comma.

Log in to reply.