Question

Writing a small custom javascript macro in GTM to set Category value if an outbound link is clicked. Most internal links are relative urls.

function(){
var eventCat = "";
if({{element url}}.indexOf("redirect") < 0) {
    eventCat = "Outbound Link Clicked";
}
return eventCat;
}

Does the {{element url}}.indexOf("redirect") < 0) { search for "redirect" within the url and set the Category as "Outbound Link Clicked"?

Was it helpful?

Solution

yes, it searches for redirect and sets eventCat but only in the case when redirect is not found.

Just say >=0 if you need to set eventCat = "Outbound Link Clicked"; when the redirect is found.

That's because indexOf returns the numeric position of the first occurence of the searched string. If the returned value is smaller than zero the string is not contained in the examined string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top