Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates User Configuration and Turns off Statsig auto js error logging #57481

Merged
merged 6 commits into from Mar 27, 2024

Conversation

hannahbergam
Copy link
Contributor

@hannahbergam hannahbergam commented Mar 22, 2024

When Statsig is initialized, it defaults to logging all js errors. This means that now our integration is working and we're logging lots of errors! The options were being logged to the user object. This pointed to a bigger issue with the way we were planning to keep track of user data. On Amplitude, we send it through Current User Redux and that persists across a user's session. For Statsig, they strongly recommend sending the user in during the initialize() call and 'do not maintain or update user objects'. That led to this new solution:

  1. Load the user id and user type into the dom to be queried by the same query selector element
  2. If an id exists, populate a user object with the id and type, and send through to initialize
  3. If no id exists, send an empty user object

This way, the options parameter will always be the third parameter so flags like disableErrorLogging will work properly. A bonus: this now more closely matches our Statsig ruby implementation, where the user is sent in on initialize.

Additionally, I am updating the managed_test_server param to its own variable before sending it through, which I believe will enable our test server to send events.

Screenshot 2024-03-22 at 4 08 58 PM

These errors do not count toward our billable events (see images from slack conversation with Statsig directly below).
Screenshot 2024-03-22 at 12 29 23 PM
Screenshot 2024-03-22 at 12 35 51 PM

Links

Testing story

I was able to show that initializing with user data makes it so every page refresh has a user_id, not just when we have a login (and the single send from current user redux). See image below.

I was also able to verify that pegasus reporting still works as anticipated (I updated the amplitude event for 'page viewed' to send to statsig and it correctly sent with an empty user object). See second image below!

Screenshot 2024-03-22 at 7 40 35 PM Screenshot 2024-03-22 at 7 50 15 PM

Deployment strategy

Follow-up work

Privacy

Security

Caching

PR Checklist:

  • Tests provide adequate coverage
  • Privacy and Security impacts have been assessed
  • Code is well-commented
  • New features are translatable or updates will not break translations
  • Relevant documentation has been added or updated
  • User impact is well-understood and desirable
  • Pull Request is labeled appropriately
  • Follow-up work items (including potential tech debt) are tracked and linked

Copy link
Contributor

@bethanyaconnor bethanyaconnor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good based on the documentation!

@@ -12,6 +12,17 @@ const NO_EVENT_NAME = 'NO_VALID_EVENT_NAME_LOG_ERROR';

class StatsigReporter {
constructor() {
let user = {};
user_id_element = document.querySelector('script[data-user-id]');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll want to be careful about relying on this on cached pages, where it'll become out of date. The nice thing about using userRedux is that it's updated for every page load so it was always (eventually) up to date.

I'm curious if we can still use the user passed through from userRedux, but save that user id in this class so that we can send it with events? Or is there a race condition there that would make that difficult?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pair on this? It seems that the current user redux has been updating Amplitude on init(), which doesn't seem to get called for each page load.

I'm also surprised that a page would be cached through a change in user log out/sign in. This would make me think that some users could access data from a previously logged in user's cache. Is that the case or am I misunderstanding?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh weird -- it's probably not great if it doesn't get called on each page load 😬

On cached pages (and also I think on uncached pages as a result), we fetch the user data asyncronously, which then is fed into the redux store here. Most of our pages aren't cached, so in most cases, this solution is fine, but it's always possible we accidentally try to use this on one of those cached page and become confused.

@bethanyaconnor
Copy link
Contributor

Also can you update the title of this PR? It seems like this is a bit more than turning off JS error logging now :)

@hannahbergam hannahbergam changed the title Turning off statsig auto js error logging Updates User Configuration and Turns off Statsig auto js error logging Mar 25, 2024
@hannahbergam
Copy link
Contributor Author

Also can you update the title of this PR? It seems like this is a bit more than turning off JS error logging now :)

Ah yes- good call! Done

@@ -6,6 +6,7 @@
= render partial: 'i18n/crowdin_in_context_tool'
= render inline: File.read(Rails.root.join('..', 'shared', 'haml', 'onetrust_cookie_scripts.haml')), type: :haml, locals: {dashboard: true, domain: 'code.org'}
- appname = Rails.env.production? ? t(:appname) : "#{t(:appname)} [#{Rails.env}]"
- managed_test_server = "#{CDO.running_web_application? && CDO.test_system?}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you ever figure out why this was set wrong? (can totally be done in a separate PR if not)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I was injecting ruby in the correct way before so am abstracting it out into its own variable to match what else is happening in this file!

Copy link
Contributor

@bethanyaconnor bethanyaconnor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I'm convinced! Left a couple questions/nits but mostly for more comments. Thanks for sticking with this!

@hannahbergam hannahbergam merged commit 385b0a5 into staging Mar 27, 2024
2 checks passed
@hannahbergam hannahbergam deleted the hbergam/statsig-fix-forward branch March 27, 2024 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants