0

I hope I am using the correct terms as I am a beginner with JS so please correct me if the terms I am using are incorrect. I am collecting sku numbers from my website via a dataLayer and turning it into a variable called DL - Product Detail - SKUs .When the variable comes across it, it's put into a string. Some products may just come across with a single product sku string (sku1) while other pages have multiple product sku string (sku1, sku2, sku3). What I am trying to do now is this: Create them as single variables, something like this:

  • SKUS: sku1
  • SKUS: sku2
  • SKUS: sku3

I honestly have no idea where to even start. So any suggestions would be appreciated. I have something like this but I don't believe this is the correct direction I need to go.

FYI the {{...}} are to call the SKUs string from the dataLayer using Google Tag Manager.

The _ltk.Activity.AddProductBrowse(productskus); is where I need to call the SKUs out individually.

(function() { 

    var productskus = "{{DL - Product Details - SKUs}}"; 

    return productskus('|');

    _ltk.Activity.AddProductBrowse(productskus);

    _ltk.Activity.Submit();
});

I am not really sure how to give a good minimal example but maybe this will help

This is the object that is pulling through:

    object  
{
  id: '24513',
  model: 'Government',
  skus: '965000050,965000070,965000146,965000147,965000185,965000244',
}

So I created a variable that calls the skus out into a string.

var sku = ('965000050,965000070,965000146,965000147,965000185,965000244')

from that sku variable, I now need each item to be it's own item for lack of knowing the correct term.

Ellen Harris
  • 89
  • 1
  • 3
  • 10
  • We need a real minimal verifiable example. What is the data structure being returned? What are they associated values and keys? Not enough information to do much. – basic Feb 18 '19 at 21:09
  • Okay. Let me see what I can create for a better minimal example. – Ellen Harris Feb 18 '19 at 21:14
  • I updated my question to hopefully fill in some of the holes. – Ellen Harris Feb 18 '19 at 21:24
  • 3
    I think you might just be looking for [`split()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split), but I may not be following your question. – benvc Feb 18 '19 at 21:25
  • 3
    Possible duplicate of [How to split a comma separated string and process in a loop using JavaScript](https://stackoverflow.com/questions/3245088/how-to-split-a-comma-separated-string-and-process-in-a-loop-using-javascript) – Mouser Feb 18 '19 at 21:36

2 Answers2

1

You can build an array of SKUs by doing this.

var productskus = "{{DL - Product Details - SKUs}}";
var skuArray = productskus.skus.split(',');

Once you have that array you can iterate over them and call the AddProductBrowse method:

skuArray.forEach(function(sku) {
  _ltk.Activity.AddProductBrowse(sku);
});

I think this might be what you are looking to do.

0

I wanted to follow up in case anyone else ever has an issue like this.

I had to create another GTM variable to split the string of SKUs:

  function(){
    var productSKUs = {{DL - Product Details - SKUs}}.split(',');
    return productSKUs
   }

The I had to update my Custom HTML tag to call out the split sku variable I created above and put it in a for loop to call out each different sku:

function() { 

// Repeat this function for every item purchased -> done.
var productsku = {{JS - Split - Skus}};
  for (var i = 0; i < productsku.length; i++) {
    _ltk.Activity.AddProductBrowse(productsku[i]);
}

And once I ran that I was seeing the data I needed in the network tab!

Thanks for all the help.

Ellen Harris
  • 89
  • 1
  • 3
  • 10