Skip to content

Commit fe9afc7

Browse files
committed
send yt detection events
Made-with: Cursor
1 parent 9efca4f commit fe9afc7

4 files changed

Lines changed: 22 additions & 12 deletions

File tree

injected/integration-test/web-interference-detection-events.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test.describe('YouTube detection events via webInterferenceDetection', () => {
2727
(webEventMessages[0].payload).params;
2828
expect(params).toEqual({
2929
type: 'youtube_adBlocker',
30-
data: {},
30+
data: { loginState: 'unknown' },
3131
});
3232
});
3333

injected/src/detectors/detections/youtube-ad-detection.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ export class YouTubeAdDetector {
2929
/**
3030
* @param {YouTubeDetectorConfig} config - Configuration from privacy-config (required)
3131
* @param {{info: Function, warn: Function, error: Function}} [logger] - Optional logger from ContentFeature
32-
* @param {(type: string) => void} [onEvent] - Callback fired when a new detection occurs (may be async)
32+
* @param {(type: string, data?: Record<string, unknown>) => void} [onEvent] - Callback fired when a new detection occurs (may be async)
3333
*/
3434
constructor(config, logger, onEvent) {
3535
// Logger for debug output (only logs when debug mode is enabled)
3636
this.log = logger || noopLogger;
37-
/** @type {(type: string) => void} */
37+
/** @type {(type: string, data?: Record<string, unknown>) => void} */
3838
this.onEvent = onEvent || (() => {});
3939

4040
// All config comes from privacy-config
@@ -134,7 +134,11 @@ export class YouTubeAdDetector {
134134

135135
if (this.config.fireDetectionEvents?.[type]) {
136136
try {
137-
const result = /** @type {any} */ (this.onEvent(`youtube_${type}`));
137+
const result = /** @type {any} */ (
138+
this.onEvent(`youtube_${type}`, {
139+
loginState: this.state.loginState?.state || 'unknown',
140+
})
141+
);
138142
if (result && typeof result.catch === 'function') {
139143
// eslint-disable-next-line promise/prefer-await-to-then
140144
result.catch(() => {});

injected/src/features/web-interference-detection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ export default class WebInterferenceDetection extends ContentFeature {
2020
// Get settings with conditionalChanges already applied by framework
2121
const settings = this.getFeatureSetting('interferenceTypes');
2222

23-
const fireEvent = async (type) => {
23+
const fireEvent = async (type, data) => {
2424
try {
25-
const result = await this.callFeatureMethod('webEvents', 'fireEvent', { type });
25+
const result = await this.callFeatureMethod('webEvents', 'fireEvent', { type, data });
2626
if (result instanceof CallFeatureMethodError && this.isDebug) {
2727
this.log.warn('webEvents.fireEvent failed:', result.message);
2828
}

injected/unit-test/youtube-ad-detection.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,31 @@ const configWithAllEvents = {
2626

2727
describe('YouTubeAdDetector', () => {
2828
describe('onEvent callback', () => {
29-
it('calls onEvent with youtube_ prefix when a new detection occurs', () => {
29+
it('calls onEvent with youtube_ prefix and loginState when a new detection occurs', () => {
3030
const events = [];
31-
const detector = new YouTubeAdDetector(configWithAllEvents, undefined, (type) => events.push(type));
31+
const detector = new YouTubeAdDetector(configWithAllEvents, undefined, (type, data) => events.push({ type, data }));
3232

3333
detector.reportDetection('adBlocker');
3434

35-
expect(events).toEqual(['youtube_adBlocker']);
35+
expect(events).toEqual([{ type: 'youtube_adBlocker', data: { loginState: 'unknown' } }]);
3636
});
3737

38-
it('fires for each detection type', () => {
38+
it('fires for each detection type with loginState', () => {
3939
const events = [];
40-
const detector = new YouTubeAdDetector(configWithAllEvents, undefined, (type) => events.push(type));
40+
const detector = new YouTubeAdDetector(configWithAllEvents, undefined, (type, data) => events.push({ type, data }));
4141

4242
detector.reportDetection('videoAd');
4343
detector.reportDetection('playabilityError', { message: 'error' });
4444
detector.reportDetection('adBlocker');
4545
detector.reportDetection('staticAd');
4646

47-
expect(events).toEqual(['youtube_videoAd', 'youtube_playabilityError', 'youtube_adBlocker', 'youtube_staticAd']);
47+
expect(events.map((e) => e.type)).toEqual([
48+
'youtube_videoAd',
49+
'youtube_playabilityError',
50+
'youtube_adBlocker',
51+
'youtube_staticAd',
52+
]);
53+
events.forEach((e) => expect(e.data).toEqual({ loginState: 'unknown' }));
4854
});
4955

5056
it('does not fire for duplicate detections', () => {

0 commit comments

Comments
 (0)