Load script before event is fired

dariusd91806962

03-01-2017

is thee a way to load a javascript or Jquery before the event is fired through DTM?  Right now I have a script on an Event Rule that works, but it only works if the event is clicked on twice. It seems the script needs to be loaded before the event fires. When I view the event in debug mode both through Adobe Debugger, and Omnibug, as well as Network, the value is passed to the variable on the first click..(according to the debugger) but it is never passed to the report suite. However if I click on the event twice then the value will show up in DTM Switch debugger and passed to the report suite. Here are some of the various scripts I have used that give me the same issue as mentioned above. Please note I have loaded the script both directly through custom script in the Event rule, as well as in a Data Element. In everyone of the cases the value is not passed to reporting unless the event is clicked on twice. So it appears the script needs to load prior to the event being fired... Would appreciate your helps as I have exhausted all that I know and could learn from various forums and online sources. 

var o_clicks = document.getElementsByClassName('mb-view'); o_clicks.onclick = function() { var o_parent = this.parentNode; name = o_parent.getElementsByClassName('mb-name').innerHTML; return name; }
var name = document.getElementsByClassName('mb-name')[0](this).innerHTML; return name;
$('.mb-view a').click(function() { var mbsItem = $(this).parents('.mbs-item'); if (mbsItem.length) { // Do something here s.eVar17 = (mbsItem.attr('data-mb-name') + ", " + mbsItem.attr('data-mb-id')); console.log(s.eVar17); } else { // Don't follow the link return false; } });

frown

Accepted Solutions (1)

Accepted Solutions (1)

ParitMittal

03-01-2017

Hi Darius ,

As per our understanding from the use-case provided by you , you are basically trying to capture the attribute value of element being clicked and then further pass those values into an eVar or prop Hence to achieve the same there is no need to load the javascript or jquery through Event based rule Instead you can follow the simple approach outlined below to achieve your use case.

1. Create an Event based rule with Event type as "Click" and "Element or Tag Selector"  as  .mb-view a i.e. css selector of element on the page on which you want the element to be triggered.

2. Then Under the Adobe Analytics Tab - Please paste your code in Custom Page Code window:

3. Save the rule and you are good to know.

Let me know in case of any more questions or queries.

Thanks & Regards

Parit Mittal

Answers (4)

Answers (4)

ParitMittal

05-01-2017

Hi Darius,

As per my understanding the usecase is to pre load a script on the page before any of the Event fires Hence to achieve the same the best case would be to create a Page load rule and trigger it at "bottom of the page" and under "Third Party/ Javascript" section Select "Non Sequential Javascript" and enter your code and the code is injected as asynchronous <SCRIPT/> in <HEAD> below DTM library include script.

Thanks & Regards

Parit Mittal

dariusd91806962

04-01-2017

So in a nutshell what I am saying is that before i created the second rule to preload the script, it would take 3 clicks beofer the eVar was populated and sent to reporting.  In other words when I had it only witin the Event rule in the custom page code, I would have to fire the event 3 times before the eVar got populated and reported.  

dariusd91806962

04-01-2017

That is exactly what I did. The script is in the Custom page code. So like I said it does not populate on the first click.. the rule does fire, but the event has to happen twice before the value is populated.. Actually it has to happen 3 times, it’s just that I also created another rule that is based on mouseover on the entire Div element of the page in order to make it preload the script, and then the script is fired again within the Event Rule.. When I first had it only within the Event rule I would have to click the event 3 times before the eVar got populated and reported to the report suite.