Load the Snowplow tracker only when events can be sent
What does this MR do and why?
Every page loads the Snowplow tracker script (about 31 KB gzipped, per the issue), even when the frontend can never send an event. Now the page only loads it when events can be sent.
The script still loads when any of these is true:
- Snowplow is enabled in the Admin area.
- Product usage data is enabled.
- There are Duo events to forward. EE builds always have some.
- The instance runs in development, or with Snowplow Micro.
So in practice, only FOSS instances with Snowplow and product usage data both off stop loading it. GitLab.com and EE instances don't change, so the saving is smaller than the issue describes.
The issue suggested checking whether the frontend tracking options are present. I couldn't use that, because they are never empty. With Snowplow off, they still point to the /-/collect_events forwarder.
When the script isn't loaded, the page still sets the tracking options. The frontend already checks whether tracking is enabled before it sends anything, so I didn't need JavaScript changes. I kept the tracker in the frontend list of always-loaded bundles, next to the conditional Sentry and performance bar bundles.
I didn't add a feature flag. On GitLab.com and every EE build the condition is always true, so a flag wouldn't change anything there.
I added tests for each case of the condition, and for the page with and without the script. In pipeline 2933370470, rspec unit predictive ran them with the other affected specs: 238 examples, 0 failures. haml-lint and rubocop passed there too. Locally I only ran ruby -c on the changed Ruby files. I couldn't check the page by hand because I don't have GDK.
I have three questions:
- Is this the right condition?
- EE instances with self-hosted AI models don't send Duo events either, but the script still loads there. Should I skip it for them too?
- Do you want a feature flag anyway?
References
- Resolves #604086
Screenshots or screen recordings
Not applicable, there are no UI changes.
How to set up and validate locally
In development, Snowplow Micro is always on, so GDK always loads the tracker. You can still check the inputs of the condition:
-
Disable Snowplow in the Admin area, and set
GITLAB_PRODUCT_USAGE_DATA_ENABLED=false. -
Run GDK as FOSS (
FOSS_ONLY=1), and open the Rails console. -
Check the two eligibility values:
Gitlab::Tracking::EventEligibilityChecker.only_send_duo_events? # => true Gitlab::Tracking::EventEligibilityChecker.internal_duo_events # => []
Outside development, these two values mean the page doesn't load the tracker.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.
Implemented with AI assistance; I reviewed and tested the changes.