DavidWells / analytics

Lightweight analytics abstraction layer for tracking page views, custom events, & identifying visitors
https://getanalytics.io
MIT License
2.44k stars 245 forks source link

analytics.reset() does not reset userId in google-analytics plugin #340

Open tomek3e opened 1 year ago

tomek3e commented 1 year ago

Google analytics plugin does not have an implementation for reset, hence after: analytics.reset() userId is still being sent.

dobesv commented 1 year ago

True. Based on the docs it should be sending GA a userId of null or "null" (I can't tell for sure which) but in the code it looks like the plugin will ignore null or empty userId values and just leave the userId as it was before.

dobesv commented 1 year ago

I guess the only workaround currently is to do a page reload, although I suppose GA would still store a cookie or value in local storage in that case so even then it could remember the old value.

tomek3e commented 1 year ago

Correct, even page reload will not work so I am extending the plugin to handle my use case (chrome kiosk mode - multiple users on same device):

  1. Set user_id to undefined.
  2. Set client_id to a new value - in google format XXX.YYY, where XXX is a random part and YYY is a unix timestamp.
  3. Clear the cookie.

If I don't do points 2 & 3 the subsequent events on the same device will still be associated with the same user in analytics.

DavidWells commented 1 year ago

Yeah looks like ga4 and ga3 plugin don't have a reset defined like so https://github.com/DavidWells/analytics/blob/0fb5860bf2b7e2c180dbc0462c66c6bd36331fde/packages/analytics-plugin-original-source/src/index.js#L40-L43 so there isn't any cleanup action happening in them. Only the libs values are getting reset.

We'd need a reset method defined in the plugin where u can run this logic. You can extend this in userland like so https://github.com/DavidWells/analytics/issues/67#issuecomment-664804311