How do I use variables within a Custom JavaScript variable?
When you create a Custom JavaScript function variable, you might need access to existing pre-configured or custom defined variables to complete the logic of function. You can use such variables in your Custom JavaScript function in the following ways (starting from Matomo 5).
Using pre configured variables
How to get a list of existing pre-configured variables
- Login to your Matomo instance.
- Click TagManager from menu.
- Click variables from left menu section
- Scroll down to see the available list of pre-configured variables
Now you can use the above pre-configured variables in your Custom JavaScript function variable in following way
function () {
//If current element is a clicked element, return the attribute `my-attribute` or else return `not found`
if ({{ClickElement}}) {
return {{ClickElement}}.getAttribute('my-attribute');
} else {
return "Not Found";
}
}
Using custom defined variables
If you have a variable with name browserLanguage
and want to use the same in your Custom JavaScript function, you can do by wrapping the name with double curly braces ({{browserLanguage}}
).
Using {{browserLanguage}}
in your Custom variable
function () {
// return the value determined from `browserLanguage` variable and append a string "country" along with it
return {{browserLanguage}} + 'country';
}