@usesophi/sophi-web-object 1.3.1 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/sophi.esm.js +513 -5
- package/dist/sophi.esm.js.map +1 -1
- package/dist/sophi.js +511 -29
- package/dist/sophi.js.map +1 -1
- package/dist/sophi.min.js +2 -2
- package/dist/types/attribution.d.ts +22 -0
- package/dist/types/collector.d.ts +7 -1
- package/dist/types/iife.d.ts +13 -0
- package/dist/types/index.d.ts +14 -2
- package/dist/types/objects/entry.d.ts +16 -0
- package/dist/types/queue.d.ts +9 -0
- package/dist/types/types.d.ts +41 -0
- package/package.json +2 -1
package/dist/sophi.js
CHANGED
|
@@ -1,10 +1,44 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* sophi-web-object v1.3.
|
|
2
|
+
* sophi-web-object v1.3.3
|
|
3
3
|
* Framework-agnostic e-commerce tracking script
|
|
4
4
|
*/
|
|
5
|
-
|
|
5
|
+
(function () {
|
|
6
6
|
'use strict';
|
|
7
7
|
|
|
8
|
+
/******************************************************************************
|
|
9
|
+
Copyright (c) Microsoft Corporation.
|
|
10
|
+
|
|
11
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
12
|
+
purpose with or without fee is hereby granted.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
15
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
16
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
17
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
18
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
19
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
20
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
21
|
+
***************************************************************************** */
|
|
22
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
function __rest(s, e) {
|
|
26
|
+
var t = {};
|
|
27
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
28
|
+
t[p] = s[p];
|
|
29
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
30
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
31
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
32
|
+
t[p[i]] = s[p[i]];
|
|
33
|
+
}
|
|
34
|
+
return t;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
38
|
+
var e = new Error(message);
|
|
39
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
40
|
+
};
|
|
41
|
+
|
|
8
42
|
/** Sophi API origin — set via SOPHI_API_URL in .env at build time. */
|
|
9
43
|
const SOPHI_API_ORIGIN = "https://api.usesophi.com";
|
|
10
44
|
const DEFAULTS = {
|
|
@@ -302,6 +336,26 @@ var SophiTracker = (function (exports) {
|
|
|
302
336
|
log(`Queued event: ${envelope.event_type} (queue size: ${queue.length})`);
|
|
303
337
|
scheduleFlush();
|
|
304
338
|
}
|
|
339
|
+
/**
|
|
340
|
+
* Send one event right away, bypassing the debounce window. Used for Sophi
|
|
341
|
+
* button clicks, which are usually followed by a same-tab navigation.
|
|
342
|
+
*
|
|
343
|
+
* With an established session the event goes out through fetch keepalive
|
|
344
|
+
* (with auth headers), which survives the page being torn down. Without a
|
|
345
|
+
* session yet it falls back to the normal queue.
|
|
346
|
+
*/
|
|
347
|
+
function sendImmediate(envelope) {
|
|
348
|
+
if (!hasStorageConsent()) {
|
|
349
|
+
log(`Dropped event (no storage consent): ${envelope.event_type}`);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
const session = getResolvedSession();
|
|
353
|
+
if (session && sendOnUnload(envelope, session)) {
|
|
354
|
+
log(`Sent event immediately: ${envelope.event_type}`);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
enqueue(envelope);
|
|
358
|
+
}
|
|
305
359
|
function scheduleFlush() {
|
|
306
360
|
if (debounceTimer !== null)
|
|
307
361
|
return;
|
|
@@ -355,6 +409,220 @@ var SophiTracker = (function (exports) {
|
|
|
355
409
|
queue.length = 0;
|
|
356
410
|
}
|
|
357
411
|
|
|
412
|
+
/**
|
|
413
|
+
* Campaign attribution. On by default; `config.attribution: false` turns it off
|
|
414
|
+
* and removes any stored value.
|
|
415
|
+
*
|
|
416
|
+
* On boot the SDK reads UTM parameters, ad click IDs and the external referrer
|
|
417
|
+
* hostname of the current page view. A page view that has any of them is a
|
|
418
|
+
* "touch". The first touch is kept for 30 days; the last touch is replaced by
|
|
419
|
+
* every new touch; direct visits and internal navigation change nothing.
|
|
420
|
+
*
|
|
421
|
+
* The value is held in memory and written to localStorage only while storage
|
|
422
|
+
* consent is granted. It is attached to every event under
|
|
423
|
+
* `page.custom._sophi.attribution` by the collector.
|
|
424
|
+
*/
|
|
425
|
+
const UTM_FIELDS = {
|
|
426
|
+
utm_source: 'source',
|
|
427
|
+
utm_medium: 'medium',
|
|
428
|
+
utm_campaign: 'campaign',
|
|
429
|
+
utm_term: 'term',
|
|
430
|
+
utm_content: 'content',
|
|
431
|
+
};
|
|
432
|
+
const CLICK_ID_SOURCES = {
|
|
433
|
+
gclid: 'google',
|
|
434
|
+
gbraid: 'google',
|
|
435
|
+
wbraid: 'google',
|
|
436
|
+
fbclid: 'facebook',
|
|
437
|
+
msclkid: 'bing',
|
|
438
|
+
ttclid: 'tiktok',
|
|
439
|
+
li_fat_id: 'linkedin',
|
|
440
|
+
};
|
|
441
|
+
const TOUCH_TTL_SECONDS = 30 * 86400;
|
|
442
|
+
const MAX_VALUE_LENGTH = 256;
|
|
443
|
+
/** Touch detected on this page view (null = direct / internal). */
|
|
444
|
+
let pageTouch = null;
|
|
445
|
+
/** Resolved attribution for this page view, attached to events. */
|
|
446
|
+
let current = null;
|
|
447
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────────
|
|
448
|
+
/** On unless the integrator sets `config.attribution: false` (default on since 1.3.3). */
|
|
449
|
+
function isEnabled() {
|
|
450
|
+
return getConfig().attribution !== false;
|
|
451
|
+
}
|
|
452
|
+
function storageKey() {
|
|
453
|
+
return `sophi-${hashKey(getConfig().apiKey)}-attribution`;
|
|
454
|
+
}
|
|
455
|
+
function clip(value) {
|
|
456
|
+
const trimmed = value === null || value === void 0 ? void 0 : value.trim();
|
|
457
|
+
return trimmed ? trimmed.slice(0, MAX_VALUE_LENGTH) : undefined;
|
|
458
|
+
}
|
|
459
|
+
function bareHost(host) {
|
|
460
|
+
return host.toLowerCase().replace(/^www\./, '');
|
|
461
|
+
}
|
|
462
|
+
function isTouch(value) {
|
|
463
|
+
return (typeof value === 'object' &&
|
|
464
|
+
value !== null &&
|
|
465
|
+
typeof value.timestamp === 'number');
|
|
466
|
+
}
|
|
467
|
+
function readStored() {
|
|
468
|
+
try {
|
|
469
|
+
const raw = localStorage.getItem(storageKey());
|
|
470
|
+
if (!raw)
|
|
471
|
+
return null;
|
|
472
|
+
const parsed = JSON.parse(raw);
|
|
473
|
+
return {
|
|
474
|
+
first: isTouch(parsed === null || parsed === void 0 ? void 0 : parsed.first) ? parsed.first : undefined,
|
|
475
|
+
last: isTouch(parsed === null || parsed === void 0 ? void 0 : parsed.last) ? parsed.last : undefined,
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
catch (_a) {
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
function writeStored(value) {
|
|
483
|
+
try {
|
|
484
|
+
if (value) {
|
|
485
|
+
localStorage.setItem(storageKey(), JSON.stringify(value));
|
|
486
|
+
}
|
|
487
|
+
else {
|
|
488
|
+
localStorage.removeItem(storageKey());
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
catch (_a) {
|
|
492
|
+
// Storage unavailable (private mode, blocked) — keep the in-memory value.
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
function fresh(touch, now) {
|
|
496
|
+
return touch && now - touch.timestamp < TOUCH_TTL_SECONDS ? touch : undefined;
|
|
497
|
+
}
|
|
498
|
+
/** Combines stored state with this page view's touch. */
|
|
499
|
+
function mergeAttribution(stored, touch, now) {
|
|
500
|
+
let first = fresh(stored === null || stored === void 0 ? void 0 : stored.first, now);
|
|
501
|
+
let last = fresh(stored === null || stored === void 0 ? void 0 : stored.last, now);
|
|
502
|
+
if (touch) {
|
|
503
|
+
last = touch;
|
|
504
|
+
first = first !== null && first !== void 0 ? first : touch;
|
|
505
|
+
}
|
|
506
|
+
if (!first && !last)
|
|
507
|
+
return null;
|
|
508
|
+
const result = {};
|
|
509
|
+
if (first)
|
|
510
|
+
result.first = first;
|
|
511
|
+
if (last)
|
|
512
|
+
result.last = last;
|
|
513
|
+
return result;
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Reads the touch from a page URL and referrer. Returns null for a direct
|
|
517
|
+
* visit or internal navigation. Only the referrer hostname is kept, and the
|
|
518
|
+
* landing URL keeps only its origin, path, UTM and click-ID parameters.
|
|
519
|
+
*/
|
|
520
|
+
function detectTouch(href, referrer, now) {
|
|
521
|
+
var _a, _b;
|
|
522
|
+
let url;
|
|
523
|
+
try {
|
|
524
|
+
url = new URL(href);
|
|
525
|
+
}
|
|
526
|
+
catch (_c) {
|
|
527
|
+
return null;
|
|
528
|
+
}
|
|
529
|
+
const params = url.searchParams;
|
|
530
|
+
const touch = { timestamp: now };
|
|
531
|
+
const kept = new URLSearchParams();
|
|
532
|
+
for (const [param, field] of Object.entries(UTM_FIELDS)) {
|
|
533
|
+
const value = clip(params.get(param));
|
|
534
|
+
if (value) {
|
|
535
|
+
touch[field] = value;
|
|
536
|
+
kept.set(param, value);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
for (const param of Object.keys(CLICK_ID_SOURCES)) {
|
|
540
|
+
const value = clip(params.get(param));
|
|
541
|
+
if (value) {
|
|
542
|
+
kept.set(param, value);
|
|
543
|
+
if (!touch.click_id) {
|
|
544
|
+
touch.click_id = value;
|
|
545
|
+
touch.click_id_type = param;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
let referrerHost;
|
|
550
|
+
if (referrer) {
|
|
551
|
+
try {
|
|
552
|
+
const host = new URL(referrer).hostname;
|
|
553
|
+
if (host && bareHost(host) !== bareHost(url.hostname)) {
|
|
554
|
+
referrerHost = host.toLowerCase();
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
catch (_d) {
|
|
558
|
+
// Malformed referrer — ignore.
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
const hasCampaign = kept.toString() !== '';
|
|
562
|
+
if (!hasCampaign && !referrerHost)
|
|
563
|
+
return null;
|
|
564
|
+
if (referrerHost)
|
|
565
|
+
touch.referrer = referrerHost;
|
|
566
|
+
if (!touch.source) {
|
|
567
|
+
if (touch.click_id_type) {
|
|
568
|
+
touch.source = CLICK_ID_SOURCES[touch.click_id_type];
|
|
569
|
+
}
|
|
570
|
+
else if (referrerHost) {
|
|
571
|
+
touch.source = referrerHost;
|
|
572
|
+
touch.medium = (_a = touch.medium) !== null && _a !== void 0 ? _a : 'referral';
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
const query = kept.toString();
|
|
576
|
+
touch.landing_url = (_b = clip(`${url.origin}${url.pathname}${query ? `?${query}` : ''}`)) !== null && _b !== void 0 ? _b : url.origin;
|
|
577
|
+
return touch;
|
|
578
|
+
}
|
|
579
|
+
// ─── Public API ───────────────────────────────────────────────────────────────
|
|
580
|
+
/**
|
|
581
|
+
* Called once at bootstrap. With `config.attribution: false` it captures
|
|
582
|
+
* nothing and removes a value stored while attribution was on.
|
|
583
|
+
*/
|
|
584
|
+
function captureAttribution() {
|
|
585
|
+
pageTouch = null;
|
|
586
|
+
current = null;
|
|
587
|
+
if (!isEnabled()) {
|
|
588
|
+
writeStored(null);
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
const now = Math.floor(Date.now() / 1000);
|
|
592
|
+
pageTouch = detectTouch(window.location.href, document.referrer, now);
|
|
593
|
+
if (hasStorageConsent()) {
|
|
594
|
+
current = mergeAttribution(readStored(), pageTouch, now);
|
|
595
|
+
writeStored(current);
|
|
596
|
+
}
|
|
597
|
+
else {
|
|
598
|
+
// No consent: keep this page view's touch in memory only.
|
|
599
|
+
current = mergeAttribution(null, pageTouch, now);
|
|
600
|
+
}
|
|
601
|
+
if (current)
|
|
602
|
+
log('Attribution captured', current);
|
|
603
|
+
}
|
|
604
|
+
/** Called once storage consent is granted: merges with stored state and persists. */
|
|
605
|
+
function persistAttribution() {
|
|
606
|
+
if (!isEnabled() || !hasStorageConsent())
|
|
607
|
+
return;
|
|
608
|
+
const now = Math.floor(Date.now() / 1000);
|
|
609
|
+
current = mergeAttribution(readStored(), pageTouch, now);
|
|
610
|
+
writeStored(current);
|
|
611
|
+
}
|
|
612
|
+
/** Called when storage consent is withdrawn: removes the stored value. */
|
|
613
|
+
function clearAttribution() {
|
|
614
|
+
writeStored(null);
|
|
615
|
+
if (!isEnabled())
|
|
616
|
+
return;
|
|
617
|
+
current = mergeAttribution(null, pageTouch, Math.floor(Date.now() / 1000));
|
|
618
|
+
}
|
|
619
|
+
/** Attribution to attach to events, or undefined when there is none. */
|
|
620
|
+
function getAttribution() {
|
|
621
|
+
if (!current || !isEnabled())
|
|
622
|
+
return undefined;
|
|
623
|
+
return Object.assign({}, current);
|
|
624
|
+
}
|
|
625
|
+
|
|
358
626
|
function mapPageEvent(page, sdkVersion, context) {
|
|
359
627
|
if (!page.type) {
|
|
360
628
|
warn('page.type is required — skipping page_view event');
|
|
@@ -367,6 +635,90 @@ var SophiTracker = (function (exports) {
|
|
|
367
635
|
return Object.assign({ event_type: 'page_view', client_timestamp: Math.floor(Date.now() / 1000), sdk_version: sdkVersion, page }, context);
|
|
368
636
|
}
|
|
369
637
|
|
|
638
|
+
const MAX_LENGTH = 64;
|
|
639
|
+
const ENTRY_TYPES = [
|
|
640
|
+
'search_bar',
|
|
641
|
+
'post_search',
|
|
642
|
+
'floating_trigger',
|
|
643
|
+
'homepage_widget',
|
|
644
|
+
'pdp',
|
|
645
|
+
'product_grid',
|
|
646
|
+
'menu',
|
|
647
|
+
'checkout',
|
|
648
|
+
];
|
|
649
|
+
/**
|
|
650
|
+
* Returns a copy of `page` with `patch` merged into `page.custom._sophi`.
|
|
651
|
+
* The integrator's own `page.custom` keys are kept, and the original object
|
|
652
|
+
* is never mutated (writing back into sophi_object.page would make the Proxy
|
|
653
|
+
* emit an extra page_view).
|
|
654
|
+
*/
|
|
655
|
+
function withSophi(page, patch) {
|
|
656
|
+
var _a;
|
|
657
|
+
const custom = (_a = page.custom) !== null && _a !== void 0 ? _a : {};
|
|
658
|
+
const existing = custom._sophi;
|
|
659
|
+
const previous = typeof existing === 'object' && existing !== null && !Array.isArray(existing)
|
|
660
|
+
? existing
|
|
661
|
+
: {};
|
|
662
|
+
return Object.assign(Object.assign({}, page), { custom: Object.assign(Object.assign({}, custom), { _sophi: Object.assign(Object.assign({}, previous), patch) }) });
|
|
663
|
+
}
|
|
664
|
+
/** Validates and normalises an entry. Returns null (after a warning) when invalid. */
|
|
665
|
+
function normalizeEntry(input) {
|
|
666
|
+
const placement = typeof (input === null || input === void 0 ? void 0 : input.placement) === 'string' ? input.placement.trim() : '';
|
|
667
|
+
if (!placement) {
|
|
668
|
+
warn('entry.placement is required — skipping entry event');
|
|
669
|
+
return null;
|
|
670
|
+
}
|
|
671
|
+
if (placement.length > MAX_LENGTH) {
|
|
672
|
+
warn(`entry.placement must be at most ${MAX_LENGTH} characters — skipping entry event`);
|
|
673
|
+
return null;
|
|
674
|
+
}
|
|
675
|
+
const entry = { placement };
|
|
676
|
+
if ((input === null || input === void 0 ? void 0 : input.group) !== undefined) {
|
|
677
|
+
const group = typeof input.group === 'string' ? input.group.trim() : '';
|
|
678
|
+
if (!group) {
|
|
679
|
+
warn('entry.group must be a non-empty string — sending without group');
|
|
680
|
+
}
|
|
681
|
+
else if (group.length > MAX_LENGTH) {
|
|
682
|
+
warn(`entry.group must be at most ${MAX_LENGTH} characters — sending without group`);
|
|
683
|
+
}
|
|
684
|
+
else {
|
|
685
|
+
entry.group = group;
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
if ((input === null || input === void 0 ? void 0 : input.type) !== undefined) {
|
|
689
|
+
if (ENTRY_TYPES.includes(input.type)) {
|
|
690
|
+
entry.type = input.type;
|
|
691
|
+
}
|
|
692
|
+
else {
|
|
693
|
+
warn(`entry.type "${String(input.type)}" is not a standard type — sending without type`);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
return entry;
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* Maps a Sophi button click to a `custom` event. The entry travels under
|
|
700
|
+
* `page.custom._sophi.entry`, so no change to the ingest contract is needed.
|
|
701
|
+
*/
|
|
702
|
+
function mapEntryEvent(input, page, sdkVersion) {
|
|
703
|
+
if (!page.type) {
|
|
704
|
+
warn('page.type is required — skipping entry event');
|
|
705
|
+
return null;
|
|
706
|
+
}
|
|
707
|
+
if (!page.url) {
|
|
708
|
+
warn('page.url is required — skipping entry event');
|
|
709
|
+
return null;
|
|
710
|
+
}
|
|
711
|
+
const entry = normalizeEntry(input);
|
|
712
|
+
if (!entry)
|
|
713
|
+
return null;
|
|
714
|
+
return {
|
|
715
|
+
event_type: 'custom',
|
|
716
|
+
client_timestamp: Math.floor(Date.now() / 1000),
|
|
717
|
+
sdk_version: sdkVersion,
|
|
718
|
+
page: withSophi(page, { entry }),
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
|
|
370
722
|
function mapUserEvent(user, page, sdkVersion, context) {
|
|
371
723
|
if (!user.consent) {
|
|
372
724
|
warn('user.consent is required — skipping identify event');
|
|
@@ -502,7 +854,7 @@ var SophiTracker = (function (exports) {
|
|
|
502
854
|
}
|
|
503
855
|
|
|
504
856
|
// Injected at build time via rollup-plugin-replace; falls back to package version.
|
|
505
|
-
const SDK_VERSION = '"1.3.
|
|
857
|
+
const SDK_VERSION = '"1.3.3"';
|
|
506
858
|
// ─── Default event types per object key ──────────────────────────────────────
|
|
507
859
|
const DEFAULT_EVENT_TYPE = {
|
|
508
860
|
page: 'page_view',
|
|
@@ -598,7 +950,36 @@ var SophiTracker = (function (exports) {
|
|
|
598
950
|
if (shouldLogCollection) {
|
|
599
951
|
log(`Collected: ${envelope.event_type} (triggered by: ${triggeredBy})`);
|
|
600
952
|
}
|
|
601
|
-
enqueue(envelope);
|
|
953
|
+
enqueue(withAttribution(envelope));
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
/**
|
|
957
|
+
* Attach campaign attribution under `page.custom._sophi.attribution` when it
|
|
958
|
+
* is enabled and known. Returns the envelope unchanged otherwise, so events
|
|
959
|
+
* stay byte-identical for integrations that have not opted in.
|
|
960
|
+
*/
|
|
961
|
+
function withAttribution(envelope) {
|
|
962
|
+
const attribution = getAttribution();
|
|
963
|
+
if (!attribution)
|
|
964
|
+
return envelope;
|
|
965
|
+
return Object.assign(Object.assign({}, envelope), { page: withSophi(envelope.page, { attribution }) });
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* Collect a Sophi button click (`SophiTracker.trackEntry` or a tagged element)
|
|
969
|
+
* and send it immediately. The click usually triggers a same-tab navigation,
|
|
970
|
+
* so it bypasses the debounced queue when a session is available.
|
|
971
|
+
*/
|
|
972
|
+
function collectEntry(entry, obj) {
|
|
973
|
+
var _a;
|
|
974
|
+
const page = obj === null || obj === void 0 ? void 0 : obj.page;
|
|
975
|
+
if (!page) {
|
|
976
|
+
warn(`sophi_object.page is required to send any event — skipping`);
|
|
977
|
+
return;
|
|
978
|
+
}
|
|
979
|
+
const envelope = mapEntryEvent(entry, page, SDK_VERSION);
|
|
980
|
+
if (envelope) {
|
|
981
|
+
log(`Collected: custom (entry: ${((_a = envelope.page.custom) === null || _a === void 0 ? void 0 : _a._sophi).entry.placement})`);
|
|
982
|
+
sendImmediate(withAttribution(envelope));
|
|
602
983
|
}
|
|
603
984
|
}
|
|
604
985
|
/**
|
|
@@ -690,14 +1071,6 @@ var SophiTracker = (function (exports) {
|
|
|
690
1071
|
function setState(s) {
|
|
691
1072
|
state = s;
|
|
692
1073
|
}
|
|
693
|
-
/**
|
|
694
|
-
* Reset all module-level state to initial values.
|
|
695
|
-
* For test isolation only — do not call in production code.
|
|
696
|
-
*/
|
|
697
|
-
function resetTrackerState() {
|
|
698
|
-
state = TrackerState.BUFFERING;
|
|
699
|
-
pendingQueue.length = 0;
|
|
700
|
-
}
|
|
701
1074
|
|
|
702
1075
|
// ─── Bootstrap guard ──────────────────────────────────────────────────────────
|
|
703
1076
|
/**
|
|
@@ -706,6 +1079,7 @@ var SophiTracker = (function (exports) {
|
|
|
706
1079
|
*/
|
|
707
1080
|
let bootstrapGuard = false;
|
|
708
1081
|
let unloadFlushRegistered = false;
|
|
1082
|
+
let entryClickListenerRegistered = false;
|
|
709
1083
|
// ─── Private push implementation ──────────────────────────────────────────────
|
|
710
1084
|
/**
|
|
711
1085
|
* Execute a push() call immediately against the current sophi_object state.
|
|
@@ -731,9 +1105,10 @@ var SophiTracker = (function (exports) {
|
|
|
731
1105
|
finally {
|
|
732
1106
|
endInternalPushSuppression();
|
|
733
1107
|
}
|
|
734
|
-
const
|
|
1108
|
+
const eventPartial = omitEmptyBasketOnRemove(partial);
|
|
1109
|
+
const explicitTrigger = resolveExplicitTriggeredKey(eventPartial);
|
|
735
1110
|
if (explicitTrigger) {
|
|
736
|
-
const scopedObj = buildExplicitScopedObject(
|
|
1111
|
+
const scopedObj = buildExplicitScopedObject(eventPartial, window.sophi_object);
|
|
737
1112
|
collectExplicit(explicitTrigger, scopedObj);
|
|
738
1113
|
}
|
|
739
1114
|
log(`Explicit push ${String((_a = partial.event_type) !== null && _a !== void 0 ? _a : 'custom')} queued once`);
|
|
@@ -821,6 +1196,13 @@ var SophiTracker = (function (exports) {
|
|
|
821
1196
|
initConfig(Object.assign(Object.assign({}, getConfig()), { userId }));
|
|
822
1197
|
resetSession();
|
|
823
1198
|
window.sophi_object.config.userId = userId !== null && userId !== void 0 ? userId : '';
|
|
1199
|
+
// Without storage consent the SDK stays passive: no session request and no
|
|
1200
|
+
// cookies. The new userId is kept in config, so setConsent(true) creates
|
|
1201
|
+
// the session for this user (and merges a guest session if one exists).
|
|
1202
|
+
if (!hasStorageConsent()) {
|
|
1203
|
+
log('updateUser: no storage consent — session will be created when consent is granted.');
|
|
1204
|
+
return;
|
|
1205
|
+
}
|
|
824
1206
|
void getSession();
|
|
825
1207
|
},
|
|
826
1208
|
/**
|
|
@@ -852,6 +1234,7 @@ var SophiTracker = (function (exports) {
|
|
|
852
1234
|
void (async () => {
|
|
853
1235
|
try {
|
|
854
1236
|
await getSession();
|
|
1237
|
+
persistAttribution();
|
|
855
1238
|
if (window.sophi_object) {
|
|
856
1239
|
collectAll(window.sophi_object);
|
|
857
1240
|
}
|
|
@@ -870,13 +1253,62 @@ var SophiTracker = (function (exports) {
|
|
|
870
1253
|
return;
|
|
871
1254
|
setStorageConsentState(false);
|
|
872
1255
|
clearSession();
|
|
1256
|
+
clearAttribution();
|
|
873
1257
|
log('Storage consent withdrawn — Web Object is now passive.');
|
|
874
1258
|
}
|
|
875
1259
|
},
|
|
1260
|
+
/**
|
|
1261
|
+
* Record a click on a Sophi entry point (a button or link that opens Sophi).
|
|
1262
|
+
* Sent as a `custom` event with the entry under `page.custom._sophi.entry`.
|
|
1263
|
+
*
|
|
1264
|
+
* Use this for flows where no element tagged with `data-sophi-placement` is
|
|
1265
|
+
* clicked (e.g. a search submitted with Enter). Do not combine it with the
|
|
1266
|
+
* data attributes on the same button, or the click is counted twice.
|
|
1267
|
+
*
|
|
1268
|
+
* @example
|
|
1269
|
+
* SophiTracker.trackEntry({ placement: 'header_search', group: 'search', type: 'search_bar' })
|
|
1270
|
+
*/
|
|
1271
|
+
trackEntry(entry) {
|
|
1272
|
+
if (getState() === TrackerState.FAILED) {
|
|
1273
|
+
warn('SophiTracker.trackEntry() called after bootstrap failure — ignoring.');
|
|
1274
|
+
return;
|
|
1275
|
+
}
|
|
1276
|
+
try {
|
|
1277
|
+
getConfig();
|
|
1278
|
+
}
|
|
1279
|
+
catch (_a) {
|
|
1280
|
+
warn('SophiTracker.trackEntry() called before initialisation — ignoring.');
|
|
1281
|
+
return;
|
|
1282
|
+
}
|
|
1283
|
+
if (!hasStorageConsent()) {
|
|
1284
|
+
log('trackEntry ignored — no storage consent.');
|
|
1285
|
+
return;
|
|
1286
|
+
}
|
|
1287
|
+
collectEntry(entry, window.sophi_object);
|
|
1288
|
+
},
|
|
876
1289
|
/** Expose session info for debugging */
|
|
877
1290
|
getSession,
|
|
878
1291
|
};
|
|
879
1292
|
// ─── Private helpers ──────────────────────────────────────────────────────────
|
|
1293
|
+
/**
|
|
1294
|
+
* Removing the last item leaves an empty basket. The external-events contract
|
|
1295
|
+
* rejects an empty `basket.line_items` and represents an empty basket by
|
|
1296
|
+
* omitting `basket`, so `remove_from_cart` is sent by its product without the
|
|
1297
|
+
* basket. Applies only to `remove_from_cart` with a product: anywhere else an
|
|
1298
|
+
* empty basket is an integration error and still skips with a warning.
|
|
1299
|
+
* Only the event changes — `sophi_object` keeps what the integrator pushed.
|
|
1300
|
+
*/
|
|
1301
|
+
function omitEmptyBasketOnRemove(partial) {
|
|
1302
|
+
var _a;
|
|
1303
|
+
if (partial.event_type !== 'remove_from_cart' || !partial.product)
|
|
1304
|
+
return partial;
|
|
1305
|
+
const lineItems = (_a = partial.basket) === null || _a === void 0 ? void 0 : _a.line_items;
|
|
1306
|
+
if (!Array.isArray(lineItems) || lineItems.length > 0)
|
|
1307
|
+
return partial;
|
|
1308
|
+
log('remove_from_cart: basket is empty — sending the event without basket.');
|
|
1309
|
+
const { basket: _emptyBasket } = partial, rest = __rest(partial, ["basket"]);
|
|
1310
|
+
return rest;
|
|
1311
|
+
}
|
|
880
1312
|
function resolveExplicitTriggeredKey(partial) {
|
|
881
1313
|
var _a;
|
|
882
1314
|
// Prefer specific commerce objects over page when multiple keys are present.
|
|
@@ -925,6 +1357,12 @@ var SophiTracker = (function (exports) {
|
|
|
925
1357
|
// no session, no cookies, no events — until SophiTracker.setConsent(true).
|
|
926
1358
|
const consentGranted = getConfig().storageConsent !== false;
|
|
927
1359
|
setStorageConsentState(consentGranted);
|
|
1360
|
+
// Campaign attribution: on unless config.attribution is false.
|
|
1361
|
+
// Held in memory without consent; persisted only with consent.
|
|
1362
|
+
captureAttribution();
|
|
1363
|
+
// Sophi button clicks on elements tagged with data-sophi-placement. Installed
|
|
1364
|
+
// in passive mode too; trackEntry() ignores clicks until consent is granted.
|
|
1365
|
+
registerEntryClickListener();
|
|
928
1366
|
if (!consentGranted) {
|
|
929
1367
|
log('Initialised in anonymous mode — no session, cookies, or events until consent is granted.');
|
|
930
1368
|
// Keep the object reactive so its state is current when consent arrives.
|
|
@@ -971,6 +1409,64 @@ var SophiTracker = (function (exports) {
|
|
|
971
1409
|
}
|
|
972
1410
|
});
|
|
973
1411
|
}
|
|
1412
|
+
/**
|
|
1413
|
+
* One capture-phase click listener on window. Capture runs before the page's
|
|
1414
|
+
* own handlers, so a `stopPropagation()` in an integrator's onclick cannot
|
|
1415
|
+
* hide the click. The innermost tagged element wins, so a chip nested inside a
|
|
1416
|
+
* tagged banner is counted once, as the chip. Never stops or cancels the click.
|
|
1417
|
+
*/
|
|
1418
|
+
function registerEntryClickListener() {
|
|
1419
|
+
if (entryClickListenerRegistered)
|
|
1420
|
+
return;
|
|
1421
|
+
entryClickListenerRegistered = true;
|
|
1422
|
+
window.addEventListener('click', handleEntryClick, true);
|
|
1423
|
+
}
|
|
1424
|
+
function handleEntryClick(event) {
|
|
1425
|
+
try {
|
|
1426
|
+
const entry = readEntryFromEvent(event);
|
|
1427
|
+
if (entry)
|
|
1428
|
+
SophiTracker.trackEntry(entry);
|
|
1429
|
+
}
|
|
1430
|
+
catch (err) {
|
|
1431
|
+
warn('Entry click tracking failed', err);
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* Finds the innermost element with `data-sophi-placement` on the event path
|
|
1436
|
+
* (composedPath also crosses open shadow roots) and reads `data-sophi-group`
|
|
1437
|
+
* and `data-sophi-type` from it or its nearest ancestor on the path.
|
|
1438
|
+
*/
|
|
1439
|
+
function readEntryFromEvent(event) {
|
|
1440
|
+
var _a;
|
|
1441
|
+
const path = [];
|
|
1442
|
+
if (typeof event.composedPath === 'function') {
|
|
1443
|
+
for (const node of event.composedPath()) {
|
|
1444
|
+
if (node instanceof Element)
|
|
1445
|
+
path.push(node);
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
else {
|
|
1449
|
+
let node = event.target instanceof Element ? event.target : null;
|
|
1450
|
+
while (node) {
|
|
1451
|
+
path.push(node);
|
|
1452
|
+
node = node.parentElement;
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
const index = path.findIndex((el) => el.hasAttribute('data-sophi-placement'));
|
|
1456
|
+
if (index === -1)
|
|
1457
|
+
return null;
|
|
1458
|
+
const inherited = (name) => { var _a, _b; return (_b = (_a = path.slice(index).find((el) => el.hasAttribute(name))) === null || _a === void 0 ? void 0 : _a.getAttribute(name)) !== null && _b !== void 0 ? _b : undefined; };
|
|
1459
|
+
const entry = {
|
|
1460
|
+
placement: (_a = path[index].getAttribute('data-sophi-placement')) !== null && _a !== void 0 ? _a : '',
|
|
1461
|
+
};
|
|
1462
|
+
const group = inherited('data-sophi-group');
|
|
1463
|
+
const type = inherited('data-sophi-type');
|
|
1464
|
+
if (group !== undefined)
|
|
1465
|
+
entry.group = group;
|
|
1466
|
+
if (type !== undefined)
|
|
1467
|
+
entry.type = type;
|
|
1468
|
+
return entry;
|
|
1469
|
+
}
|
|
974
1470
|
// ─── Stub adoption ────────────────────────────────────────────────────────────
|
|
975
1471
|
/**
|
|
976
1472
|
* Inspect the existing window.SophiTracker value before replacing it.
|
|
@@ -1020,20 +1516,6 @@ var SophiTracker = (function (exports) {
|
|
|
1020
1516
|
else {
|
|
1021
1517
|
void bootstrap();
|
|
1022
1518
|
}
|
|
1023
|
-
/** Reset the bootstrap guard between tests. */
|
|
1024
|
-
function _resetBootstrapGuard() {
|
|
1025
|
-
bootstrapGuard = false;
|
|
1026
|
-
unloadFlushRegistered = false;
|
|
1027
|
-
}
|
|
1028
|
-
|
|
1029
|
-
exports._bootstrap = bootstrap;
|
|
1030
|
-
exports._resetBootstrapGuard = _resetBootstrapGuard;
|
|
1031
|
-
exports.default = SophiTracker;
|
|
1032
|
-
exports.resetTrackerState = resetTrackerState;
|
|
1033
|
-
|
|
1034
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
1035
|
-
|
|
1036
|
-
return exports;
|
|
1037
1519
|
|
|
1038
|
-
})(
|
|
1520
|
+
})();
|
|
1039
1521
|
//# sourceMappingURL=sophi.js.map
|