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

feat: Add prev pageview duration #1348

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
95 changes: 53 additions & 42 deletions src/page-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isUndefined } from './utils/type-utils'

interface PageViewEventProperties {
$prev_pageview_pathname?: string
$prev_pageview_duration?: number // seconds
$prev_pageview_last_scroll?: number
$prev_pageview_last_scroll_percentage?: number
$prev_pageview_max_scroll?: number
Expand All @@ -16,72 +17,82 @@ interface PageViewEventProperties {

export class PageViewManager {
_currentPath?: string
_prevPageviewTimestamp?: Date
_instance: PostHog

constructor(instance: PostHog) {
this._instance = instance
}

doPageView(): PageViewEventProperties {
const response = this._previousScrollProperties()
doPageView(timestamp: Date): PageViewEventProperties {
const response = this._previousPageViewProperties(timestamp)

// On a pageview we reset the contexts
this._currentPath = window?.location.pathname ?? ''
this._instance.scrollManager.resetContext()
this._prevPageviewTimestamp = timestamp

return response
}

doPageLeave(): PageViewEventProperties {
return this._previousScrollProperties()
doPageLeave(timestamp: Date): PageViewEventProperties {
return this._previousPageViewProperties(timestamp)
}

private _previousScrollProperties(): PageViewEventProperties {
private _previousPageViewProperties(timestamp: Date): PageViewEventProperties {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably easier to view this diff without whitespace changes

const previousPath = this._currentPath
const previousTimestamp = this._prevPageviewTimestamp
const scrollContext = this._instance.scrollManager.getContext()

if (!previousPath || !scrollContext) {
return {}
}

let { maxScrollHeight, lastScrollY, maxScrollY, maxContentHeight, lastContentY, maxContentY } = scrollContext
let properties: PageViewEventProperties = {}
if (scrollContext) {
let { maxScrollHeight, lastScrollY, maxScrollY, maxContentHeight, lastContentY, maxContentY } =
scrollContext

if (
isUndefined(maxScrollHeight) ||
isUndefined(lastScrollY) ||
isUndefined(maxScrollY) ||
isUndefined(maxContentHeight) ||
isUndefined(lastContentY) ||
isUndefined(maxContentY)
) {
return {}
}
if (
!isUndefined(maxScrollHeight) &&
!isUndefined(lastScrollY) &&
!isUndefined(maxScrollY) &&
!isUndefined(maxContentHeight) &&
!isUndefined(lastContentY) &&
!isUndefined(maxContentY)
) {
// Use ceil, so that e.g. scrolling 999.5px of a 1000px page is considered 100% scrolled
maxScrollHeight = Math.ceil(maxScrollHeight)
lastScrollY = Math.ceil(lastScrollY)
maxScrollY = Math.ceil(maxScrollY)
maxContentHeight = Math.ceil(maxContentHeight)
lastContentY = Math.ceil(lastContentY)
maxContentY = Math.ceil(maxContentY)

// Use ceil, so that e.g. scrolling 999.5px of a 1000px page is considered 100% scrolled
maxScrollHeight = Math.ceil(maxScrollHeight)
lastScrollY = Math.ceil(lastScrollY)
maxScrollY = Math.ceil(maxScrollY)
maxContentHeight = Math.ceil(maxContentHeight)
lastContentY = Math.ceil(lastContentY)
maxContentY = Math.ceil(maxContentY)
// if the maximum scroll height is near 0, then the percentage is 1
const lastScrollPercentage = maxScrollHeight <= 1 ? 1 : clamp(lastScrollY / maxScrollHeight, 0, 1)
const maxScrollPercentage = maxScrollHeight <= 1 ? 1 : clamp(maxScrollY / maxScrollHeight, 0, 1)
const lastContentPercentage = maxContentHeight <= 1 ? 1 : clamp(lastContentY / maxContentHeight, 0, 1)
const maxContentPercentage = maxContentHeight <= 1 ? 1 : clamp(maxContentY / maxContentHeight, 0, 1)

// if the maximum scroll height is near 0, then the percentage is 1
const lastScrollPercentage = maxScrollHeight <= 1 ? 1 : clamp(lastScrollY / maxScrollHeight, 0, 1)
const maxScrollPercentage = maxScrollHeight <= 1 ? 1 : clamp(maxScrollY / maxScrollHeight, 0, 1)
const lastContentPercentage = maxContentHeight <= 1 ? 1 : clamp(lastContentY / maxContentHeight, 0, 1)
const maxContentPercentage = maxContentHeight <= 1 ? 1 : clamp(maxContentY / maxContentHeight, 0, 1)
properties = {
$prev_pageview_last_scroll: lastScrollY,
$prev_pageview_last_scroll_percentage: lastScrollPercentage,
$prev_pageview_max_scroll: maxScrollY,
$prev_pageview_max_scroll_percentage: maxScrollPercentage,
$prev_pageview_last_content: lastContentY,
$prev_pageview_last_content_percentage: lastContentPercentage,
$prev_pageview_max_content: maxContentY,
$prev_pageview_max_content_percentage: maxContentPercentage,
}
}
}

return {
$prev_pageview_pathname: previousPath,
$prev_pageview_last_scroll: lastScrollY,
$prev_pageview_last_scroll_percentage: lastScrollPercentage,
$prev_pageview_max_scroll: maxScrollY,
$prev_pageview_max_scroll_percentage: maxScrollPercentage,
$prev_pageview_last_content: lastContentY,
$prev_pageview_last_content_percentage: lastContentPercentage,
$prev_pageview_max_content: maxContentY,
$prev_pageview_max_content_percentage: maxContentPercentage,
if (previousPath) {
properties.$prev_pageview_pathname = previousPath
}
if (previousTimestamp) {
// Use seconds, for consistency with our other duration-related properties like $duration
properties.$prev_pageview_duration = (timestamp.getTime() - previousTimestamp.getTime()) / 1000
}

return properties
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,10 +802,13 @@ export class PostHog {
this.persistence.set_initial_person_info()
}

const systemTime = new Date()
const timestamp = options?.timestamp || systemTime

let data: CaptureResult = {
uuid: uuidv7(),
event: event_name,
properties: this._calculate_event_properties(event_name, properties || {}),
properties: this._calculate_event_properties(event_name, properties || {}, timestamp),
}

if (clientRateLimitContext) {
Expand All @@ -822,10 +825,10 @@ export class PostHog {
}

data = _copyAndTruncateStrings(data, options?._noTruncate ? null : this.config.properties_string_max_length)
data.timestamp = options?.timestamp || new Date()
data.timestamp = timestamp
if (!isUndefined(options?.timestamp)) {
data.properties['$event_time_override_provided'] = true
data.properties['$event_time_override_system_time'] = new Date()
data.properties['$event_time_override_system_time'] = systemTime
}

// Top-level $set overriding values from the one from properties is taken from the plugin-server normalizeEvent
Expand Down Expand Up @@ -858,7 +861,7 @@ export class PostHog {
this.on('eventCaptured', (data) => callback(data.event, data))
}

_calculate_event_properties(event_name: string, event_properties: Properties): Properties {
_calculate_event_properties(event_name: string, event_properties: Properties, timestamp: Date): Properties {
if (!this.persistence || !this.sessionPersistence) {
return event_properties
}
Expand Down Expand Up @@ -905,9 +908,9 @@ export class PostHog {
if (!this.config.disable_scroll_properties) {
let performanceProperties: Record<string, any> = {}
if (event_name === '$pageview') {
performanceProperties = this.pageViewManager.doPageView()
performanceProperties = this.pageViewManager.doPageView(timestamp)
} else if (event_name === '$pageleave') {
performanceProperties = this.pageViewManager.doPageLeave()
performanceProperties = this.pageViewManager.doPageLeave(timestamp)
}
properties = extend(properties, performanceProperties)
}
Expand All @@ -918,7 +921,7 @@ export class PostHog {

// set $duration if time_event was previously called for this event
if (!isUndefined(startTimestamp)) {
const duration_in_ms = new Date().getTime() - startTimestamp
const duration_in_ms = timestamp.getTime() - startTimestamp
properties['$duration'] = parseFloat((duration_in_ms / 1000).toFixed(3))
}

Expand Down
Loading