0

Suppose I have a string variable {{Name}} that looks like this:

APPLE-CARROT-PAPER-HILL

I want to create 4 variables using JavaScript that captures each piece:

var1 = APPLE
var2 = CARROT
var3 = PAPER
var4 = HILL

In Tag Manager, I assume the JS for var1 would be:

function(){
  var name = {{Name}}.slice(0, {{Name}}.indexOf("-"));
  return name;
}

but how then to do the others?

3
  • 1
    You should use an array to contain that data. Creating global variables like this isn't a good option. Commented Aug 27, 2021 at 6:36
  • 1
    Yeah, why not simply const names = Name.split("-") ? Commented Aug 27, 2021 at 6:38
  • Thanks all. I am extracting image file names from my website that are descriptive: i.e. BEACH-KIDS-GAMES.jpg The idea is to extract the file name "BEACH-KIDS-GAMES" and then split that into different variables to pass to a GA4 tag to get insights into which content users click on (i.e. do beach images get more clicks than go karts) Commented Aug 27, 2021 at 6:52

5 Answers 5

1

Not sure what You are wanting to do, but it's easier and better to:

  1. Store all the values in one array, not separate vars.
  2. Use split instead of complicated function to extract them.

var str = 'APPLE-CARROT-PAPER-HILL';
console.log(str.split('-'));

Sign up to request clarification or add additional context in comments.

4 Comments

what happend with {}? it doesn't work in my answer.
What do you mean "what happend with {}"? What {}? You have a string, not an object
@JeremyThille, he wants to use a custom javascript variable in GTM, ie.e an anonymous function that requires curly braces. General Javascript advice is not necessarily applicable in a straightforward way to GTM, which has a few extra conventions.
Well... to be exact I referred to the "{}" button (code block) in the answer editor that didn't work for me, made whole code in one line and didn't color the syntax... replaced it with cyte at first, then with snippet
1

var name_str = "APPLE-CARROT-PAPER-HILL";

function a(){
  var v1, v2, v3, v4;
  var name = name_str.split('-');
  [v1, v2, v3, v4] = name;
  console.log(v1);
  console.log(v2);
  console.log(v3);
  console.log(v4);
}

a();

3 Comments

Good solution, but why include jQuery? There's 0 need for it
I was just testing other snippet. And it stuck there.
Aaah, fair enough :)
1

Since you are using GTM (so far the other answers have ignored the google-tag-manager tag), I suspect your actual question is if there is a way to solve this with a single variable. Alas, no, you need to create a variable for each piece of your string

APPLE-CARROT-PAPER-HILL

// Apple
function(){
  return {{Name}}.split("-")[0];
}

// Carrot
function(){
  return {{Name}}.split("-")[1];
}

etc.

You can make this a bit nicer but creating a custom template that returns the value for a given index from an array, but if you want to use the parts of the name in separate fields (e.g. for use as custom dimensions) then alas you need a variable for each segment of your delimited string.

Comments

0

Try This,

let name = 'APPLE-CARROT-PAPER-HILL';
let nameAr = name.split('-');
let var1 = nameAr[0];
let var2 = nameAr[1];
let var3 = nameAr[2];
let var4 = nameAr[3];

Comments

0

I hope this code helping you

var name = "APPLE-CARROT-PAPER-HILL"

name.split("-")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.