-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add time_on_page trigger #242
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
8a5c87b
3fe4ab7
6e556ee
c6ed9a4
d7b8418
945bbbb
91f88e1
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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { | |
| ElementAppearedPayload, | ||
| ManualTriggerPayload, | ||
| MessageType, | ||
| TimeOnPagePayload, | ||
| } from './message-bus'; | ||
| import { DebouncedMutationManager } from './mutation-manager'; | ||
| import { | ||
|
|
@@ -15,6 +16,7 @@ import { | |
| ManualTriggerValue, | ||
| PageObject, | ||
| PageObjects, | ||
| TimeOnPageTriggerValue, | ||
| } from './types'; | ||
|
|
||
| const evaluationEngine = new EvaluationEngine(); | ||
|
|
@@ -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 timeOnPageTimeouts: Set<ReturnType<typeof setTimeout>> = new Set(); | ||
|
|
||
| constructor( | ||
| webExperimentClient: DefaultWebExperimentClient, | ||
|
|
@@ -67,9 +70,10 @@ export class SubscriptionManager { | |
| this.setupMutationObserverPublisher(); | ||
| this.setupVisibilityPublisher(); | ||
| this.setupPageObjectSubscriptions(); | ||
| this.setupUrlChangeReset(); | ||
| // Initial check for elements that already exist | ||
| this.checkInitialElements(); | ||
| this.setUpTimeOnPagePublisher(); | ||
| this.setupUrlChangeReset(); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -188,6 +192,7 @@ export class SubscriptionManager { | |
| ); | ||
| this.setupVisibilityPublisher(); | ||
| this.checkInitialElements(); | ||
| this.setUpTimeOnPagePublisher(); | ||
| }); | ||
| }; | ||
|
|
||
|
|
@@ -413,6 +418,28 @@ export class SubscriptionManager { | |
| wrapHistoryMethods(); | ||
| }; | ||
|
|
||
| setUpTimeOnPagePublisher = () => { | ||
| // Clear any existing timeouts first | ||
| this.timeOnPageTimeouts.forEach((timeoutId) => clearTimeout(timeoutId)); | ||
| this.timeOnPageTimeouts.clear(); | ||
| // Publish initial time_on_page event | ||
| this.messageBus.publish('time_on_page'); | ||
|
|
||
| for (const pages of Object.values(this.pageObjects)) { | ||
| for (const page of Object.values(pages)) { | ||
| if (page.trigger_type === 'time_on_page') { | ||
| const triggerValue = page.trigger_value as TimeOnPageTriggerValue; | ||
| const durationMs = triggerValue.durationMs; | ||
| const timeoutId = setTimeout(() => { | ||
|
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.
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.
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.
IOW, clear timeout when the current tab is hidden, and restart |
||
| this.messageBus.publish('time_on_page', { durationMs }); | ||
| this.timeOnPageTimeouts.delete(timeoutId); | ||
| }, durationMs); | ||
| this.timeOnPageTimeouts.add(timeoutId); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| private isPageObjectActive = <T extends MessageType>( | ||
| page: PageObject, | ||
| message: MessagePayloads[T], | ||
|
|
@@ -501,6 +528,12 @@ export class SubscriptionManager { | |
| return this.elementVisibilityState.get(observerKey) ?? false; | ||
| } | ||
|
|
||
| case 'time_on_page': { | ||
| const triggerValue = page.trigger_value as TimeOnPageTriggerValue; | ||
| const triggerPayload = message as TimeOnPagePayload; | ||
| return triggerPayload.durationMs >= triggerValue.durationMs; | ||
| } | ||
|
|
||
| default: | ||
| return false; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.