Creating a Custom Variable for Scope
To get started, we need to create a custom variable. In the JENTIS State and JENTIS Data Layer context, a variable is a storage unit that contains specific data or information about the user, session, or other elements.
This variable will define either "true" or "false" as the value, with the following meaning:
"true": this is the first occurrence per this variable's scope (page/session/user scope is defined in the variable, see the following section)
"false": this is not the first occurrence per this variable's scope
With this design, we can use that as a condition in a trigger that reads "activate once per ... equals: true" (or false), respectively. This gives you a readable JENTIS configuration that helps with maintenance.
We need the variable that holds most of the logic to achieve this result.
In JENTIS, navigate to your list of variables and create a new one. There, select the server-side (backend) custom JavaScript variable. Name it "Once per User" and enter the following code:
async function() {
const STORAGE_KEY_NAME = "recurrence_limiter";
const RECCURNC_LIMITER = this.getFrontendVariable("user_doc_id");
const MAX_STORAGE_SIZE = 3;
let existing_ids = await this.storage.read(STORAGE_KEY_NAME) || "";
if(existing_ids && RECCURNC_LIMITER != null && RECCURNC_LIMITER != "" && existing_ids.indexOf(RECCURNC_LIMITER) >= 0){
return false; //this value was received previously
} else if(RECCURNC_LIMITER && RECCURNC_LIMITER != null && RECCURNC_LIMITER != "") {
existing_ids += RECCURNC_LIMITER+";";
if(existing_ids.split(";").length > MAX_STORAGE_SIZE){
existing_ids = existing_ids.split(";");
existing_ids.shift();
existing_ids = existing_ids.join(";");
}
this.storage.write(STORAGE_KEY_NAME, existing_ids, Date.now()+94608000000);
return true; //this value is new
}
return null;
}
Let's run through this code line by line.
We must define the scope first to return either true (this observation is new in respect of the scope) or false (this observation is not new).
This is done so in the "RECCURNC_LIMITER" constant. It is assigned the value of a variable in reference, in this example it is "user_doc_id" (the ID of the JENTIS User ID variable, the single unique value for a user in JENTIS Tag Manager).
With this set, we have determined the scope. We will return the value "true" only once per this single user ID.
Further, the constant "MAX_STORAGE_SIZE" determines how many inputs of an ID should be persisted. For a user, this can not be more than one value. However, for other scopes ("once" per transaction ID, session ID, page ID, etc.), it can be a good idea to also keep track of the recent values (the storage here works in a first-in-first-out scheme; if the max. count is reached, the first value inserted will be removed from the memory).
The value of "STORAGE_KEY_NAME" is a static string that defines the key name for the persistence of the ID. The name can be anything (but should be used only in this use case to mitigate a situation of collision).
No other value in the code must be customized. The most crucial section is the RECCURNC_LIMITER value, so let's discuss this in more detail.\
Last updated
Was this helpful?