-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add exit_intent trigger #246
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
base: web/page-triggers-1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,13 +6,15 @@ import { | |
| MessagePayloads, | ||
| ElementAppearedPayload, | ||
| ManualTriggerPayload, | ||
| ExitIntentPayload, | ||
| MessageType, | ||
| } from './message-bus'; | ||
| import { DebouncedMutationManager } from './mutation-manager'; | ||
| import { | ||
| ElementAppearedTriggerValue, | ||
| ElementVisibleTriggerValue, | ||
| ManualTriggerValue, | ||
| ExitIntentTriggerValue, | ||
| PageObject, | ||
| PageObjects, | ||
| } from './types'; | ||
|
|
@@ -41,6 +43,7 @@ export class SubscriptionManager { | |
| private elementVisibilityState: Map<string, boolean> = new Map(); | ||
| private elementAppearedState: Map<string, boolean> = new Map(); | ||
| private activeElementSelectors: Set<string> = new Set(); | ||
| private pageLoadTime: number = Date.now(); | ||
|
|
||
| constructor( | ||
| webExperimentClient: DefaultWebExperimentClient, | ||
|
|
@@ -66,6 +69,7 @@ export class SubscriptionManager { | |
| } | ||
| this.setupMutationObserverPublisher(); | ||
| this.setupVisibilityPublisher(); | ||
| this.setupExitIntentPublisher(); | ||
| this.setupPageObjectSubscriptions(); | ||
| this.setupUrlChangeReset(); | ||
| // Initial check for elements that already exist | ||
|
|
@@ -182,6 +186,7 @@ export class SubscriptionManager { | |
| this.messageBus.subscribe('url_change', () => { | ||
| this.elementAppearedState.clear(); | ||
| this.activeElementSelectors.clear(); | ||
| this.pageLoadTime = Date.now(); | ||
| const elementSelectors = this.getElementSelectors(); | ||
| elementSelectors.forEach((selector) => | ||
| this.activeElementSelectors.add(selector), | ||
|
|
@@ -373,6 +378,61 @@ export class SubscriptionManager { | |
| }); | ||
| }; | ||
|
|
||
| private setupExitIntentPublisher = () => { | ||
| // Get all page objects that use exit_intent trigger | ||
| const pages = Object.values(this.pageObjects).flatMap((pages) => | ||
| Object.values(pages).filter( | ||
| (page) => page.trigger_type === 'exit_intent', | ||
| ), | ||
| ); | ||
|
|
||
| if (pages.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| // Get minimum time requirement (use lowest value so listener activates earliest) | ||
| let minTimeOnPageMs = 0; | ||
| for (const page of pages) { | ||
| const triggerValue = page.trigger_value as ExitIntentTriggerValue; | ||
| minTimeOnPageMs = Math.min( | ||
| minTimeOnPageMs, | ||
| triggerValue.minTimeOnPageMs ?? 0, | ||
| ); | ||
| } | ||
|
|
||
| // Detect exit intent via mouse movement | ||
| const handleMouseLeave = (event: MouseEvent) => { | ||
| // Only trigger if: | ||
| // 1. Mouse Y position is near top of viewport (leaving towards browser chrome) | ||
| // 2. Mouse is leaving the document (relatedTarget is null) | ||
| // 3. Not already triggered | ||
| if ( | ||
| event.clientY <= 50 && // 50px from top | ||
| event.relatedTarget === null | ||
| ) { | ||
| this.messageBus.publish('exit_intent', { | ||
| durationMs: Date.now() - this.pageLoadTime, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| // Install listener after minimum time requirement | ||
| if (minTimeOnPageMs > 0) { | ||
| setTimeout(() => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These timeouts also need to be cancelled when user navigates to another page etc. It looks like you can already use
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noted - I will add this in a follow up PR so we can centralize cleanup logic across all triggers |
||
| this.globalScope.document.addEventListener( | ||
| 'mouseleave', | ||
| handleMouseLeave, | ||
| ); | ||
| }, minTimeOnPageMs); | ||
| } else { | ||
| // Install immediately if no time requirement | ||
| this.globalScope.document.addEventListener( | ||
| 'mouseleave', | ||
| handleMouseLeave, | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| private setupLocationChangePublisher = () => { | ||
| // Add URL change listener for back/forward navigation | ||
| this.globalScope.addEventListener('popstate', () => { | ||
|
|
@@ -501,6 +561,15 @@ export class SubscriptionManager { | |
| return this.elementVisibilityState.get(observerKey) ?? false; | ||
| } | ||
|
|
||
| case 'exit_intent': { | ||
| const durationMs = (message as ExitIntentPayload).durationMs; | ||
| const triggerValue = page.trigger_value as ExitIntentTriggerValue; | ||
| return ( | ||
| triggerValue.minTimeOnPageMs === undefined || | ||
| durationMs >= triggerValue.minTimeOnPageMs | ||
| ); | ||
| } | ||
|
|
||
| default: | ||
| return false; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minTimeOnPageMsalways resolves to0because it starts at0and usesMath.min(...). Consider initializing toInfinityand defaulting to0if none are set so configured delays take effect.