Here’s a simple question:
If I have two Page Load Rules setting the same variable, and if both rules fire, which one wins?
Hm…
I might have a standard PLR that fires on every single page of my site. This rule might use the URL of the current page to compute some kind of page name. This page name is then written into s.pageName
.
(Note: while this is not recommended, it happens quite a lot in practice.)
Another rule — say one specific for product detail pages — sets s.pageName
according to a slightly different algorithm.
This is a fairly common scenario.
“Hierarchical rule sets”, in which some data is processed the same way everywhere, while other rules cover more specific cases, is one of the normal ways to deploy Analytics using Tag Management Systems.
Do we know?
When I was asked this question for the very first time, I thought “well, there’s obviously going to be an order, right?!”, hoping there would be one. Nothing is worse than non-deterministic behaviour when you want to program…
So is there? Does DTM follow some order when it works through the rules it has to fire? And is that order always the same?
Turns out, yes, there is an order. In essence, the rules are stored in a Javascript array.
And so there is our answer: the rules are stored in alphabetical order, and DTM executes them in that order, too.
No, sorry, scratch that…
The rules are stored in alphabetical order, and when DTM traverses them to see which one to execute, it does so in the same order.
Wow, that was a short post, wasn’t it? Finally a simple answer to a simple question!
When I asked Rudi and Jeff, they told me that I shouldn’t rely on this, because they cannot confirm this won’t change in the future.
LikeLike
Indeed, and a good point.
LikeLike
I’ve actually seen that often it’s whichever rule you saved most recently that executes first.
LikeLike
Ah. I haven’t seen that so far.
When that happens, do the rules get listed in the order you described in the DOM? Or still alphabetically?
LikeLike
Same observation here. I updated/saved two rules that fire on the same page. That affected the actual rule order… -> seems like it first fires the rule that was most recently saved (and published). DTM debugger confirms that theory. The “older” the rule the earlier it gets fired (so long as they have the same trigger)
LikeLike
Good to know, thanks Paul!
LikeLike
I was looking at this exact thing the other day. Using the DTM Switch plugin I watched the rules fire in descending alphabetical order. I tried changing some rule names to change the order, and the updated rules rearranged to fire in their new alphabetical order.
Something else to keep in mind – just because a rule starts before another rule doesn’t guarantee it finishes before that following rule. There are ways to get rules to fire (and finish) in the order you prefer or to have rules check if a variable has already been set by a separate rule, but they can get complex, confusing, and potentially unstable over time so I avoid that if at all possible.
LikeLike
Indeed! It is good practice to keep rules conceptionally independent, I guess.
LikeLike