Tag Manager: Unable to reset Custom Dimension to "null" from JS variable

I have a “Custom JavaScript” variable in MTM with varying output over the lifetime of a visit, let’s say the following:

function () { return (Math.random() > 0.5) ? "SET" : null; }

I send this Variable as an Action-scoped Custom Dimension with every request in my Matomo configuration, lets say in “dimension1”.

The problem is: Once the variable has ever returned a “real” value ("SET" in the example above), then “dimension1” will never be “reset” to the empty value (neither null nor ""), even when the underlying variable returns null. It will always stay at the value "SET". There is also no possibility to define that the default value of a variable is null instead of "".

I debugged this down to the following two locations:

In plugins/TagManager/javascripts/tagmanager.js (L1233):

if ((!utils.isDefined(value) || value === null || value === false) && utils.isDefined(this.defaultValue)) {
    value = this.defaultValue;

:arrow_up: This sets the variable to the default value, which is the empty string "", if the variable ever returns null, undefined or false.

In plugins/TagManager/Template/Tag/MatomoTag.web.js (L232):

if (dimension && TagManager.utils.isObject(dimension) && dimension.index && (dimension.value || dimension.value === null)) {
    tracker.setCustomDimension(dimension.index, dimension.value);

:arrow_up: This prevents the Custom Dimension from being set to that empty string "", because dimension.value is falsy.

Is there another way to set and reset Custom Dimensions via Custom JavaScript variables? And/or am I right that this is not intended behaviour?

I am on Matomo 4.15.1.

Hi @cataclyst
This should have been fixed in Matomo 4.8.0:

Hi @heurteph-ei, thank you for your research!

I think in the issue you linked to there is talk about a first fix that was implemented (“set the CD whenever there is any ‘value’, including all falsy values”, https://github.com/matomo-org/tag-manager/pull/441) and then that had to be reverted and was replaced by a different fix with “stronger” constraints (“set the CD whenever the value is set or null”, https://github.com/matomo-org/tag-manager/pull/593).

In my case, the value of the Tag Manager variable yields null, which falls back to the default value of "" (no way to prevent this in MTM), which is “falsy” (so fix #1 from the issue would have worked), but not null (so fix #2 does not work).

In the issue, the recommended workaround is to explicitly set the CD to null. But I cannot do that because my way of setting that CD is that MTM variable, which cannot in any way return null because of the mandatory default value. :smile: Any thoughts?

Hi @cataclyst
You’re right!
I think you should add a comment in the MTM ticket 440, in order to take this into account.

Thanks, I did that, let’s see what happens. :slight_smile:

1 Like