Clear widget context using runtime JS

To clear contextual information from a widget:

  1. Add a script block in the head tag above the Discover beacon.

    RequestResponse
    <script type="module">
    // Widget context code goes here
    </script>
  2. Create an updateContext object and set the context you want to clear to null. In the following code sample, the store.id context is cleared.

    RequestResponse
    const clearContext = {
      "widget": {
        "rfkids": [ "<rfkid 1>", "<rfkid 2>", ... ]
      },
      "context": {
        "store": {
          "id": null
        }
      }
    }
    Tip

    If there are no other keys in store, you can set the value of store to null. In addition, if there are no other keys in context, you can set the value of context to null.

  3. Push the "updateContext" action and object(s) to Discover using rfk.push().

    RequestResponse
    var rfk = window.rfk = window.rfk || [];
    rfk.push(["updateContext", clearContext]);
Important

If an object within context only contains null values, that object is removed from context.

For specific examples of how to use the updateContext action to clear context, refer to Examples: Clearing page and widget context.

Do you have some feedback for us?

If you have suggestions for improving this article,