@aller/blink
Version:
A library for tracking user behaviour.
404 lines (354 loc) • 10.8 kB
text/typescript
import createBlink from '../main';
import { VERSION } from '../config/config';
import jest from 'jest-mock';
describe('Article activity integration test', () => {
it('should support a common scenario', () => {
const mockSend = jest.fn();
const blink = createBlink({
send: mockSend,
sendDirect: mockSend,
});
// First send of a pageLoad event, to set the general state
blink.pageInit({
url: 'http://some.site',
pageView: 'the-pageview-id',
pageType: 'fb instant article',
referrer: 'www.sol.no',
abCookie: 38,
commercialSegments: 'sport,soccer,gardening',
site: 'www.dagbladet.no',
});
expect(mockSend.mock.calls.length).toBe(0);
blink.pageActivityStart({
url: 'http://www.dagbladet.no/a/444',
pageScrollOffsetY: 500,
time: new Date(1),
});
blink.pageActivityStop({
url: 'http://www.dagbladet.no/a/444',
time: new Date(2),
});
// We expect that stop event did send something
expect(mockSend.mock.calls.length).toBe(1);
blink.pageActivityStart({
url: 'http://www.dagbladet.no/a/444',
pageScrollOffsetY: 450,
time: new Date(3),
});
blink.pageActivityStart({
url: 'http://www.dagbladet.no/a/444',
pageScrollOffsetY: 920,
time: new Date(4),
});
blink.pageActivityStop({
url: 'http://www.dagbladet.no/a/444',
time: new Date(5),
});
// We expect that stop did send something
expect(mockSend.mock.calls.length).toBe(2);
const ad = mockSend.mock.calls[1][0][0];
expect(ad).toEqual({
id: 'dagbladet.no/444',
type: 'activeTime',
version: VERSION,
site: 'www.dagbladet.no',
pageView: 'the-pageview-id',
referrer: 'www.sol.no',
activeTime: 3,
article: {
harvesterId: 'dagbladet.no/444',
url: 'http://www.dagbladet.no/a/444',
},
pageScrollMaxOffsetY: 920,
pageScrollLatestOffsetY: 920,
});
// Check that we get out the expected activeTime for this url
expect(blink.getPage({ url: 'http://www.dagbladet.no/a/444' })).toEqual({
abCookie: 38,
commercialSegments: 'sport,soccer,gardening',
activeTime: 3,
isActive: false,
pageScrollMaxOffsetY: 920,
pageScrollLatestOffsetY: 920,
pageType: 'fb instant article',
pageView: 'the-pageview-id',
previousPageView: undefined,
referrer: 'www.sol.no',
site: 'www.dagbladet.no',
url: 'http://some.site',
version: VERSION,
});
// Check that a url without activity gives to activeTime
expect(
blink.getPage({ url: 'https://www.dagbladet.no/a/3294820' }).activeTime,
).toBe(0);
});
it('should know that the user is active', () => {
const mockSend = jest.fn();
const blink = createBlink({
send: mockSend,
sendDirect: mockSend,
});
// First send of a pageLoad event, to set the general state
blink.pageInit({
url: 'http://some.site',
pageView: 'the-pageview-id',
referrer: 'www.sol.no',
abCookie: 38,
commercialSegments: 'sport,soccer,gardening',
site: 'www.dagbladet.no',
});
expect(mockSend.mock.calls.length).toBe(0);
blink.pageActivityStart({
url: 'http://www.dagbladet.no/a/444',
pageScrollOffsetY: 540,
time: new Date(1000),
});
// Check that we get out the expected activeTime for this url
expect(
blink.getPage({
url: 'http://www.dagbladet.no/a/444',
time: new Date(2000),
}),
).toEqual({
abCookie: 38,
commercialSegments: 'sport,soccer,gardening',
activeTime: 1000,
isActive: true,
pageType: '',
pageView: 'the-pageview-id',
previousPageView: undefined,
referrer: 'www.sol.no',
site: 'www.dagbladet.no',
version: VERSION,
pageScrollMaxOffsetY: 540,
pageScrollLatestOffsetY: 540,
url: 'http://some.site',
});
});
it('should not count active time when the screen is hidden', () => {
const mockSend = jest.fn();
const blink = createBlink({
send: mockSend,
sendDirect: mockSend,
});
// First send of a pageLoad event, to set the general state
blink.pageInit({
url: 'http://some.site',
pageView: 'the-pageview-id',
referrer: 'www.sol.no',
abCookie: 38,
commercialSegments: 'sport,soccer,gardening',
site: 'www.dagbladet.no',
});
expect(mockSend.mock.calls.length).toBe(0);
blink.pageActivityStart({
url: 'http://www.dagbladet.no/a/444',
pageScrollOffsetY: 500,
time: new Date(1000),
});
blink.pageActivityStop({
url: 'http://www.dagbladet.no/a/444',
time: new Date(2000),
});
// We expect that stop event did send something
expect(mockSend.mock.calls.length).toBe(1);
blink.pageActivityStart({
url: 'http://www.dagbladet.no/a/444',
pageScrollOffsetY: 993,
time: new Date(3000),
});
blink.screenHide(new Date(4000));
blink.screenShow(new Date(5500));
blink.pageActivityStop({
url: 'http://www.dagbladet.no/a/444',
time: new Date(6000),
});
// We expect that stop did send something
expect(mockSend.mock.calls.length).toBe(2);
const ad = mockSend.mock.calls[1][0][0];
expect(ad).toEqual({
id: 'dagbladet.no/444',
type: 'activeTime',
version: VERSION,
site: 'www.dagbladet.no',
pageView: 'the-pageview-id',
referrer: 'www.sol.no',
activeTime: 2500,
article: {
harvesterId: 'dagbladet.no/444',
url: 'http://www.dagbladet.no/a/444',
},
pageScrollMaxOffsetY: 993,
pageScrollLatestOffsetY: 993,
});
// Check that we get out the expected activeTime for this url
expect(blink.getPage({ url: 'http://www.dagbladet.no/a/444' })).toEqual({
abCookie: 38,
commercialSegments: 'sport,soccer,gardening',
activeTime: 2500,
isActive: false,
pageType: '',
pageView: 'the-pageview-id',
previousPageView: undefined,
referrer: 'www.sol.no',
site: 'www.dagbladet.no',
url: 'http://some.site',
version: VERSION,
pageScrollMaxOffsetY: 993,
pageScrollLatestOffsetY: 993,
});
// Check that a url without activity gives to activeTime
expect(
blink.getPage({ url: 'https://www.dagbladet.no/a/3294820' }).activeTime,
).toBe(0);
});
it('should not count active time after 10 seconds since last pulse', () => {
const mockSend = jest.fn();
const blink = createBlink({
send: mockSend,
sendDirect: mockSend,
});
// First send of a pageLoad event, to set the general state
blink.pageInit({
url: 'http://some.site',
pageView: 'the-pageview-id',
abCookie: 38,
commercialSegments: 'sport,food',
site: 'www.dagbladet.no',
});
expect(mockSend.mock.calls.length).toBe(0);
blink.pageActivityStart({
url: 'http://www.dagbladet.no/a/444',
time: new Date(1000),
});
blink.pageActivityStart({
url: 'http://www.dagbladet.no/a/444',
time: new Date(4000),
});
// Check that a url without activity gives to activeTime
// Start: 1000. Expected end: 4000 + 10000 = 14000.
// Expected active time: 14000 - 1000 = 13000
expect(
blink.getPage({
url: 'http://www.dagbladet.no/a/444',
time: new Date(50000),
}).activeTime,
).toBe(13000);
});
it('should not add 10 seconds if last pulse happened <10s ago', () => {
const mockSend = jest.fn();
const blink = createBlink({
send: mockSend,
sendDirect: mockSend,
});
// First send of a pageLoad event, to set the general state
blink.pageInit({
url: 'http://some.site',
pageView: 'the-pageview-id',
abCookie: 38,
commercialSegments: 'sport,food',
site: 'www.dagbladet.no',
});
expect(mockSend.mock.calls.length).toBe(0);
blink.pageActivityStart({
url: 'http://www.dagbladet.no/a/3294820',
time: new Date(5000),
});
// Check that a url without activity gives to activeTime
// Start: 5000. End: 9000.
// Expected active time: 9000 - 5000 = 4000
expect(
blink.getPage({
url: 'https://www.dagbladet.no/a/3294820',
time: new Date(9000),
}).activeTime,
).toBe(4000);
});
it('should send two separate events for two separate pages', () => {
enum PAGES {
FIRST = 'FIRST_PAGE',
SECOND = 'SECOND_PAGE',
}
const mockSend = jest.fn();
const blink = createBlink({
send: mockSend,
sendDirect: mockSend,
});
// FIRST PAGE INIT
blink.pageInit({
url: 'http://some.site',
pageView: `${PAGES.FIRST}-pageview`,
pageId: PAGES.FIRST,
referrer: 'www.sol.no',
site: 'www.kk.no',
});
// SECOND PAGE INIT
blink.pageInit({
url: 'http://other.site',
pageView: `${PAGES.SECOND}-pageview`,
pageId: PAGES.SECOND,
referrer: 'www.sol.no',
site: 'www.kk.no',
});
// FIRST PAGE ACTIVITY START
blink.pageActivityStart({
url: 'https://www.dagbladet.no',
time: new Date(1000),
pageId: PAGES.FIRST,
});
// SECOND PAGE ACTIVITY START
blink.pageActivityStart({
url: 'https://www.dagbladet.no',
time: new Date(2000),
pageId: PAGES.SECOND,
});
// FIRST PAGE ACTIVITY STOP
blink.pageActivityStop({
url: 'https://www.dagbladet.no',
time: new Date(5000),
pageId: PAGES.FIRST,
});
// SECOND PAGE ACTIVITY STOP
blink.pageActivityStop({
url: 'https://www.dagbladet.no',
time: new Date(8000),
pageId: PAGES.SECOND,
});
expect(mockSend.mock.calls.length).toBe(2);
// Check first event
const firstEvent = mockSend.mock.calls[0][0][0];
expect(firstEvent).toEqual({
activeTime: 4000,
article: {
harvesterId: 'www.kk.no',
url: 'https://www.dagbladet.no',
},
pageScrollMaxOffsetY: 0,
pageScrollLatestOffsetY: 0,
pageView: 'FIRST_PAGE-pageview',
referrer: 'www.sol.no',
site: 'www.kk.no',
type: 'activeTime',
version: VERSION,
id: 'www.kk.no',
});
// Check second event
const secondEvent = mockSend.mock.calls[1][0][0];
expect(secondEvent).toEqual({
activeTime: 6000,
article: {
harvesterId: 'www.kk.no',
url: 'https://www.dagbladet.no',
},
pageScrollMaxOffsetY: 0,
pageScrollLatestOffsetY: 0,
id: 'www.kk.no',
pageView: 'SECOND_PAGE-pageview',
referrer: 'www.sol.no',
site: 'www.kk.no',
type: 'activeTime',
version: VERSION,
});
});
});