Step 3: Setting Session Tracking Dynamically

Once session tracking is turned off by default, you have two options. The first is to simply keep it off and never turn it back on under any circumstances. However, we strongly recommend against this because session tracking significantly improves the quality of our analytics.

A better option is to toggle session tracking dynamically based on whether the user has consented to cookies. The mechanism to do this will change based on how you are integrating.

Search HH Theme Integrations

Use the runtimeConfig to turn on sessionTrackingEnabled:

  • If you are using the JS Snippet option, call:

    AnswersExperienceFrame.runtimeConfig.set("sessionTrackingEnabled", true)
  • If you are using a subdomain, call:

    AnswersExperience.runtimeConfig.set("sessionTrackingEnabled", true)

Search UI SDK Integrations

Use the ANSWERS.setSessionsOptIn hook:

ANSWERS.setSessionsOptIn(true)

Examples

Here is an example of how you might accomplish this with a simple HTML checkbox. This can be extended to integrate with any cookie consent management system.

<label>
  <input
    type="checkbox"
    onChange="ANSWERS.setSessionsOptIn(this.checked);"
  />
  I consent to the cookie policy.
</label>

In this example, when the user checks the check box, it calls the ANSWERS.setSessionsOptIn hook to update sessionTrackingEnabled. It passes true if the box is checked, and false if it’s not.

If you’re using the JS Snippet option and the Search HH Theme, here’s how the checkbox might function.

<label>
  <input 
    type="checkbox"
    onChange="AnswersExperienceFrame.runtimeConfig.set('sessionTrackingEnabled', this.checked)"
  />
  I consent to the cookie policy.
</label>