fix(analytics): identify Memind users before pageviews
Memind CI / Test, build, and release guards (pull_request) Successful in 3m11s
Memind CI / Test, build, and release guards (pull_request) Successful in 3m11s
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import vm from 'node:vm';
|
||||
|
||||
import {
|
||||
injectMindSpaceAnalytics,
|
||||
@@ -64,10 +65,14 @@ test('injects one local same-origin tracker with page dimensions', () => {
|
||||
assert.match(out, /src="\/analytics\/script\.js"/);
|
||||
assert.match(out, /data-host-url="\/analytics"/);
|
||||
assert.match(out, /data-auto-track="false"/);
|
||||
assert.match(out, /window\.umami\.identify\(d\.owner_id,\{username:d\.username,memind_page_url:location\.href,owner_segment:d\.owner_segment,channel:d\.channel\}\)/);
|
||||
assert.match(out, /function pageview\(\).*window\.umami\.track\(\)/);
|
||||
assert.ok(out.indexOf('identify();pageview();') > 0);
|
||||
assert.doesNotMatch(out, /t\('page_view'\)/);
|
||||
assert.match(out, /page_id/);
|
||||
assert.match(out, /owner_segment/);
|
||||
assert.doesNotMatch(out, /owner_label/);
|
||||
assert.doesNotMatch(out, /张三/);
|
||||
assert.match(out, /"username":"张三"/);
|
||||
assert.match(out, /page_click/);
|
||||
assert.match(out, /page_form_submit/);
|
||||
assert.match(out, /page_scroll_/);
|
||||
@@ -76,6 +81,55 @@ test('injects one local same-origin tracker with page dimensions', () => {
|
||||
assert.equal(injectMindSpaceAnalytics(out, { ownerId: 'user-123', config: { enabled: true, websiteId: 'local-website', idSecret: 'secret' } }), out);
|
||||
});
|
||||
|
||||
test('identifies the pseudonymous owner before sending a standard page view', () => {
|
||||
const out = injectMindSpaceAnalytics('<!doctype html><html><head></head><body></body></html>', {
|
||||
ownerId: 'user-123',
|
||||
ownerSegment: 'plan:pro',
|
||||
ownerLabel: '张三',
|
||||
channel: 'h5',
|
||||
config: {
|
||||
enabled: true,
|
||||
websiteId: 'local-website',
|
||||
idSecret: 'secret',
|
||||
scriptPath: '/analytics/script.js',
|
||||
hostPath: '/analytics',
|
||||
},
|
||||
});
|
||||
const inlineScript = out.match(/<script data-memind-analytics="1">([\s\S]*?)<\/script>/)?.[1];
|
||||
assert.ok(inlineScript);
|
||||
|
||||
const calls = [];
|
||||
vm.runInNewContext(inlineScript, {
|
||||
window: {
|
||||
umami: {
|
||||
identify: (...args) => calls.push(['identify', ...args]),
|
||||
track: (...args) => calls.push(['track', ...args]),
|
||||
},
|
||||
innerHeight: 800,
|
||||
scrollY: 0,
|
||||
addEventListener: () => {},
|
||||
},
|
||||
document: {
|
||||
readyState: 'complete',
|
||||
title: 'Demo',
|
||||
documentElement: { scrollHeight: 1600 },
|
||||
addEventListener: () => {},
|
||||
},
|
||||
location: { href: 'https://m.tkmind.cn/MindSpace/demo/public/page.html' },
|
||||
setTimeout: () => {},
|
||||
});
|
||||
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(calls)), [
|
||||
['identify', pseudonymizeAnalyticsId('user-123', 'secret'), {
|
||||
username: '张三',
|
||||
memind_page_url: 'https://m.tkmind.cn/MindSpace/demo/public/page.html',
|
||||
owner_segment: 'plan:pro',
|
||||
channel: 'h5',
|
||||
}],
|
||||
['track'],
|
||||
]);
|
||||
});
|
||||
|
||||
test('does not alter non-full-html or disabled pages', () => {
|
||||
const fragment = '<div>hello</div>';
|
||||
assert.equal(injectMindSpaceAnalytics(fragment, { ownerId: 'u', config: { enabled: true, websiteId: 'w', idSecret: 's' } }), fragment);
|
||||
|
||||
Reference in New Issue
Block a user