Repository navigation
Expand file tree
/
Copy pathexample-localstorage.ts
More file actions
65 lines (56 loc) · 1.87 KB
/
Copy pathexample-localstorage.ts
File metadata and controls
65 lines (56 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {
configure,
initialize,
LocalStorageEventQueue,
SearchAction,
trackProductClick,
trackSearch,
} from './src/index';
const hasLocalStorage = typeof globalThis !== 'undefined'
&& typeof globalThis.localStorage !== 'undefined';
if (!hasLocalStorage) {
console.log('example:localstorage is browser-only (localStorage is not available in Node.js).');
} else {
// Create LocalStorageEventQueue - events will persist across page reloads
const eventQueue = new LocalStorageEventQueue(undefined, 10);
// Configure with LocalStorageEventQueue
configure({
endpoint: 'ignored',
channel: 'de',
queueSettings: {
batchInterval: 5000,
maxBatchSize: 10,
},
overrides: {
eventQueue,
},
});
console.log('SearchCollector configured with LocalStorageEventQueue');
console.log('Events will persist in localStorage across page reloads');
// Initialize
initialize();
console.log('Initialized');
// Track some events
trackSearch({
keywords: 'laptop',
count: 50,
action: SearchAction.Search,
});
console.log('Tracked search: laptop');
trackProductClick({
productId: 'prod-456',
position: 1,
keywords: 'laptop',
});
console.log('Tracked product: prod-456');
trackSearch({
keywords: 'gaming mouse',
count: 30,
action: SearchAction.SearchPagination,
});
console.log('Tracked search: gaming mouse');
console.log('\nEvents are now stored in localStorage (key: "search-collector-queue")');
console.log('They will be automatically sent after 5 seconds, or when you call flush()');
console.log('\nIf you reload this page before the events are sent, they will be loaded from localStorage');
console.log('and sent when the queue is flushed again.');
}