@datalyr/web 1.2.1 → 1.3.0

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @datalyr/web v1.2.1
2
+ * @datalyr/web v1.3.0
3
3
  * Datalyr Web SDK - Modern attribution tracking for web applications
4
4
  * (c) 2026 Datalyr Inc.
5
5
  * Released under the MIT License
@@ -729,8 +729,36 @@ function getRootDomain() {
729
729
  // Handle .co.uk, .com.au, etc
730
730
  const tld = parts[parts.length - 1];
731
731
  const sld = parts[parts.length - 2];
732
- // Common two-part TLDs
733
- const twoPartTlds = ['co.uk', 'com.au', 'co.nz', 'co.jp', 'co.in', 'co.za'];
732
+ // Common two-part TLDs (country-specific domains)
733
+ // Note: The CookieStorage.getAutoDomain() probe method handles unknown TLDs dynamically,
734
+ // but this list provides faster resolution for known patterns
735
+ const twoPartTlds = [
736
+ // United Kingdom
737
+ 'co.uk', 'org.uk', 'net.uk', 'ac.uk', 'gov.uk', 'me.uk',
738
+ // Australia
739
+ 'com.au', 'net.au', 'org.au', 'edu.au', 'gov.au',
740
+ // New Zealand
741
+ 'co.nz', 'net.nz', 'org.nz', 'govt.nz',
742
+ // Japan
743
+ 'co.jp', 'ne.jp', 'or.jp', 'ac.jp', 'go.jp',
744
+ // India
745
+ 'co.in', 'net.in', 'org.in', 'gov.in', 'ac.in',
746
+ // South Africa
747
+ 'co.za', 'net.za', 'org.za', 'gov.za',
748
+ // Brazil
749
+ 'com.br', 'net.br', 'org.br', 'gov.br', 'edu.br',
750
+ // South Korea
751
+ 'co.kr', 'ne.kr', 'or.kr', 'go.kr', 'ac.kr',
752
+ // China
753
+ 'com.cn', 'net.cn', 'org.cn', 'gov.cn', 'edu.cn',
754
+ // Other Asia-Pacific
755
+ 'co.id', 'co.th', 'com.sg', 'com.my', 'com.ph', 'com.vn',
756
+ 'com.tw', 'com.hk',
757
+ // Latin America
758
+ 'com.mx', 'com.ar', 'com.co', 'com.pe', 'com.cl',
759
+ // Europe
760
+ 'co.il', 'co.at', 'co.hu', 'co.pl'
761
+ ];
734
762
  const lastTwo = `${sld}.${tld}`;
735
763
  if (twoPartTlds.includes(lastTwo) && parts.length >= 3) {
736
764
  return '.' + parts.slice(-3).join('.');
@@ -802,6 +830,22 @@ class IdentityManager {
802
830
  * Get or create anonymous ID (device/browser identifier)
803
831
  */
804
832
  getOrCreateAnonymousId() {
833
+ // 0. Check URL parameter first (cross-domain linking via _dl_vid)
834
+ // This enables tracking continuity when users navigate between different root domains
835
+ try {
836
+ const urlParams = new URLSearchParams(window.location.search);
837
+ const urlVisitorId = urlParams.get('_dl_vid');
838
+ if (urlVisitorId && urlVisitorId.startsWith('anon_')) {
839
+ // Valid visitor ID from URL - use it and persist
840
+ this.setRootDomainCookie('__dl_visitor_id', urlVisitorId);
841
+ storage.set('dl_anonymous_id', urlVisitorId);
842
+ return urlVisitorId;
843
+ }
844
+ }
845
+ catch (e) {
846
+ // URL parsing failed - continue with cookie/localStorage checks
847
+ console.warn('[Datalyr] Failed to parse URL for _dl_vid:', e);
848
+ }
805
849
  // 1. Check root domain cookie first (works across subdomains)
806
850
  let anonymousId = cookies.get('__dl_visitor_id');
807
851
  if (anonymousId) {
@@ -3512,6 +3556,60 @@ class Datalyr {
3512
3556
  this.trackError(error, { event: eventName });
3513
3557
  }
3514
3558
  }
3559
+ /**
3560
+ * Track an app download click and redirect to the app store.
3561
+ * Fires a $app_download_click event with full attribution data,
3562
+ * then redirects the user to the appropriate store URL.
3563
+ * For Android, encodes attribution params into the Play Store referrer
3564
+ * so the mobile SDK can retrieve them deterministically after install.
3565
+ */
3566
+ trackAppDownloadClick(options) {
3567
+ if (!this.initialized) {
3568
+ console.warn('[Datalyr] SDK not initialized. Call init() first.');
3569
+ return;
3570
+ }
3571
+ if (!this.shouldTrack())
3572
+ return;
3573
+ // Fire the event with target platform info — attribution data is
3574
+ // automatically merged by createEventPayload via getAttributionData()
3575
+ this.track('$app_download_click', {
3576
+ target_platform: options.targetPlatform,
3577
+ app_store_url: options.appStoreUrl,
3578
+ });
3579
+ // Flush immediately via sendBeacon before page navigates away
3580
+ this.queue.forceFlush();
3581
+ // For Android: append referrer param to Play Store URL with click attribution
3582
+ if (options.targetPlatform === 'android' && options.appStoreUrl.includes('play.google.com')) {
3583
+ const lastTouch = this.attribution.getLastTouch();
3584
+ const referrerParams = new URLSearchParams();
3585
+ if (lastTouch === null || lastTouch === void 0 ? void 0 : lastTouch.clickId)
3586
+ referrerParams.set('dl_click_id', lastTouch.clickId);
3587
+ if (lastTouch === null || lastTouch === void 0 ? void 0 : lastTouch.clickIdType)
3588
+ referrerParams.set('dl_click_id_type', lastTouch.clickIdType);
3589
+ if (lastTouch === null || lastTouch === void 0 ? void 0 : lastTouch.source)
3590
+ referrerParams.set('utm_source', lastTouch.source);
3591
+ if (lastTouch === null || lastTouch === void 0 ? void 0 : lastTouch.medium)
3592
+ referrerParams.set('utm_medium', lastTouch.medium);
3593
+ if (lastTouch === null || lastTouch === void 0 ? void 0 : lastTouch.campaign)
3594
+ referrerParams.set('utm_campaign', lastTouch.campaign);
3595
+ if (lastTouch === null || lastTouch === void 0 ? void 0 : lastTouch.content)
3596
+ referrerParams.set('utm_content', lastTouch.content);
3597
+ if (lastTouch === null || lastTouch === void 0 ? void 0 : lastTouch.term)
3598
+ referrerParams.set('utm_term', lastTouch.term);
3599
+ try {
3600
+ const url = new URL(options.appStoreUrl);
3601
+ url.searchParams.set('referrer', referrerParams.toString());
3602
+ window.location.href = url.toString();
3603
+ }
3604
+ catch (_a) {
3605
+ // If URL parsing fails, redirect without referrer
3606
+ window.location.href = options.appStoreUrl;
3607
+ }
3608
+ }
3609
+ else {
3610
+ window.location.href = options.appStoreUrl;
3611
+ }
3612
+ }
3515
3613
  /**
3516
3614
  * Identify a user
3517
3615
  */