amplitude / Amplitude-TypeScript

TypeScript Amplitude Analytics SDK
https://amplitude.github.io/Amplitude-TypeScript/
MIT License
126 stars 36 forks source link

Ability to add custom identity storage provider in @amplitude/analytics-browser@v2.0.0 #448

Open williamrobertson13 opened 1 year ago

williamrobertson13 commented 1 year ago

Summary

The cookieStorageProvider property was removed in v2.0.0 of the Browser SDK, so this blocks us from being able to specify a custom storage provider for GDPR compliance purposes. It would be amazing if the SDK provided this type of functionality out of the box (e.g. switchIdentityStorage) or added a custom option to identityStorage so folks can provide custom storage providers again.

Motivations

For GDPR compliance, we would like to track users using memory storage and then switch to cookies only after users have provided their consent to use tracking cookies. We were able to do this in @amplitude/analytics-browser@v1.0.0 by creating a custom Storage instance (https://github.com/MystenLabs/sui/blob/main/apps/core/src/utils/persistableStorage.ts) and using that for the cookieStorageProvider property. We're aware of the default recommended solution of deferring the initialization of Amplitude until users have provided consent, but that isn't quite the same and we miss out on potentially useful insights.

kevinpagtakhan commented 1 year ago

Hi @williamrobertson13, thank you for choosing Amplitude. We like hearing from our customers so we really appreciate sharing this with us. I'm more than happy to find the best path forward for you. Before anything else, I'd like to hear more.

but that isn't quite the same and we miss out on potentially useful insights

Can you elaborate on this point more? I'd also like to explore opportunities on how to improve our deferred initialization logic, even if end up moving forward with a different solution for you.

williamrobertson13 commented 1 year ago

Hey @kevinpagtakhan! Thanks for the quick response. So to elaborate on the problem with deferred initialization - a lot of folks don't accept or reject consent to use tracking cookies when prompted with consent banners on websites. With the current solution of deferred initialization, a user could go to a site -> do all sorts of actions without ever accepting/rejecting consent -> leave the site and Amplitude won't ever initialize and log events. As one alternative solution, I suppose maybe you could add a pagehide listener and initialize Amplitude with the transport type to beacon and the identity storage to none for this particular use case? AFAIK pagehide isn't the most reliable though hence the custom Storage provider route we're experimenting with.

jnthns commented 1 year ago

hey @williamrobertson13 you can set identityStorage to None on init, like this: amplitude.init(apiKey, { identityStorage: 'none' });

This stores everything in memoryStorage, which is wiped when the user abandons the website. You can reinitialize like this once the user consents to cookies: amplitude.init(apiKey, { identityStorage: 'cookie' });

RubenZx commented 8 months ago

hey @williamrobertson13 you can set identityStorage to None on init, like this: amplitude.init(apiKey, { identityStorage: 'none' });

This stores everything in memoryStorage, which is wiped when the user abandons the website. You can reinitialize like this once the user consents to cookies: amplitude.init(apiKey, { identityStorage: 'cookie' });

Hey, is it correct to call init twice to change the indentityStorage? Do you know if there is any helper to do that?

gabrielwallin commented 6 months ago

I'm wondering about the same thing as @RubenZx , we would like to achieve the following process (in order to maintain tracking across multiple apps on the same domain):

pageload: init with sessionStorage consent: init with cookieStorage revoke consent: init with sessionStorage again

michal-filip commented 3 months ago

I share the same interest. From what some of our testing has shown, custom plugins do not work after re-initialization.