Use cached monthly commitment for credits cart abandonment

What does this MR do and why?

POST /-/gitlab_subscriptions/hand_raise_leads/track_cart_abandonment with a credits param schedules CartAbandonmentCreditsPurchaseWorker 3 hours out. credits is the namespace's current monthly commitment, used as the baseline to detect a purchase. It comes straight from the request, and the worker's dedup key includes it, so sending a different value on each request schedules a new job every time. Each job also calls CustomersDot when it runs.

This MR stops trusting the request value:

  • FetchMonthlyCommitmentService: it takes a new cache: option. The default, nil, doesn't touch the cache. :read reads the cached value for the namespace and fetches from CustomersDot on a miss. :refresh always fetches and overwrites the cached value. Any other value raises ArgumentError. Only successful fetches are cached (1 hour), and client errors are raised in every mode. An unsuccessful CustomersDot response is now returned as an error, still with total_credits: 0 in the payload, instead of a success with 0 that looks like a real 0. The worker and the two components that already call the service keep the default, so their behavior doesn't change.
  • Billing page: it already fetches the monthly commitment from CustomersDot on every load. It now uses cache: :refresh, so each load caches a fresh value for the namespace.
  • Tracking request: it ignores the credits value and calls the service with cache: :read. If CustomersDot fails or the client raises, the tracking service handles the error, nothing is cached, and no job is scheduled.

The worker and the dedup key (user_id, namespace_id, credits) are unchanged. Since credits now comes from the server, one user can only schedule one job per namespace per real commitment value, and the tracking endpoint makes at most about one CustomersDot call per namespace per hour.

Everything is behind the cart_abandonment_server_side_credits gitlab_com_derisk flag, checked with the loaded namespace record. With the flag off, nothing changes.

Why not the earlier approaches?
  • Validate credits against allowed amounts: credits isn't a purchase amount. It's the namespace's running total, which can be any number, and checking the exact value means a CustomersDot call on every request.
  • Drop credits from the dedup key (this MR's first version): it stopped the flood, but a user who bought and then abandoned a second purchase within the same 3-hour window lost that lead. It would also have deduplicated serial test runs. Discussed in https://gitlab.com/gitlab-org/gitlab/-/work_items/637422#note_4169999116.

References

Screenshots or screen recordings

Not applicable: backend-only change with no UI.

How to set up and validate locally

  1. Run GDK in SaaS mode (GITLAB_SIMULATE_SAAS=1) and use a free or trial top-level group you own.

  2. In the Rails console, opt in and enable the flag for the group:

    user = User.find_by_username('root')
    user.update!(onboarding_status_email_opt_in: true)
    group = Group.find_by_full_path('<your-group>')
    Feature.enable(:cart_abandonment_server_side_credits, group)
  3. Visit the group's Settings > Billing page, then check that the commitment was cached:

    Rails.cache.read("gitlab_subscriptions:monthly_commitment:#{group.id}")
  4. In the browser console on any GitLab page, send tracking requests with different credits values:

    const t = document.querySelector('meta[name=csrf-token]').content;
    for (let i = 1; i <= 5; i++)
      fetch(`/-/gitlab_subscriptions/hand_raise_leads/track_cart_abandonment?namespace_id=<group-id>&credits=${i}`,
            { method: 'POST', headers: { 'X-CSRF-Token': t } });
  5. Count the scheduled jobs. Expect 1 with the flag enabled, and 5 after Feature.disable(:cart_abandonment_server_side_credits) and a cleanup:

    w = GitlabSubscriptions::CartAbandonmentCreditsPurchaseWorker
    Sidekiq::ScheduledSet.new.count { |j| j.klass == w.name }
    # cleanup
    Sidekiq::ScheduledSet.new.select { |j| j.klass == w.name }.each(&:delete)

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.

Edited by Marco Tomasi

Merge request reports

Loading
Loading