- Print
- DarkLight
Lookup Tables - Contains and Regex Match
This article version is deprecated. Please find here the latest version: https://docs.jentis.com/docs/lookup-tables-contains-and-regex-match
Currently there are multiple ways to create a common programmatic technique: lookup tables. For data scientists and tag management experts that is a frequently used scheme. Though we do not have that variable template yet available in JENTIS Tag Manager, but here we want to provide you options to get your work done nonetheless.
Basic Lookups
Two generic options to lookup a value exists that will work on per-container basis. So for each container and environment of your JENTIS implementation a different value can be used.
For that there is a "Constant" variable available. Also the same is possible on each tool configuration.
This is what the variable will look like:
And on tool-level the same lookup table option is possible per container/environment.
Further Lookup Options
If that is not fitting your requirement there is also the option to create a custom JavaScript server-side (backend) variable that will do the trick. Here is a guide on two possible implementations, one per contains- and one for regular expression matches.
Both require you to create a custom JavaScript variable on the backend. Navigate to the "Data Sources: Variables" section in your JTM account and add a new element.
Select "Custom Backend JavaScript":
There you can implement the both following options for lookup purposes.
Lookup per Contains-String
Use the following Code as a copy-paste to your variable (simply paste it to the JavaScript code block) - and find the walkthrough to the code below:
function(){
const LOOKUP_VARIABLE = "JTM_variable_id"; // JENTIS Frontend (Client) Variable ID
const LOOKUP_CONTAINS = [
{
contains : "cat",
then_value : "1"
},{
contains : "dog",
then_value : "2"
},{
contains : "fox",
then_value : "3"
}
];
const DEFAULT_VALUE = "0";
// DO NOT CHANGE THE FOLLOWING SECTION;
const INPUT_VALUE = this.getFrontendVariable(LOOKUP_VARIABLE);
if(!INPUT_VALUE || INPUT_VALUE == "" || LOOKUP_CONTAINS.length == 0)
return undefined;
for(var i = 0; i < LOOKUP_CONTAINS.length; i++)
if(INPUT_VALUE.indexOf(LOOKUP_CONTAINS[i].contains) >= 0)
return LOOKUP_CONTAINS[i].then_value;
return DEFAULT_VALUE;
}
To make this lookup work you need to change the first three references.
LOOKUP_VARIABLE must name the JTM variable id to read the input value from. So it can be any variable that is of type "Frontend Variable" (ie. a cookie value, a custom javascript from the client, a GTM data layer value, etc.)
LOOKUP_CONTAINS is an array (list) object. Here you can add as many pairs as you want (Objects with "contains : <value to look for>" and "then_value : <what to return on match>"). So the object structure is: { contains: "cat", then_value: "1"},{ ... } - for each pair to match.
DEFAULT_VALUE is what is returned if no entry matches the input. Though the variable will return "undefined" on errors (ie. when the frontend variable value is empty or undefined).
Lookup per Regex-Match
A similar approach is possible with regular expression matches. Please use the following adjusted code to not look for contains-rules but regex matches. The definitions are same as with the code block above.
function(){
const LOOKUP_VARIABLE = "JTM_variable_id"; // JENTIS Frontend (Client) Variable ID
const LOOKUP_REGEXP = [
{
regexmatch : /^cat$/,
then_value : "1"
},{
regexmatch : /^dog$/,
then_value : "2"
},{
regexmatch : /^fox$/,
then_value : "3"
}
];
const DEFAULT_VALUE = "0";
// DO NOT CHANGE THE FOLLOWING SECTION;
const INPUT_VALUE = this.getFrontendVariable(LOOKUP_VARIABLE);
if(!INPUT_VALUE || INPUT_VALUE == "" || LOOKUP_REGEXP.length == 0)
return undefined;
for(var i = 0; i < LOOKUP_REGEXP.length; i++)
if(INPUT_VALUE.match(LOOKUP_REGEXP[i].regexmatch))
return LOOKUP_REGEXP[i].then_value;
return DEFAULT_VALUE;
}