Skip to content
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

TDS (blocklist) override via A/B test #2906

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions shared/js/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ settings.ready().then(() => {
});

const remoteConfig = new RemoteConfig({ settings });
const tds = new TDSStorage({ settings, remoteConfig });
const abnMetrics = BUILD_TARGET !== 'firefox' ? new AbnExperimentMetrics({ remoteConfig }) : null;
const tds = new TDSStorage({ settings, remoteConfig, abnMetrics });
const devtools = new Devtools({ tds });
/**
* @type {{
Expand All @@ -72,12 +73,11 @@ const components = {
debugger: new DebuggerConnection({ tds, devtools }),
devtools,
remoteConfig,
abnMetrics,
};

// Chrome-only components
if (BUILD_TARGET === 'chrome' || BUILD_TARGET === 'chrome-mv2') {
const abnMetrics = new AbnExperimentMetrics({ remoteConfig });
components.abnMetrics = abnMetrics;
components.metrics = [new AppUseMetric({ abnMetrics }), new SearchMetric({ abnMetrics }), new PixelMetric({ abnMetrics })];
components.fireButton = new FireButton({ settings, tabManager });
}
Expand Down
60 changes: 57 additions & 3 deletions shared/js/background/components/tds.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import ResourceLoader from './resource-loader.js';
import constants from '../../../data/constants';
import { generateRetentionMetrics } from './abn-experiments.js';

/**
* @typedef {import('../settings.js')} Settings
* @typedef {import('./remote-config.js').default} RemoteConfig
* @typedef {import('./abn-experiments.js').default} AbnExperimentMetrics
*/

const TDS_OVERRIDE_SETTINGS_KEY = 'tdsOverride';
const CONTENT_BLOCKING = 'contentBlocking';

export default class TDSStorage {
/**
* @param {{
* settings: Settings,
* remoteConfig: RemoteConfig
* remoteConfig: RemoteConfig,
* abnMetrics: AbnExperimentMetrics?
* }} opts
*/
constructor({ settings, remoteConfig }) {
constructor({ settings, remoteConfig, abnMetrics }) {
this.settings = settings;
this.remoteConfig = remoteConfig;
this.abnMetrics = abnMetrics;
/** @deprecated config is an alias of remoteConfig */
this.config = this.remoteConfig;
this.surrogates = new ResourceLoader(
Expand All @@ -28,14 +36,60 @@ export default class TDSStorage {
this.tds = new ResourceLoader(
{
name: 'tds',
remoteUrl: constants.tdsLists[1].url,
remoteUrl: this.getTDSUrl.bind(this),
updateIntervalMinutes: 15,
},
{ settings },
);
this.remoteConfig.onUpdate(this.checkShouldOverrideTDS.bind(this));
}

ready() {
return Promise.all([this.tds.ready, this.surrogates.ready, this.remoteConfig.ready]);
}

async getTDSUrl() {
await this.config.ready;
const overridePath = this.settings.getSetting(TDS_OVERRIDE_SETTINGS_KEY);
if (overridePath) {
return `https://staticcdn.duckduckgo.com/trackerblocking/${overridePath}`;
}
return constants.tdsLists[1].url;
}

checkShouldOverrideTDS() {
const contentBlockingSubFeatures = this.config.config?.features[CONTENT_BLOCKING].features || {};
const enabledBlocklistOverrides = Object.keys(contentBlockingSubFeatures).filter(
(k) => k.startsWith('TDS') && this.config.isSubFeatureEnabled(CONTENT_BLOCKING, k),
);
if (enabledBlocklistOverrides.length > 0 && this.abnMetrics) {
const subFeatureName = enabledBlocklistOverrides[0];
const overrideSubFeature = contentBlockingSubFeatures[subFeatureName];
// If this is enabled via an experiment, the override URL is defined as `${cohortName}Url`, otherwise, for a normal rollout use `nextUrl`.
const settingsKey = `${this.remoteConfig.getCohortName(CONTENT_BLOCKING, subFeatureName) || 'next'}Url`;
const overridePath = overrideSubFeature.settings && overrideSubFeature.settings[settingsKey];
if (!overridePath) {
console.warn(`Couldn't find TDS override path in subfeature settings.`);
return;
}
if (overridePath !== this.settings.getSetting(TDS_OVERRIDE_SETTINGS_KEY)) {
console.log('TDS URL override changed to ', overridePath);
this.settings.updateSetting(TDS_OVERRIDE_SETTINGS_KEY, overridePath);
this.tds.checkForUpdates(true);
}
this.abnMetrics.markExperimentEnrolled(CONTENT_BLOCKING, subFeatureName, [
...generateRetentionMetrics(),
{
metric: 'brokenSiteReport',
conversionWindowStart: 0,
conversionWindowEnd: 14,
value: 1,
},
]);
} else if (this.settings.getSetting(TDS_OVERRIDE_SETTINGS_KEY)) {
// User removed from experiment/rollout, reset TDS override and fetch default list
this.settings.removeSetting(TDS_OVERRIDE_SETTINGS_KEY);
this.tds.checkForUpdates(true);
}
}
}
Loading