Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

javascript conditions in DTM custom code

kunjalk3064084
Level 2
Level 2

Hi,

I am trying to set values in some evars, props and events based on keywords present in the URL in the custom code of a direct call rule. so IF the URL contains xyz or abc, set

if (s.pageURL.indexOf("/xyz")>-1 || s.pageURL.indexOf("/abc")>-1)

{

s.event="event1";

s.prop5=s.eVar5=_satellite.getVar('test');

}

any idea on how to make these conditions work?

4 Replies
pabloc82923542
Level 9
Level 9

What are you trying to understand from a user behaviour perspective for the 2 types of URLs? Can you explain in more detail your desired categorization reporting output?

Rydal_Williams
Level 4
Level 4

Hi, your sudo code looks like you are on the right path - are you having issues, if so what are they?

I'll use document.location instead of s.* though.

kunjalk3064084
Level 2
Level 2

i am getting data for evar and prop but not for the event. is it required to pass values into event after the evar  and prop?

Abhijeet_Mishra
Employee
Employee

Hi,

Looks like your event script has syntax error.

You currently have it implemented as s.event="event1", where-as it should be using s.events.

Your script should be s.events="event1".

kunjalk3064084
Level 2
Level 2

my bad. it is s.events="event1"

so evar5 and prop5 getting triggered but event1 not getting triggered.

Abhijeet_Mishra
Employee
Employee

If the syntax is correct, another factor which could prevent the event from being triggered completely is that your Direct Call Rule is setup to fire an "s.tl()" and your custom code is missing 'linktrackevents'.

Abhijeet_Mishra
Employee
Employee

Can you IM me the Site URL, you currently have it implemented on? I'd further like to debug the implementation and server calls.

Thanks.

PratheepArunRaj
Moderator
Moderator

Because the syntax is not correct.

s.events="event1"; and not s.event="event1"; 's' is missing. Kindly change.

No need to pass the value into the event.

PratheepArunRaj
Moderator
Moderator

Can you use this instead?

s.events = s.apl(s.events, "event1", ",", 2);

kunjalk3064084
Level 2
Level 2

will this have an impact on other events in the same rule?

PratheepArunRaj
Moderator
Moderator

Wherever you have events, rather than assigning the variable directly, use the below syntax.

s.event = "event1"

to

s.events = s.apl(s.events, "event1", ",", 2);

s.event = "event2"

to

s.events = s.apl(s.events, "event2", ",", 2);

Etc.

SarahEOwen
Level 10
Level 10

Hi!

I had the same problem! In my Direct Call Rule, I set up an s.tl() call in the Adobe Analytics section of the rule. I have eVars and props set in the the Global Variables section and I have eVars, props, and events being set in the Custom Code. Yet none of my events in the Custom Code were being included in the analytics call, even though the s.events variable was being correctly valued.

This is how I fixed it

1) in the Custom Code, you can set your s.event in any fashion (either with your s.events=event2 or by using the append function of s.apl()).

2) add this line of code as the last line in your Custom Code --> s.linkTrackEvents=s.events;

And that is it! Save your rule, refresh the web page, go through the steps to trigger the Direct Call Rule, and you should see the events included in your analytics call

HAve a good day -

Sarah

kunjalk3064084
Level 2
Level 2

update:

replaced s.pageURL with window.document.URL and the rule seems to work fine. however only the first part of the condition is being considered i.e. rules fires when URL contains /xyz but not when it contains /abc.

if (window.document.URL.indexOf("/xyz")>-1 || window.document.URL.indexOf("/abc")>-1)

is there any other way to implement an OR condition?