How does DTM know which rule is more specific?

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.

[Screenshot]
4 Page Load Rules in DTM
Funny enough this is the exact same order that you see in the UI in DTM itself.

[Screenshot]
The same 4 PLRs in the DTM UI
Looks alphabetical to me, doesn’t it?

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!

8 thoughts on “How does DTM know which rule is more specific?

  1. 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.

    Like

    1. 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)

      Like

  2. 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.

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.