How to removeGlobalContexts for a schema

Hi ya,

I’m having a little trouble understanding the removeGlobalContext function for the react browser v3 tracker. There don’t appear to be any examples.

It says to send in context to be removed, however, normally contexts have a schema and data. I want to remove any global contexts that have that particular schema, independent of the data in the previous context.

Here is where we are adding our schema:

  const schema = 'iglu:com.submittable.snowplow/gms_page_context/jsonschema/1-0-1'
  const pageContext = {
    schema,
    data: snakeCaseObjectKeys(newData),
  }
  addGlobalContexts([pageContext])

However, we have it setup, so that anytime our react/redux state updates, we update the global “gms_page_context” context with the above code. Therefore, it is adding the same context over and over…we want to remove old contexts with the above schema and send only the new one every time.

Any help on doing this with the removeGlobalContext would be appreciated.
Thanks!
Patrick

Hello, sorry about the lack of examples in the docs for global context, we are currently working on adding more examples there.

The removeGlobalContexts function takes the same input that you passed into addGlobalContexts. So if you called addGlobalContexts([pageContext]), you will need to call removeGlobalContexts([pageContext]) with the same, unmodified pageContext.

I don’t think that removeGlobalContexts is very helpful in your case as it would require you to store =the added context somewhere to be able to remove later. Maybe the clearGlobalContexts() function to remove all global contexts would be more useful unless there is some other global context that you want to keep.

It might be easier for you to use a context generator function to generate the global context entity on the fly when it is needed. You can use it as follows:

const contextGenerator = (args) => { // the args contain info about the event, but you can ignore it
     return {
         schema: 'iglu:com.acme.marketing/some_event/jsonschema/1-0-0',
         data: { test: 1 },
     };
};
addGlobalContexts([contextGenerator]);

You could add logic inside the generator function to fetch the global context from an up-to-date state.

2 Likes

Thanks a lot for explanation. It turns out our FE engineer was able to pull the previous data before setting the new data in redux so we were able to use that method after all.

Thanks again!
patrick

1 Like