Timer Actions - Custom State
  • 02 Aug 2022
  • 2 Minutes to read
  • Contributors
  • Dark
    Light

Timer Actions - Custom State

  • Dark
    Light

 

This article version is deprecated. Please find here the latest version: https://docs.jentis.com/docs/timer-actions-custom-state

Article Summary

Timing an event or waiting for some result can be a tricky challenge in the asynchronous world of JavaScript. Hence callbacks, promises and event-listeners are the best practice to keep everything running smoothly. However that is not always possible. So here come timings and intervals into play, that are more primitive but can be a good help in tag management.

Timing States in JENTIS Tag Manager

With the concept of JENTIS States (JENTIS States Framework) we can invoke our tracking at certain situations and events. Timing is no different. So we will use that existing framework in your JENTIS implementation to activate after some time-interval loops.

For that to activate in your JTM account you need to add a new custom state. Navigate to the "Components: States" section and add a new element.

In the "JavaScript Code" text field input paste the following - a quick walkthrough to the code is found below:

function(initState){
	var INTERVAL_TIME_MS = 1000; // interval timing in millisec
	var LOOPS_COUNTER 	 = 10; // number of intervals

	// DO NOT CHANGE THE FOLLOWING SECTION
	var int_id = setInterval ( function() {
		_jts.push({
			track: "interval",
			int_timer: INTERVAL_TIME_MS,
			int_count: LOOPS_COUNTER
		});

		initState();

		if (LOOPS_COUNTER < 1) {
			clearInterval(int_id)
		}

		LOOPS_COUNTER--;
		
	}, INTERVAL_TIME_MS);
}

Inside the code block feel free to customize those both options:

  • INTERVAL_TIME_MS is an integer value that indicates the interval time per loop in milliseconds (ie. "1000")
  • LOOPS_COUNTER is an integer value that counts the number of loop iterations, we strongly recommend to set this value and not to intentionally run the loop infinite times, a value below 1 will activate the interval a single time and any non-integer value will break this JTM State execution

Complete the Implementation

There are also some more elements that you can add to this implementation. Within the JTM State code we also pushed two custom properties that can be used in triggers and tags:

  • int_count: the current intervals counter value
  • int_timer: the interval timer value

Let's add a variable that reads for each interval the current interval count. Navigate to the "Data Sources: Variables" section in your JTM and add a new "Get JTM data layer value" variable.


In that variable you can read what was pushed in the according JTM State we created in the steps before:

Now this variable comes in handy, ie. for triggers and tags. So you can activate a tag after a set interval. In the following screenshot we defined a trigger condition to activate when the counter is exactly 5.

Now it is up to you to actually put all those elements into use on your implementation.


Was this article helpful?