Skip to content

Commit 45b04eb

Browse files
CesarManriqueHpaulboocock
authored andcommitted
Fix dynamic contexts not being evaluated on each page ping (close #891)
* Compute dynamic context on every page ping event * Add unit test for dynamic contexts in page pings
1 parent 3c6869f commit 45b04eb

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/js/tracker.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,13 +1636,13 @@ export function Tracker(functionName, namespace, version, mutSnowplowState, argm
16361636
//Clear page ping heartbeat on new page view
16371637
clearInterval(config.activityInterval);
16381638

1639-
activityInterval(config, finalizeContexts(context, contextCallback));
1639+
activityInterval(config, context, contextCallback);
16401640
}
16411641
}
16421642
}
16431643
}
16441644

1645-
function activityInterval(config, context) {
1645+
function activityInterval(config, context, contextCallback) {
16461646
const executePagePing = (cb, c) => {
16471647
refreshUrl();
16481648
cb({ context: c, pageViewId: getPageViewId(), minXOffset, minYOffset, maxXOffset, maxYOffset });
@@ -1655,7 +1655,7 @@ export function Tracker(functionName, namespace, version, mutSnowplowState, argm
16551655
// There was activity during the heart beat period;
16561656
// on average, this is going to overstate the visitDuration by configHeartBeatTimer/2
16571657
if (lastActivityTime + config.configMinimumVisitLength > now.getTime()) {
1658-
executePagePing(config.callback, context);
1658+
executePagePing(config.callback, finalizeContexts(context, contextCallback));
16591659
}
16601660

16611661
config.activityInterval = setInterval(heartbeat, config.configHeartBeatTimer);
@@ -1667,7 +1667,7 @@ export function Tracker(functionName, namespace, version, mutSnowplowState, argm
16671667
// There was activity during the heart beat period;
16681668
// on average, this is going to overstate the visitDuration by configHeartBeatTimer/2
16691669
if (lastActivityTime + config.configHeartBeatTimer > now.getTime()) {
1670-
executePagePing(config.callback, context);
1670+
executePagePing(config.callback, finalizeContexts(context, contextCallback));
16711671
}
16721672
};
16731673

tests/unit/tracker.spec.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,53 @@ describe('Activity tracker behaviour', () => {
168168
expect(countWithStaticValueEq(pageTwoTime)(outQueues)).toBe(0)
169169
})
170170

171+
it('does compute dynamic context for each page ping', () => {
172+
const outQueues = []
173+
const t = new Tracker(
174+
'',
175+
'',
176+
'',
177+
{ outQueues },
178+
{
179+
stateStorageStrategy: 'cookies',
180+
encodeBase64: false,
181+
contexts: {
182+
webPage: true,
183+
},
184+
}
185+
)
186+
t.enableActivityTracking(0, 30)
187+
let id = 0
188+
t.trackPageView(null, null, () => ([
189+
{
190+
schema: 'iglu:com.acme/dynamic_context/jsonschema/1-0-0',
191+
data: { pingId: id++ },
192+
},
193+
]))
194+
195+
jest.advanceTimersByTime(10000)
196+
197+
// PP 1
198+
t.updatePageActivity()
199+
jest.advanceTimersByTime(30000)
200+
201+
// PP 2
202+
t.updatePageActivity()
203+
jest.advanceTimersByTime(30000)
204+
205+
const findWithPingId = F.filter(F.get('data.pingId'))
206+
const extractContextsWithPingId = F.compose(
207+
findWithPingId,
208+
F.flatten,
209+
extractSchemas,
210+
getPPEvents
211+
)
212+
const contexts = extractContextsWithPingId(outQueues)
213+
214+
expect(contexts[0].data.pingId).toBe(1)
215+
expect(contexts[1].data.pingId).toBe(2)
216+
})
217+
171218
it('does not reset activity tracking on pageview when resetActivityTrackingOnPageView: false,', () => {
172219
const outQueues = []
173220
const t = new Tracker(

0 commit comments

Comments
 (0)