I want to unset/reset page_location
for GTM (and thus GA4).
After pushing a page_location
to the dataLayer, I want to revert to the default page_location (i.e., the current URL) for subsequent events (this is a single-page application, so that URL changes).
My understanding is that dataLayer.push({page_location:undefined})
should do that, but although page_location
shows up in the dataLayer as undefined (according to the console debugger and Tag Assistant), GTM invocations of my GA4 tag use the old value. When I replace undefined
in the call with 'test'
, that actually works, and the new value is used.
I have tried calling several variations of reset
, but they don’t do anything.
I almost hate to mention it, at the risk of diverting attention from the problem, but just to be complete, within GTM we have defined a GA4 Event tag that assigns the event parameter page_location
, which it obtains from {{dlv_page_location}}… and thus if page_location is not set in the dataLayer, the tag uses the default URL location. This works fine except in the case above, where we’re trying to unset/reset a dataLayer value.
After hours of experimentation, I have determined that page_location
is a special case, and must be cached somewhere withing GTM code itself.
I can unset other dataLayer variables via undefined
, but not page_location
.
Since any variation of reset
seems to accomplish nothing, I went so far as to set dataLayer.length = 1
(which does work on other variables), but it did nothing to the original page_location
setting.
At this point page_location
is not in the dataLayer according to the page console debugger, and is not in the dataLayer according to TagAssistant. Once you set it, it apparently cannot be unset, so you have to keep explicitly setting it (in a SPA) rather than letting GTM take it from the current URL.