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

chore: Remove '$web_experiment_applied' event #1653

Merged
merged 1 commit into from
Jan 15, 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
25 changes: 3 additions & 22 deletions src/__tests__/web-experiments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,27 +286,15 @@ describe('Web Experimentation', () => {
}

assertElementChanged('Signup', 'innerText', 'Sign me up')
expect(posthog.capture).toHaveBeenCalledWith('$web_experiment_applied', {
$web_experiment_document_url:
'https://example.com/landing-page?utm_campaign=marketing&utm_medium=mobile',
$web_experiment_elements_modified: 1,
$web_experiment_name: 'Signup button test',
$web_experiment_variant: 'Signup',
})
expect(posthog.capture).not.toHaveBeenCalled()
})

it('makes no modifications if control variant', () => {
experimentsResponse = {
experiments: [signupButtonWebExperimentWithFeatureFlag],
}
assertElementChanged('control', 'innerText', 'original')
expect(posthog.capture).toHaveBeenCalledWith('$web_experiment_applied', {
$web_experiment_document_url:
'https://example.com/landing-page?utm_campaign=marketing&utm_medium=mobile',
$web_experiment_elements_modified: 0,
$web_experiment_name: 'Signup button test',
$web_experiment_variant: 'control',
})
expect(posthog.capture).not.toHaveBeenCalled()
})

it('can render previews based on URL params', () => {
Expand All @@ -327,14 +315,7 @@ describe('Web Experimentation', () => {

WebExperiments.getWindowLocation = original
expect(elParent.innerText).toEqual('Sign me up')
expect(posthog.capture).toHaveBeenCalledWith('$web_experiment_applied', {
$web_experiment_document_url:
'https://example.com/landing-page?__experiment_id=3&__experiment_variant=Signup',
$web_experiment_elements_modified: 1,
$web_experiment_name: 'Signup button test',
$web_experiment_variant: 'Signup',
$web_experiment_preview: true,
})
expect(posthog.capture).not.toHaveBeenCalled()
})

it('can set css of Span Element', async () => {
Expand Down
32 changes: 2 additions & 30 deletions src/web-experiments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ export class WebExperiments {
this.applyTransforms(
previewExperiments[0].name,
variant,
previewExperiments[0].variants[variant].transforms,
true
previewExperiments[0].variants[variant].transforms
)
}
}
Expand Down Expand Up @@ -238,29 +237,14 @@ export class WebExperiments {
logger.info(`[WebExperiments] ${msg}`, args)
}

private applyTransforms(
experiment: string,
variant: string,
transforms: WebExperimentTransform[],
isPreview?: boolean
) {
private applyTransforms(experiment: string, variant: string, transforms: WebExperimentTransform[]) {
if (this._is_bot()) {
WebExperiments.logInfo('Refusing to render web experiment since the viewer is a likely bot')
return
}

if (variant === 'control') {
WebExperiments.logInfo('Control variants leave the page unmodified.')
if (this.instance && this.instance.capture) {
this.instance.capture('$web_experiment_applied', {
$web_experiment_name: experiment,
$web_experiment_preview: isPreview,
$web_experiment_variant: variant,
$web_experiment_document_url: WebExperiments.getWindowLocation()?.href,
$web_experiment_elements_modified: 0,
})
}

return
}

Expand All @@ -271,12 +255,10 @@ export class WebExperiments {
transform
)

let elementsModified = 0
// eslint-disable-next-line no-restricted-globals
const elements = document?.querySelectorAll(transform.selector)
elements?.forEach((element) => {
const htmlElement = element as HTMLElement
elementsModified += 1
if (transform.attributes) {
transform.attributes.forEach((attribute) => {
switch (attribute.name) {
Expand Down Expand Up @@ -314,16 +296,6 @@ export class WebExperiments {
htmlElement.setAttribute('style', transform.css)
}
})

if (this.instance && this.instance.capture) {
this.instance.capture('$web_experiment_applied', {
$web_experiment_name: experiment,
$web_experiment_variant: variant,
$web_experiment_preview: isPreview,
$web_experiment_document_url: WebExperiments.getWindowLocation()?.href,
$web_experiment_elements_modified: elementsModified,
})
}
}
})
}
Expand Down
Loading