Amit Joshi
Amit Joshi

Reputation: 11

Fire DTM rule after certain seconds

This is one of the scenarios suggested by our clients which includes firing DTM rule after certain time.

Scenario - As per the requirement, once the website is loaded, and the user does not perform any activity, a page load rule must be fired after 5 seconds. In case a user performs a click or any other action before 5 second, then that particular tracking rule must be fired.

Any suggestion on how should i go for this one.

Upvotes: 0

Views: 1395

Answers (1)

CrayonViolent
CrayonViolent

Reputation: 32517

This is meant to be a PoC, a starting point. You will need to expand on this based on what you define.

As mentioned in my comments on your question, you need to define what "website is loaded" actually means to you (in a way that can actually be coded), and also define what constitutes a user action (also in a way that can actually be coded).

As a Proof of Concept (PoC), I will define "website is loaded" as the window.load event, and "click or any other action" as "clicks on any link".

Also, you weren't entirely clear on what you want to actually trigger. Near as I can tell, you want to trigger the same thing no matter what, and have stated no difference otherwise. So I'm going to make this PoC simplified accordingly, by calling a single Direct Call rule exposing a message signifying which scenario happened.


Part 1 - Timeout

A Page Load Rule is created and set to trigger on the window.load event. When that happens, a setTimeout is called, which will call a function after 5 seconds have passed. When the function iscalled, it checks if a Data Element called "fiveSecondRule" exists. If it does not exist, then it sets it to a value "timeout" to signify it was set from this code. Then a Direct Call rule named "fiveSecondRule" (defined below) is called.


Instructions

First, go to Rules > Page Load Rules, and click Create New Rule. Give your rule a Name of "Five Second Rule - Timeout".

Under Conditions, set the Trigger rule at dropdown to "Onload".

In the Javascript / Third Party Tags section, click Add New Script to open up a script overlay. Name it "config". For Type, select "Non-Sequential Javascript", and select/check the Execute Globally option.

Add the following code to the code box:

window.setTimeout(
  function() {
    if (!_satellite.getVar('fiveSecondRule')) {
      _satellite.setVar('fiveSecondRule','timeout');
      _satellite.track('fiveSecondRule');
    }
  }
  ,5000
);

Click Save Code and then Save Rule to save the rule.


Expanding the PoC

This is what you will need to alter if your definition of "website is loaded" is something other than window.load event. For example you may instead define it is DOM Ready, in which case you can simply change Trigger rule at to "DOM Ready". However, if your definition is more complex (e.g. waiting for some callback from your site's framework), then you will need to instead put this into a Direct Call rule and explicitly call it yourself.

Note: There is an Event Based Rule of "time passed" Event Type, that you can set to trigger after 5 seconds. As an alternative to the PoC you can do this instead, and add a rule condition similar to described in Part 2 below (instead of code above). The DTM documentation does not detail when the timer actually starts or how it actually keeps track of time for this event.

I haven't really spent much time trying to reverse engineer the core DTM library, but from 5 second eyeballing of it, near as I can tell it "registers" events of this type sometime during "Top of Page" code execution (where DTM Header script is placed), and it uses a cookie/localstorage and a timeInterval to constantly poll it.

So if this all fits your definition of the 5 second rule, it's a little bit less coding to make a 5 second Event Based Rule instead. But I chose the Page Load Rule and setTimeout method above to provide you more flexibility as a baseline, should your definition be more complex.


Part 2 - User Actions

An Event Based Rule is setup to trigger whenever the user clicks on a link. When the rule triggers, the condition code checks if the Data Element named "fiveSecondRule" is set. If not, then a value of "linkClick" is set to signify a link was clicked. Then the Direct Call rule named "fiveSecondRule" is called.


Instructions

Go to Rules > Event Based Rules and click Create New Rule. Give your rule a Name of "Five Second Rule - Link Clicks".

Under Conditions > Event > Event Type, select "click" (should be the default option).

Then For Tag > Element Tag or Selector, add "a" (no quotes).

Note: It's beyond the scope of this post to get into, but depending on what other code is implemented on your site/page, you may need to change some of the other settings in this section.

Next, under Rule Conditions > Criteria, select "Data> Custom" and click Add Criteria. In the Custom code box, add the following code:

if (!_satellite.getVar('fiveSecondRule')) {
  _satellite.setVar('fiveSecondRule','linkClick');
  _satellite.track('fiveSecondRule');
}
return true;

Click Save Rule to save the rule.


Expanding the PoC

As mentioned, this just covers clicking on links. It sounds like your definition of "action" includes more than that. Once you define what constitutes a user action, create more Event Based Rules accordingly.


Part 3 - Trigger

A Direct Call Rule is setup to act as the "trigger". All rules from Part 1 and Part 2 will ultimately call this rule, and this is where you place the code you want to execute whenever a user performs an action before 5 seconds, or when the 5 seconds pass.


Instructions

Go to Rules > Direct Call Rules and click Create New Rule. Give your rule a Name of "Five Second Rule - Trigger". Under Conditions > String, add "fiveSecondRule" (no quotes).

From here, you can trigger whatever tools you have implemented, or add whatever 3rd party tags you want to trigger. You can use %fiveSecondRule% syntax in the tool fields and it will have a value signifying how it was invoked (e.g. "linkClick" or "timeout", shown above). For javascript syntax (in js code boxes) you can use _satellite.getVar('fiveSecondRule') to get the value.


Expanding the PoC

I suppose it's within the realm of possibilities you may want to trigger separate Direct Call Rules depending on whether it triggers before or after 5 seconds are up. But that really depends on what you are ultimately looking to trigger, which you haven't clarified.

But if so, then rename this Direct Call Rule to "Five Second Rule - Before" and for the Condition, change it to "beforeFiveSecondRule" (no quotes). Also, go back and change the _satellite.track() call argument to this, for all rules you made from Part 2.

Then, create another Direct Call Rule named "Five Second Rule - After", and for the Condition, put "afterFiveSecondRule" (no quotes). Also, go back and change the _satellite.track() argument in Part 1 to this.

Upvotes: 2

Related Questions