Learn business growth with Google Analytics 4 Forums Google Analytics 4 Improving GTM Data Layer by Clearing Previous Event Properties

  • Improving GTM Data Layer by Clearing Previous Event Properties

    Posted by Felix on 11 June 2022 at 5:30 pm

    “When I make an event and call dataLayer.push, the old properties from data layer keep showing up in the new events. After the first event, ‘Zack Ryder’, I tried making a different event ‘John’ with a hobby ‘Sports’, but it also includes ‘Ryder’ from the previous event. Isn’t it supposed to only record ‘John’ and his ‘Sports’? When I tried resetting, it just erased everything I had. Any solutions on how to make this right?”

    Emma replied 11 months ago 3 Members · 2 Replies
  • 2 Replies
  • Lucas

    5 October 2022 at 11:36 pm

    In this scenario, it seems like old properties from your previous event are recurring because you’re not setting these properties as null when they’re not in use. What you can do is while sending the data object include all properties, but for the ones that are not being used, just assign them as null. So when you’re sending ‘Zack’ and ‘Ryder’, also send hobby and age as null.

    Also, you mentioned something about trying to reset through tag sequencing. The thing here is, you should call the reset after your event is pushed, not before. This way, the data you need doesn’t get wiped out prematurely.

    Real quick though, keep in mind that although tag sequencing could work, it might affect the state of your data layer.

    Now, if you can’t nullify fields from the frontend and still want to fix this using Google Tag Manager, you’ve got another option. This involves creating a custom JavaScript variable (CJS). Here, all your data layer variables go through this CJS variable. The function of this CJS variable would be to inspect what values each event has and pass them on as required. But remember, you’ll need to do this for each data layer field that you want to cleanse.

  • Emma

    26 November 2022 at 7:33 am

    It sounds like you’re observing persistent data between events, when you’re aiming to have specific data for each event. Google Tag Manager’s dataLayer is designed to persist data across multiple events, so it’s the expected behavior. However, if you only want specific data with each push, consider creating a new object each time you push to the dataLayer. The dataLayer doesn’t automatically clear itself after each event, so data from prior pushes continues to be present unless overwritten. So, instead of reset, which erases everything, this approach would help you retain the information you want. The trick is to treat each dataLayer.push() as its own unique event with its own unique data, and be deliberate with the data you push each time.

Log in to reply.