Skip to content

fix(analytics-browser): prevent infinite Amplitude network requests #1100

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

daniel-graham-amplitude
Copy link
Collaborator

Summary

If network tracking is configured to include amplitude.com and allows status code 200, then it can cause infinite network request calls because it would trigger an Amplitude event, and then that would trigger a network request callback, which would trigger an Amplitude event...

This checks the events, and if it contains '[Amplitude] Network Request' as an event_type, then do not track it

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?: No

daniel-graham-amplitude and others added 16 commits May 13, 2025 13:25
 - @amplitude/[email protected]
 - @amplitude/[email protected]
 - @amplitude/[email protected]
 - @amplitude/[email protected]
 - @amplitude/[email protected]
 - @amplitude/[email protected]
 - @amplitude/plugin-page-view-tracking-browser@2.3.28-fetchhardeningxhrsupport.0
 - @amplitude/[email protected]
 - @amplitude/plugin-session-replay-react-native@0.4.2-fetchhardeningxhrsupport.0
 - @amplitude/[email protected]
 - @amplitude/[email protected]
 - @amplitude/[email protected]
@daniel-graham-amplitude daniel-graham-amplitude changed the base branch from main to AMP-125616/fetch-hardening-xhr-support May 23, 2025 01:30
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR prevents infinite loops in Amplitude network tracking by filtering out events that represent Amplitude’s own “[Amplitude] Network Request” calls. Key changes include:

  • Adding manual browser test pages for XHR/Axios and Fetch scenarios.
  • Extending shouldTrackNetworkEvent with a new isAmplitudeNetworkRequestEvent check and related tests.
  • Adjusting core types and exports to support the new request‐type and wrapper interfaces.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test-server/browser-sdk/xhr.html New manual test suite for XHR/Axios network tracking, including abort and various payload types
test-server/browser-sdk/fetch.html New manual test suite for Fetch network tracking with similar scenarios
packages/plugin-network-capture-browser/src/track-network-event.ts Implements amplitude‐loop detection via isAmplitudeNetworkRequestEvent and skips those events
packages/plugin-network-capture-browser/test/autocapture-plugin/track-network-event.test.ts Adds unit tests covering the new skip logic for “[Amplitude] Network Request” and URL‐missing cases
packages/analytics-core/src/types/browser-config.ts Updates networkTracking description to reflect detailed options
packages/analytics-core/src/index.ts Reorders exports to expose NetworkRequestEvent from the public API
packages/analytics-core/src/network-request-event.ts Introduces safe request/response wrappers and updated event serialization
packages/analytics-core/src/network-observer.ts Refactors fetch/XHR observers to use the new wrappers and centralized handling logic
Comments suppressed due to low confidence (1)

packages/plugin-network-capture-browser/test/autocapture-plugin/track-network-event.test.ts:284

  • [nitpick] Consider adding an end-to-end test for trackNetworkEvents itself to verify that requests flagged by shouldTrackNetworkEvent (e.g., Amplitude loop events) are actually filtered out in the emitted analytics stream.
test('network request body contains "[Amplitude] Network Request"', () => {

@@ -8,6 +8,7 @@ import {
import { filter } from 'rxjs';
import { AllWindowObservables, TimestampedEvent } from './network-capture-plugin';
import { AMPLITUDE_NETWORK_REQUEST_EVENT } from './constants';
import { IRequestWrapper } from '@amplitude/analytics-core/lib/esm/network-request-event';
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

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

[nitpick] Avoid deep-importing from internal paths (lib/esm/...). Instead, import IRequestWrapper from the public package entry point to reduce brittleness.

Suggested change
import { IRequestWrapper } from '@amplitude/analytics-core/lib/esm/network-request-event';
import { IRequestWrapper } from '@amplitude/analytics-core';

Copilot uses AI. Check for mistakes.

@@ -51,7 +52,10 @@ function isCaptureRuleMatch(rule: NetworkCaptureRule, hostname: string, status?:
return true;
}

function parseUrl(url: string) {
function parseUrl(url: string | undefined) {
Copy link
Preview

Copilot AI May 23, 2025

Choose a reason for hiding this comment

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

[nitpick] The name parseUrl suggests returning a standard URL object but actually returns a custom result. Consider renaming to extractUrlComponents or explicitly typing the return to improve clarity.

Suggested change
function parseUrl(url: string | undefined) {
interface UrlComponents {
query: string;
fragment: string;
href: string;
hrefWithoutQueryOrHash: string;
host: string;
}
function extractUrlComponents(url: string | undefined): UrlComponents | undefined {

Copilot uses AI. Check for mistakes.

Copy link

promptless bot commented May 23, 2025

✅ No documentation updates required.

@daniel-graham-amplitude daniel-graham-amplitude changed the title [AMP-125616] prevent infinite Amplitude network requests fix(analytics-browser): prevent infinite Amplitude network requests May 23, 2025
Copy link
Contributor

@Mercy811 Mercy811 left a comment

Choose a reason for hiding this comment

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

Should we ignore all requests to amplitude http v2 endpoint?

@daniel-graham-amplitude
Copy link
Collaborator Author

Should we ignore all requests to amplitude http v2 endpoint?

I think just requests that have [Amplitude] Network Request in their response. It's still useful for us, especially internally, to find errors from those endpoints.

Base automatically changed from AMP-125616/fetch-hardening-xhr-support to main May 27, 2025 18:30
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.

3 participants