Skip to content

Instantly share code, notes, and snippets.

@piatra
Last active May 28, 2021 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piatra/fb3876257f876386da104df593000ce9 to your computer and use it in GitHub Desktop.
Save piatra/fb3876257f876386da104df593000ce9 to your computer and use it in GitHub Desktop.
Force enrollment snippet
// copy paste the enroll.js gist into the Browser Toolbox
ForceEnrollment.optInToExperiment({slug: "onboarding-modal-experiment", branch: "treatment"})
// optionally also `collection: "nimbus-preview"`
XPCOMUtils.defineLazyGetter(this, "log", () => {
const { Logger } = ChromeUtils.import(
"resource://messaging-system/lib/Logger.jsm"
);
return new Logger("RSLoader");
});
const { ExperimentManager } = ChromeUtils.import(
"resource://nimbus/lib/ExperimentManager.jsm"
);
const { RemoteSettings } = ChromeUtils.import(
"resource://services-settings/remote-settings.js"
);
const { NormandyUtils } = ChromeUtils.import(
"resource://normandy/lib/NormandyUtils.jsm"
);
XPCOMUtils.defineLazyPreferenceGetter(
this,
"COLLECTION_ID",
"messaging-system.rsexperimentloader.collection_id",
"nimbus-desktop-experiments"
);
let ForceEnrollment = {
optInToExperiment: async ({ slug, branch: branchSlug, collection }) => {
log.debug(`Attempting force enrollment with ${slug} / ${branchSlug}`);
let recipes;
try {
recipes = await RemoteSettings(collection || COLLECTION_ID).get();
} catch (e) {
Cu.reportError(e);
throw new Error("Error getting recipes from remote settings.");
}
let recipe = recipes.find(r => r.slug === slug);
if (!recipe) {
throw new Error(
`Could not find experiment slug ${slug} in collection ${collection ||
COLLECTION_ID}.`
);
}
let branch = recipe.branches.find(b => b.slug === branchSlug);
if (!branch) {
throw new Error(`Could not find branch slug ${branchSlug} in ${slug}.`);
}
ForceEnrollment.forceEnroll(recipe, branch);
},
forceClientId: async ({ slug: experimentSlug, branches }, optinBranch) => {
let id = NormandyUtils.generateUuid();
log.debug(`Looking for client_id to enroll in optinBranch ${optinBranch}`);
while (true) {
const { slug } = await ExperimentManager.chooseBranch(
experimentSlug,
branches,
id
);
if (slug === optinBranch) {
break;
}
id = NormandyUtils.generateUuid();
}
log.debug(`Updated normandy id to ${id} for ${optinBranch}`);
Services.prefs.setStringPref("app.normandy.user_id", id);
},
forceEnroll: async (recipe, branch, source = "force-enrollment") => {
let experiment = ExperimentManager.store.getExperimentForFeature(
branch.feature?.featureId
);
if (experiment) {
log.debug(
`Existing experiment found for the same feature ${branch?.feature.featureId}, unenrolling.`
);
ExperimentManager.unenroll(experiment.slug, source);
}
recipe.userFacingName = `${recipe.userFacingName} - Forced enrollment`;
recipe.bucketConfig.start = 0;
recipe.bucketConfig.count = 100;
recipe.bucketConfig.total = 100;
recipe.slug = `optin-${recipe.slug}-${Math.random()}`;
await ForceEnrollment.forceClientId(recipe, branch.slug);
return ExperimentManager.enroll(recipe, source);
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment