简体   繁体   中英

how to clear variable in google tag manager through tag

i am trying to reset values of variable in google tag manager when particular tag fires

example:

action : add-to-cart

when 'add to cart' button is clicked then via datalayer i am pushing product data to gtm. this data i am storing in different variable such as - productid, productname,productprice.

now when add to cart button hits then this variables will fill with data.

after that i have created one tag which sends this variable to 3rd party site via tag.

tag name : send-data-to-3rd-partysite
tag type : custom html
tag trigger - productprice does not contain undefined.

it means when productprice does not contains undefined then this tag should fire. (at start it contains undefined ) but problem is that when one 'add to cart' button is hits then the variable contains those product data for entire session and when another add to cart button hits then the data is replaced by new one. problem is here that ' send-data-to-3rd-partysite ' fired when productprice contains some value. it does not fires becuase of productprice is not undefined.

so what i want is when ' send-data-to-3rd-partysite ' tag fires then ' productprice ' variable should be reset or clear. i tries below code in send-data-to-3rd-partysite tag but did not work

<script>
...
window.google_tag_manager[{{productprice}}].dataLayer.reset();
<script>

Try not to reset the dL. It leads to complete flush and that's not a very good thing to do, considering how DL is supposed to be used.

What I saw people doing and it works pretty well, however flimsy looks, is making a custom HTML tag where they push undefines into the DL, effectively overwriting the old values.

In your case, it would be something like dataLayer.push({productprice: undefined})

Now you use timing rules to always fire this tag after the tag that uses productprice has fired and there you go, your cleaning logic is in place.

I just want to mention that if you have to clean up DL like this, then your tracking architecture is a wreck. This must be viewed as a hack and not an elegant one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM