Unexpected optOut behavior for persisted cookie vs. initialization options
Expected Behavior
- When a user has an Amplitude cookie with
optOut: trueand we initialize an Amplitude client withoptOut: falseas anoptionsuch that a new session is created, the client is initialized withoptOut: false - Alternatively, an Amplitude client initialized with a new session always has its
optOutdetermined by the same precedence rule of persisted cookie setting vs option, regardless of whether the cookie hasoptOut: trueand the initialization option hasoptOut: falseor the cookie hasoptOut: falseand the initialization option hasoptOut: true - Alternatively, there is better documentation around this behavior
Current Behavior
We were using options to determine users' optOut settings. We made a change causing previously opted out users to be opted in. The optOut setting from the users' Amplitude cookies was true, and even though enough time had passed that Amplitude initialized with a new session, the persisted, old cookie value overrode the initialization option and the Amplitude client was initialized opted out.
Possible Solution
The fix depends on whether this is intended behavior, I've made some suggestions above. Also, we may want the initialization option to take precedence regardless of whether initialization starts a new session.
Steps to Reproduce
To confirm that the optOut setting in the cookie is unexpectedly (to me) persisted when true:
- Ensure that the
amp_xxxxxxcookie is deleted - Set code up to initialize the Amplitude client with
{ optOut: true, sessionTimeout: 60 * 1000 }(along with any other non-conflicting params needed for your environment). You can use a callback passed toinitthat takesinstanceas an argument and checksinstance.isNewSession()and logs accordingly to confirm that a new session was started. - Go through a UI flow that will cause the Amplitude client to be initialized
- Fire an Amplitude event with the initialized client
- Inspect the
amp_xxxxxxcookie and confirm that it has a '1' at index 2 when split on '.' reflecting that theoptOutsetting was persisted (and is truthy) - Confirm that the Amplitude event did not cause an Amplitude API call
- Wait at least one minute so the session timeout expires and Amplitude will initialize with a new session
- Change the code setting to
optOut: falseinstead ofoptOut: true - Repeat steps 3 and 4 above
- Inspect the
amp_xxxxxxcookie and confirm that it still is set tooptOut - Confirm that the Amplitude event did not cause an Amplitude API call
To confirm that the optOut setting in the cookie is not persisted when false:
- Ensure that the
amp_xxxxxxcookie is deleted - Set up code to initialize the Amplitude client with
{ optOut: false, sessionTimeout: 60 * 1000 }(along with any other non-conflicting params needed for your environment). You can use a callback passed toinitthat takesinstanceas an argument and checksinstance.isNewSession()and logs accordingly to confirm that a new session was started. - Go through a UI flow that will cause the Amplitude client to be initialized
- Fire an Amplitude event with the initialized client
- Inspect the
amp_xxxxxxcookie and confirm that it has '' at index 2 when split on '.', reflecting that theoptOutsetting was persisted (and is falsy) - Confirm that the Amplitude event caused an Amplitude API call
- Wait at least one minute so the session timeout expires and Amplitude will initialize with a new session
- Change the code setting to
optOut: trueinstead ofoptOut: false - Repeat steps 3 and 4 from this section
- Inspect the
amp_xxxxxxcookie and confirm that it now indicatesoptOut: true - Confirm that the Amplitude event did not cause an Amplitude API call
Other
See this commit. The wording of the test that was added ('should favor the config optOut setting over cookie optOut if the config optOut is set to true') makes me think this could be intended behavior.
Using an init callback and calling instance.setOptOut(optOut) gives the client the correct optOut setting.
Environment
- JS SDK Version: amplitude-js version 8.17.0
- Installation Method: npm
- Browser and Version: Chrome Version 102.0.5005.61 (Official Build) (x86_64)