-
Notifications
You must be signed in to change notification settings - Fork 57
Adding aggregated engagement tracking to snowplow.js #1290
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
Open
jrpeck1989
wants to merge
3
commits into
main
Choose a base branch
from
add-agg-engagement-tracking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ import { | |
addGlobalContexts, | ||
enableActivityTracking, | ||
disableAnonymousTracking, | ||
trackSelfDescribingEvent, | ||
enableActivityTrackingCallback, | ||
} from '@snowplow/browser-tracker' | ||
import { | ||
LinkClickTrackingPlugin, | ||
|
@@ -25,6 +27,39 @@ import { reloadOnce } from './src/helpers/reloadOnce' | |
import { isEmpty, pickBy } from 'lodash' | ||
import { SnowplowMediaPlugin } from '@snowplow/browser-plugin-media' | ||
|
||
let aggregatedEvent = { | ||
minXOffset: 0, | ||
maxXOffset: 0, | ||
minYOffset: 0, | ||
maxYOffset: 0, | ||
numEvents: 0, | ||
} | ||
|
||
const sendAggregatedEvent = () => { | ||
if (aggregatedEvent.numEvents > 0) { | ||
trackSelfDescribingEvent({ | ||
event: { | ||
schema: 'iglu:com.snowplowanalytics/page_view_aggregated/jsonschema/1-0-0', | ||
data: { | ||
minXOffset: Math.max(0, Math.round(aggregatedEvent.minXOffset)), | ||
maxXOffset: Math.max(0, Math.round(aggregatedEvent.maxXOffset)), | ||
minYOffset: Math.max(0, Math.round(aggregatedEvent.minYOffset)), | ||
maxYOffset: Math.max(0, Math.round(aggregatedEvent.maxYOffset)), | ||
activeSeconds: aggregatedEvent.numEvents, | ||
}, | ||
}, | ||
}) | ||
} | ||
// Reset | ||
aggregatedEvent = { | ||
minXOffset: 0, | ||
maxXOffset: 0, | ||
minYOffset: 0, | ||
maxYOffset: 0, | ||
numEvents: 0, | ||
} | ||
} | ||
|
||
const createTrackerConfig = (cookieName) => { | ||
const appId = DOCS_SITE_URLS.includes(window.location.hostname) | ||
? 'docs2' | ||
|
@@ -43,6 +78,7 @@ const createTrackerConfig = (cookieName) => { | |
cookieDomain: `.${domain[1]}.${domain[0]}`, | ||
cookieName, | ||
cookieSameSite: 'Lax', | ||
keepalive: true, | ||
contexts: { | ||
webPage: true, | ||
performanceTiming: true, | ||
|
@@ -64,16 +100,18 @@ const createTrackerConfig = (cookieName) => { | |
const setupBrowserTracker = () => { | ||
newTracker( | ||
'snplow5', | ||
'https://collector.snowplow.io', | ||
// 'https://collector.snowplow.io', | ||
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. fix these commented out bits please |
||
'com-snplow-sales-aws-prod1.collector.snplow.net', | ||
createTrackerConfig('_sp5_') | ||
) | ||
newTracker('biz1', 'https://c.snowplow.io', createTrackerConfig('_sp_biz1_')) | ||
// newTracker('biz1', 'https://c.snowplow.io', createTrackerConfig('_sp_biz1_')) | ||
|
||
const selectedTabContext = () => { | ||
const data = pickBy({ | ||
cloud: localStorage.getItem('docusaurus.tab.cloud'), | ||
data_warehouse: localStorage.getItem('docusaurus.tab.warehouse'), | ||
}) | ||
// @ts-ignore | ||
if (!_.isEmpty(data)) | ||
return { | ||
schema: | ||
|
@@ -84,18 +122,49 @@ const setupBrowserTracker = () => { | |
addGlobalContexts([selectedTabContext]) | ||
|
||
enableLinkClickTracking() | ||
|
||
enableActivityTracking({ | ||
heartbeatDelay: 10, | ||
minimumVisitLength: 10, | ||
}) // precise tracking for the unified log | ||
|
||
enableActivityTrackingCallback({ | ||
minimumVisitLength: 1, | ||
heartbeatDelay: 1, | ||
callback: function (event) { | ||
const newMinX = | ||
aggregatedEvent.numEvents === 0 | ||
? event.minXOffset | ||
: Math.min(aggregatedEvent.minXOffset, event.minXOffset) | ||
|
||
const newMinY = | ||
aggregatedEvent.numEvents === 0 | ||
? event.minYOffset | ||
: Math.min(aggregatedEvent.minYOffset, event.minYOffset) | ||
|
||
aggregatedEvent = { | ||
minXOffset: newMinX, | ||
maxXOffset: Math.max(aggregatedEvent.maxXOffset, event.maxXOffset), | ||
minYOffset: newMinY, | ||
maxYOffset: Math.max(aggregatedEvent.maxYOffset, event.maxYOffset), | ||
numEvents: aggregatedEvent.numEvents + 1, | ||
} | ||
}, | ||
}) | ||
|
||
enableButtonClickTracking() | ||
enableFormTracking() | ||
} | ||
|
||
if (ExecutionEnvironment.canUseDOM) { | ||
setupBrowserTracker() | ||
|
||
document.addEventListener('visibilitychange', () => { | ||
if (document.visibilityState === 'hidden') { | ||
sendAggregatedEvent() | ||
} | ||
}) | ||
|
||
onPreferencesChanged((preferences) => { | ||
preferences.cookieOptions.forEach(({ id, isEnabled }) => { | ||
if (id === 'analytics') { | ||
|
@@ -108,7 +177,7 @@ if (ExecutionEnvironment.canUseDOM) { | |
} else { | ||
const cookieKeys = document.cookie | ||
.split(';') | ||
.reduce((ac, str) => [...ac, str?.split('=')[0].trim()], []) | ||
.map((str) => str.split('=')[0].trim()) | ||
const snowplowCookies = cookieKeys.filter((cookieKey) => | ||
cookieKey.startsWith('_sp5_') | ||
) | ||
|
@@ -126,6 +195,7 @@ if (ExecutionEnvironment.canUseDOM) { | |
const module = { | ||
onRouteDidUpdate({ location, previousLocation }) { | ||
if (location.pathname !== previousLocation?.pathname) { | ||
sendAggregatedEvent() | ||
// see https://github.com/facebook/docusaurus/pull/7424 regarding setTimeout | ||
setTimeout(() => { | ||
trackPageView() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
what's this do?
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.
This is an important point. It uses the keep alive functionality of the Fetch API that means even after a window is closed or you've navigated away, the browser will still complete the request. This was the problem with the Beacon API which made this entire aggregated page ping logic so flaky, and why I want to try this method out.