Skip to content

Commit

Permalink
disable analytics.page() when url tracking disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
ritch committed Sep 16, 2024
1 parent 38527cb commit 12dac08
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
27 changes: 26 additions & 1 deletion app/packages/analytics/src/analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ describe("Analytics", () => {
});
analytics.track("random_event", { uri: "@my_name/my_plugin/my_operator" });
// segment should be called with properties.uri = "<redacted>"
console.log(mockSegment.track.mock.calls[0]);
expect(mockSegment.track).toHaveBeenCalledWith(
"random_event",
{ uri: "<redacted>" },
Expand Down Expand Up @@ -195,4 +194,30 @@ describe("Analytics", () => {
const redacted4 = analytics.redact(undefined);
expect(redacted4).toEqual(undefined);
});

describe("analytics.page()", () => {
it("should call segment.page()", () => {
analytics = new Analytics();
analytics.load({
writeKey: "test",
userId: "user",
userGroup: "group",
debug: false,
});
analytics.page("my_page");
expect(mockSegment.page).toHaveBeenCalled();
});
it("should be a no-op if disableUrlTracking is set to true", () => {
analytics = new Analytics();
analytics.load({
writeKey: "test",
userId: "user",
userGroup: "group",
debug: false,
disableUrlTracking: true,
});
analytics.page("my_page");
expect(mockSegment.page).not.toHaveBeenCalled();
});
});
});
4 changes: 2 additions & 2 deletions app/packages/analytics/src/usingAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Analytics {
private _debug = false;
private _lastEventTimestamps: Record<string, number> = {}; // Tracks last event times
private _debounceInterval = 1000; // Default debounce interval in milliseconds (5 seconds)
private _disableUrlTracking = false;
private _disableUrlTracking = false; // Disable tracking page URL and .page() calls
private _redactedProperties: string[] = [];

constructor(config?: AnalyticsConfig) {
Expand Down Expand Up @@ -88,7 +88,7 @@ export class Analytics {
}

page(name?: string, properties?: {}) {
if (!this._segment) return;
if (!this._segment || this._disableUrlTracking) return;
properties = this.redact(properties);
this._segment.page(name, properties);
}
Expand Down

0 comments on commit 12dac08

Please sign in to comment.