1

I am trying to use custom script in the Event Based Rule to fire tag in Dynamic Tag Management.
Please note that my objective is just to fire tag on the page as of now, so that I can leverage this script for my further use in DTM.

Please have a look at my problem statement: In the 'Conditions' section of 'Event Based Rules', I have selected 'Event Type' as 'Click'. Set the 'Element Tag or Selector' as 'div'. Now, I can use 'Manually assign properties & attributes' option here, for eg: setting 'property' as 'id' and 'Value' as '12345' (which is id's value on the page, suppose). So, whenever user clicks on any link with HTML element tag as 'div' having property 'id' and value being '12345', a Omniture tag will fire on the click functionality of this.

What I want now is, instead of using 'Manually assign properties & attributes' option, the similar thing I want to do by writing a script (In Rule Conditions -> Data -> Customs). I am writing this code in the script (after un-checking the 'Manually assign properties & attributes' option ):

$('#12345').click(function() { return true ; });

But this is not working.

Can someone please help me out with this. I am badly stuck in this.

Thanks, Adi

Adwait
  • 49
  • 2
  • 10
  • 1
    This is not a forward question... Please, try composing some code, post it here and then people can try to help you solve any issues you might still have. This is how StackOverflow works. – Ruby Racer Nov 01 '14 at 12:12

1 Answers1

2

If you just want the rule to trigger onclick of div with id 12345 then just put div#12345 as the element tag/selector field (it accepts a css path, which is evaluated with querySelectorAll).

If you are looking to alternatively do it a more "manual" way, i.e. you just want the click event to be generic and do a 100% custom condition w/ your own logic, you MUST enter a value in the element tag/selector field, because DTM needs to know what to attach the event to. So at a minimum you need to do something very top level, e.g. body. IOW you need to do it same principle as event delegation.

Then in...

Rule Conditions -> Data -> Customs

..you can do pretty much anything you want, but it MUST return true or false.

So for example, you had the following:

$('#12345').click(function() { return true ; });

This isn't going to work as expected, because you are just attaching an event handler. An event based rule already handles the event, which is why you have to specify the event, i.e. "click". So that code is basically going to end up returning a falsey value since you aren't actually returning anything.

So instead, you'd do something like this:

Set the element tag/selector to div or something top level like body, then in the custom section, do something like this:

if ( $(this).attr('id') == '12345' )
  return true;
else
  return false;

Note that I use jQuery syntax because you used it in your post; DTM doesn't output jQuery or have it built-in, so you need to ensure jQuery is loaded for that to work. But it's easy enough to do the "vanilla js" version of getting the id attribute of this

If you want to be able to specify your own event handler, like what you had:

$('#12345').click(function() { return true ; });

Then the best thing you can do is instead output this in a page load rule in the javascript / 3rd party script section. From there you'd make the closure call a direct call rule instead of returning true, for example:

$('#12345').click(function() { _satellite.track('doSomething'); });

Then you make a direct call rule instead of event based rule, and for the string condition, you'd put "doSomething" and then pop whatever you want from within there.

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
  • Thank you so much Crayon for this detailed explanation. You answers are always awesome. Let me try your way and will get back to you if I have any new question. – Adwait Nov 02 '14 at 16:37
  • would it be possible to use a custom event to do... object.addEventListener("copy", myScript); – Finbar Maginn Mar 08 '16 at 12:33
  • 1
    @FinbarMaginn within an event based rule, under **Conditions** > **Event** > **Event Type** you can choose "Custom" and specify the custom event name (e.g. "copy"). However this basically *subscribes* to an event. It does not *create* a custom event. You will need to do that with your own code (e.g. your own js within a dtm page load rule 3rd party tag container) – CrayonViolent Mar 08 '16 at 15:25
  • @CrayonViolent so in the case of a 'copy' event handler, would it be possible to just subscribe to the already existing 'copy' event? the idea would be to use that instead of creating my own event and injecting the JS onto the page. – Finbar Maginn Mar 14 '16 at 14:40
  • If there already exists a `copy` event attached to the object, you would still do the above. Like I said, the above method *subscribes* to an existing event. – CrayonViolent Mar 14 '16 at 17:34