Skip to content

Commit

Permalink
update for NextJS routing use case
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Jun 24, 2024
1 parent 0520e90 commit 671505a
Showing 1 changed file with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions packages/experiment-tag/src/experiment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ExperimentUser,
Variant,
} from '@amplitude/experiment-js-client';
import mutate from 'dom-mutator';
import mutate, { MutationController } from 'dom-mutator';

import { WindowMessenger } from './messenger';
import {
Expand All @@ -18,6 +18,9 @@ import {
concatenateQueryParamsOf,
} from './util';

const appliedMutations: MutationController[] = [];
let previousUrl: string | undefined = undefined;

export const initializeExperiment = (apiKey: string, initialFlags: string) => {
WindowMessenger.setup();
const experimentStorageName = `EXP_${apiKey.slice(0, 10)}`;
Expand Down Expand Up @@ -82,6 +85,38 @@ export const initializeExperiment = (apiKey: string, initialFlags: string) => {

const variants = globalScope.experiment.all();

// add URL change listener
window.addEventListener('popstate', () => {
revertMutations();
applyVariants(globalScope.experiment.all());
});

(function (history) {
const pushState = history.pushState;
const replaceState = history.replaceState;

history.pushState = function (...args) {
previousUrl = window.location.href;
const result = pushState.apply(history, args);
window.dispatchEvent(new Event('popstate'));
return result;
};

history.replaceState = function (...args) {
previousUrl = window.location.href;
const result = replaceState.apply(history, args);
window.dispatchEvent(new Event('popstate'));
return result;
};
})(window.history);

applyVariants(variants);
}
};

const applyVariants = (variants) => {
const globalScope = getGlobalScope();
if (globalScope) {
for (const key in variants) {
const variant = variants[key];
const isWebExperimentation = variant.metadata?.deliveryMethod === 'web';
Expand Down Expand Up @@ -117,7 +152,7 @@ const handleRedirect = (action, key: string, variant: Variant) => {
const urlExactMatch = variant?.metadata?.['urlMatch'] as string[];
const currentUrl = urlWithoutParamsAndAnchor(globalScope.location.href);
const referrerUrl = urlWithoutParamsAndAnchor(
globalScope.document.referrer,
previousUrl || globalScope.document.referrer,
);
const redirectUrl = action?.data?.url;
// if in preview mode, strip query params
Expand Down Expand Up @@ -161,8 +196,16 @@ const handleMutate = (action, key: string, variant: Variant) => {

if (matchesUrl(urlExactMatch, currentUrl)) {
const mutations = action.data?.mutations;
mutations.forEach((m) => mutate.declarative(m));
mutations.forEach((m) => {
appliedMutations.push(mutate.declarative(m));
});
globalScope.experiment.exposure(key);
}
}
};

const revertMutations = () => {
while (appliedMutations.length > 0) {
appliedMutations.pop()?.revert();
}
};

0 comments on commit 671505a

Please sign in to comment.