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

Add Comments #42

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
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
60 changes: 55 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
* governing permissions and limitations under the License.
*/

/**
* Set isDebugEnabled accordingly based on the URL and plugin options.
* @param {Object} url window.location
* @param {Object} pluginOptions with default options and custom options
* @returns {Boolean} whether debug mode is enabled
*/
let isDebugEnabled;
export function setDebugMode(url, pluginOptions) {
const { host, hostname, origin } = url;
Expand All @@ -29,6 +35,12 @@
}
}

/**
* Default options for the plugin.
* could be extended and overridden by passing custom options
* e.g. 'abtest' instead of 'experiment'
* by passing { experimentsMetaTagPrefix: 'abtest' } in custom options
*/
export const DEFAULT_OPTIONS = {

// Audiences related properties
Expand Down Expand Up @@ -199,10 +211,11 @@
}

/**
* Replaces element with content from path
* @param {String} path
* @param {HTMLElement} el
* @return Returns the path that was loaded or null if the loading failed
* Replaces the inner content of an element with content from a specified path.
* @param {String} path - the url path to load the content from
* @param {HTMLElement} el - the element to replace the content of
* @param {String} selector - taget CSS selector for fragment replacement
* @return The path that was loaded or null if the loading failed
*/
async function replaceInner(path, el, selector) {
try {
Expand Down Expand Up @@ -516,6 +529,7 @@
}
}));

// Fragment-level modifications
if (pageMetadata.manifest) {
let entries = await getManifestEntriesForCurrentPage(pageMetadata.manifest);
if (entries) {
Expand All @@ -537,6 +551,12 @@
return configs;
}

/**
* Aggregates the entries into a single object.
* @param {String} type experiment, campaign or audience
* @param {String[]} allowedMultiValuesProperties properties that can have multiple values
* @returns object with the aggregated entries
*/
function aggregateEntries(type, allowedMultiValuesProperties) {
return (entries) => entries.reduce((aggregator, entry) => {
Object.entries(entry).forEach(([key, value]) => {
Expand Down Expand Up @@ -672,7 +692,9 @@
}

/**
* Parses the campaign manifest.
* Parses the experiemnt manifest
* @param {Object} entries fragment manifest entries
* @returns {Object} parsed fragment manifest that grouped by experiment
*/
function parseExperimentManifest(entries) {
return Object.values(Object.groupBy(
Expand All @@ -687,6 +709,12 @@
: null;
}

/**
* Runs the experiment on page/section/fragment.
* Page and Section level modifications are applied immediately,
* while fragment level modifications will be watched by observer.
* And dispatches custom events for further handling if needed.
*/
async function runExperiment(document, pluginOptions) {
return applyAllModifications(
pluginOptions.experimentsMetaTagPrefix,
Expand Down Expand Up @@ -790,6 +818,12 @@
: null;
}

/**
* Runs the campaign on page/section/fragment.
* Page and Section level modifications are applied immediately,
* while fragment level modifications will be watched by observer.
* And dispatches custom events for further handling if needed.
*/
async function runCampaign(document, pluginOptions) {
return applyAllModifications(
pluginOptions.campaignsMetaTagPrefix,
Expand Down Expand Up @@ -872,6 +906,12 @@
: null;
}

/**
* Serves the audience on page/section/fragment.
* Page and Section level modifications are applied immediately,
* while fragment level modifications will be watched by observer.
* And dispatches custom events for further handling if needed.
*/
async function serveAudience(document, pluginOptions) {
document.body.dataset.audiences = Object.keys(pluginOptions.audiences).join(',');
return applyAllModifications(
Expand Down Expand Up @@ -901,6 +941,11 @@
);
}

/**
* Loads and initializes plugins and functionalities eagerly,
* @param {Document} document The HTML document to be processed
* @param {Object} [options={}] Custom options passed from customer
*/
export async function loadEager(document, options = {}) {
const pluginOptions = { ...DEFAULT_OPTIONS, ...options };
setDebugMode(window.location, pluginOptions);
Expand All @@ -916,14 +961,19 @@
ns.campaign = ns.campaigns.find((e) => e.type === 'page');
}

/**
* Loads the experimentation pill when debug mode is enabled
*/
export async function loadLazy(document, options = {}) {
const pluginOptions = { ...DEFAULT_OPTIONS, ...options };
// do not show the experimentation pill on prod domains
if (!isDebugEnabled) {
return;
}
// fetch the central-host preview script from github pages

Check warning on line 973 in src/index.js

View check run for this annotation

Codecov / codecov/patch

src/index.js#L973

Added line #L973 was not covered by tests
// eslint-disable-next-line import/no-unresolved
const preview = await import('https://opensource.adobe.com/aem-experimentation/preview.js');
// functions to be passed to the preview script
const context = {
getMetadata,
toClassName,
Expand Down
Loading