I am trying to use GTM & custom Javascript to dynamically insert structured mark-up to a page depending on the text that is displayed at a certain position on the page (this changes every few weeks).
To do this I am trying to use If/Else but it results in undefined each time. If have already created a variable that is working correctly and pulling the required text from the page {{EventLocationVariable}}.
I have so far:
function () {
var eventloc = {{EventLocationVariable}};
var customeventloc;
if (eventloc === Manchester) {
customeventloc = 'Manchester details goes here';
}
else
if (eventloc === Liverpool) {
customeventloc = 'Liverpool details goes here';
}
else
if (eventloc === Leeds) {
customeventloc = 'Leeds details goes here';
}
else
if (eventloc === Birmingham) {
customeventloc = 'Birmingham details goes here';
}
else {
customeventloc = 'some other details goes here';
}
return customeventloc;
}
This might be straightforward, but I just can't seem to get this to work.
Oh, you're missing out on gold. What you actually need is a Lookup Table, and the {{EventLocationVariable}} you already have. Here:
When you work with GTM, make sure you're thoroughly familiar with all its tag/variable/trigger types. The tendency to code your way to the solution may be great, particularly if you're from a JavaScript background, but GTM is great at offering simpler solutions.