Skip to content

Commit c83503c

Browse files
refactor(pixel): use consent capability queries in Pixel
Replace 5 consent string comparisons in pixel.ts with calls to consentAllowsTracking() from core: - init(): level !== 'none' → consentAllowsTracking(level) - setConsent(): level !== 'none' → consentAllowsTracking(level) - startCmpDetection() onUpdate: level !== 'none' → consentAllowsTracking - startCmpDetection() onDetected: detector.level !== 'none' → same - canTrack(): level !== 'none' → consentAllowsTracking(level) The earlier version of pixel.ts had userId/identify checks that needed includesUserId() and canIdentify(), but those were removed in PR #2846 (pixel identify removal). Only canTrack() is needed now. Update pixel.test.ts mock to include the canTrack export. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e3aceea commit c83503c

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

packages/audience/pixel/src/pixel.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ jest.mock('@imtbl/audience-core', () => ({
7272
};
7373
},
7474
),
75+
canTrack: (level: string) => level !== 'none',
7576
}));
7677

7778
// Mock fetch globally

packages/audience/pixel/src/pixel.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
collectAttribution,
2323
getOrCreateSession,
2424
createConsentManager,
25+
canTrack as consentAllowsTracking,
2526
startCmpDetection,
2627
} from '@imtbl/audience-core';
2728
import { setupAutocapture } from './autocapture';
@@ -124,7 +125,7 @@ export class Pixel {
124125
if (isAutoConsent) {
125126
// CMP detection will fire the deferred page view when consent upgrades.
126127
this.startCmpDetection();
127-
} else if (this.consent.level !== 'none') {
128+
} else if (consentAllowsTracking(this.consent.level)) {
128129
// Static consent — fire page view immediately.
129130
this.initialPageViewFired = true;
130131
this.page();
@@ -168,7 +169,7 @@ export class Pixel {
168169
// Fire the deferred page view if consent was upgraded from 'none'
169170
// (covers the case where CMP detection failed and the caller
170171
// manually sets consent as a fallback).
171-
if (level !== 'none' && !this.initialPageViewFired) {
172+
if (consentAllowsTracking(level) && !this.initialPageViewFired) {
172173
this.initialPageViewFired = true;
173174
this.page();
174175
}
@@ -200,7 +201,7 @@ export class Pixel {
200201
this.consent!.setLevel(level);
201202

202203
// Fire the deferred page view on first consent upgrade from 'none'.
203-
if (level !== 'none' && !this.initialPageViewFired) {
204+
if (consentAllowsTracking(level) && !this.initialPageViewFired) {
204205
this.initialPageViewFired = true;
205206
this.page();
206207
}
@@ -210,7 +211,7 @@ export class Pixel {
210211
onCmpUpdate,
211212
(detector: CmpDetector) => {
212213
// CMP found — apply the initial consent level it reported.
213-
if (detector.level !== 'none') {
214+
if (consentAllowsTracking(detector.level)) {
214215
onCmpUpdate(detector.level);
215216
}
216217
},
@@ -338,7 +339,7 @@ export class Pixel {
338339
// -- Guards -------------------------------------------------------------
339340

340341
private canTrack(): boolean {
341-
return this.isReady() && this.consent!.level !== 'none';
342+
return this.isReady() && consentAllowsTracking(this.consent!.level);
342343
}
343344

344345
private isReady(): boolean {

0 commit comments

Comments
 (0)