adobe-client-data-layer icon indicating copy to clipboard operation
adobe-client-data-layer copied to clipboard

Capability to reset the data layer

Open gabrielwalt opened this issue 4 years ago • 8 comments

For web applications like SPA, it should be possible to simulate a reset of the data layer as when a new page is loaded. This is especially important in cases the Data Layer holds a lot of information and triggers many events because the array can then quickly become massive if a visitor actively browses around for a few minutes or more.

For example: adobeDataLayer.reset();

A more detailed example:

adobeDataLayer.push({ one: "some data" });
adobeDataLayer.length;
// 1
adobeDataLayer.getState();
// { one: "some data" }

adobeDataLayer.reset();
adobeDataLayer.length;
// 0
adobeDataLayer.getState();
// {}

gabrielwalt avatar Apr 16 '20 06:04 gabrielwalt

Given that we want to allow instantiating a new data layer (see #44), we could also use that to reset the data layer.

Here's an example of how that could work for an SPA that wants to reset the data layer each time the visitor gets routed to a different URL:

window.addEventListener("popstate", function (event) {
    window.adobeDataLayer = new AdobeDataLayer();
    // Register again your event listeners here.
});

This has been documented here: https://github.com/adobe/adobe-client-data-layer/wiki/Adobe-Client-Data-Layer#reset-data-layer

gabrielwalt avatar Apr 17 '20 09:04 gabrielwalt

When a view change happens in SPA, the Data Layer does not always change in its entirety, So it will be good to give flexibility for the developers to decide what should be reset and what should be retained when a reset is called on the Data Layer.

For instance, when a user authenticates, the User object of the data layer will be populated. Further view change on the same session can retain this User object. Resetting the Data Layer completely will require the developers to pull user data from backend on every view change.

it will be great if developers can call reset with a an object and array list of events to retain.

Example of how the API could potentially look:

// Example for clearing/resetting everything
adobeDataLayer.reset({}, []); 

// retain user and navigation component object and reset all other data.
// retain "click" and "view-change" event handlers and remove all other event handlers.
adobeDataLayer.reset({ 
"component" : {
  "navigation" : adobeDataLayer.getState("component.navigation")
 },
 "user": adobeDataLayer.getState("user")
 },
 ["click", "view-change"]
); 

bennet-roy avatar Apr 23 '20 20:04 bennet-roy

We could introduce a keepOptions argument for the reset() method with 2 properties, paths and events, which are arrays defining the paths and events to keep. E.g.:

  const keepOptions = {
    paths: ["page", "component.carousel.carousel1"],
    events: ["click"]
  };
  dataLayer.reset(keepOptions);

WDYT? @bennet-roy @gabrielwalt

jckautzmann avatar Apr 27 '20 12:04 jckautzmann

Sounds good. Just to add more flexibility, Can we add a flag which can tell whether to keep or remove whatever is in the Array?



//For removing
const options = {
    paths: ["page", "component.carousel.carousel1"],
    events: ["click"],
    action : remove
  };
  dataLayer.reset(keepOptions);

//For keeping
const options = {
    paths: ["page", "component.carousel.carousel1"],
    events: ["click"],
    action : keep
  };
  dataLayer.reset(options);



bennet-roy avatar Apr 27 '20 17:04 bennet-roy

A proof of concept is available at: https://github.com/adobe/adobe-client-data-layer/tree/feature/create-copy-reset

It works as follows:

// For keeping
const options = {
  keep: {
    paths: ["component.carousel.carousel4"],
    events: ["click"],
    history: true // will keep the data layer items history
  }
};
adobeDataLayer.reset(options);

// For removing
const options = {
  remove: {
    paths: ["component.carousel.carousel4"],
    events: ["click"],
    history: true // will remove the data layer items history
  }
};
adobeDataLayer.reset(options);

jckautzmann avatar May 08 '20 15:05 jckautzmann

How's the status for adding this functionality?

sorenhoyer avatar Aug 25 '20 10:08 sorenhoyer

This capability exists in the 'Adobe Client Data Layer' Launch extension, but I can't find any evidence of it being added here?

image

charltoncraig avatar Feb 17 '21 12:02 charltoncraig

Is there any update on this issue? It would be very useful.

s10-steve avatar Mar 22 '21 08:03 s10-steve