@datalyr/web 1.0.6 → 1.1.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.
package/dist/datalyr.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @datalyr/web v1.0.6
2
+ * @datalyr/web v1.1.0
3
3
  * Datalyr Web SDK - Modern attribution tracking for web applications
4
4
  * (c) 2025 Datalyr Inc.
5
5
  * Released under the MIT License
@@ -164,7 +164,16 @@ var Datalyr = (function () {
164
164
  const value = `; ${document.cookie}`;
165
165
  const parts = value.split(`; ${name}=`);
166
166
  if (parts.length === 2) {
167
- return ((_a = parts.pop()) === null || _a === void 0 ? void 0 : _a.split(';').shift()) || null;
167
+ const rawValue = ((_a = parts.pop()) === null || _a === void 0 ? void 0 : _a.split(';').shift()) || null;
168
+ if (rawValue) {
169
+ try {
170
+ return decodeURIComponent(rawValue);
171
+ }
172
+ catch (_b) {
173
+ // Return raw value if decoding fails (backwards compatibility)
174
+ return rawValue;
175
+ }
176
+ }
168
177
  }
169
178
  return null;
170
179
  }
@@ -398,6 +407,33 @@ var Datalyr = (function () {
398
407
  return navigator.globalPrivacyControl === true ||
399
408
  window.globalPrivacyControl === true;
400
409
  }
410
+ /**
411
+ * Get root domain for cross-subdomain tracking
412
+ */
413
+ function getRootDomain() {
414
+ const hostname = window.location.hostname;
415
+ // Handle localhost and IP addresses
416
+ if (hostname === 'localhost' ||
417
+ hostname.match(/^[0-9]{1,3}\./) || // IPv4
418
+ hostname.match(/^\[?[0-9a-fA-F:]+\]?$/)) { // IPv6
419
+ return hostname;
420
+ }
421
+ // Get root domain (last two parts: example.com)
422
+ const parts = hostname.split('.');
423
+ if (parts.length >= 2) {
424
+ // Handle .co.uk, .com.au, etc
425
+ const tld = parts[parts.length - 1];
426
+ const sld = parts[parts.length - 2];
427
+ // Common two-part TLDs
428
+ const twoPartTlds = ['co.uk', 'com.au', 'co.nz', 'co.jp', 'co.in', 'co.za'];
429
+ const lastTwo = `${sld}.${tld}`;
430
+ if (twoPartTlds.includes(lastTwo) && parts.length >= 3) {
431
+ return '.' + parts.slice(-3).join('.');
432
+ }
433
+ return '.' + parts.slice(-2).join('.');
434
+ }
435
+ return hostname;
436
+ }
401
437
  /**
402
438
  * Get referrer data
403
439
  */
@@ -461,13 +497,61 @@ var Datalyr = (function () {
461
497
  * Get or create anonymous ID (device/browser identifier)
462
498
  */
463
499
  getOrCreateAnonymousId() {
464
- let anonymousId = storage.get('dl_anonymous_id');
465
- if (!anonymousId) {
466
- anonymousId = `anon_${generateUUID()}`;
500
+ // 1. Check root domain cookie first (works across subdomains)
501
+ let anonymousId = cookies.get('__dl_visitor_id');
502
+ if (anonymousId) {
503
+ // Found in cookie - sync to localStorage
467
504
  storage.set('dl_anonymous_id', anonymousId);
468
- }
505
+ return anonymousId;
506
+ }
507
+ // 2. Check localStorage (fallback for cookie issues)
508
+ anonymousId = storage.get('dl_anonymous_id');
509
+ if (anonymousId) {
510
+ // Found in localStorage - set root domain cookie
511
+ this.setRootDomainCookie('__dl_visitor_id', anonymousId);
512
+ return anonymousId;
513
+ }
514
+ // 3. Generate new ID
515
+ anonymousId = `anon_${generateUUID()}`;
516
+ // 4. Store in both cookie (primary) and localStorage (backup)
517
+ this.setRootDomainCookie('__dl_visitor_id', anonymousId);
518
+ storage.set('dl_anonymous_id', anonymousId);
469
519
  return anonymousId;
470
520
  }
521
+ /**
522
+ * Set a root domain cookie for cross-subdomain tracking
523
+ */
524
+ setRootDomainCookie(name, value) {
525
+ try {
526
+ const rootDomain = getRootDomain();
527
+ const secure = location.protocol === 'https:' ? '; Secure' : '';
528
+ const encodedValue = encodeURIComponent(value);
529
+ // Set cookie with root domain, 1 year expiry
530
+ document.cookie = `${name}=${encodedValue}; domain=${rootDomain}; path=/; max-age=31536000; SameSite=Lax${secure}`;
531
+ // Verify cookie was set successfully (cookies.get already decodes)
532
+ const verifyValue = cookies.get(name);
533
+ if (verifyValue === value) {
534
+ console.log(`[Datalyr] Set root domain cookie: ${name} on domain: ${rootDomain}`);
535
+ }
536
+ else {
537
+ // Fallback: try without domain (current subdomain only)
538
+ document.cookie = `${name}=${encodedValue}; path=/; max-age=31536000; SameSite=Lax${secure}`;
539
+ console.log(`[Datalyr] Set cookie without domain (fallback): ${name}`);
540
+ }
541
+ }
542
+ catch (e) {
543
+ console.error('[Datalyr] Error setting root domain cookie:', e);
544
+ // Still try to set without domain as fallback
545
+ try {
546
+ const secure = location.protocol === 'https:' ? '; Secure' : '';
547
+ const encodedValue = encodeURIComponent(value);
548
+ document.cookie = `${name}=${encodedValue}; path=/; max-age=31536000; SameSite=Lax${secure}`;
549
+ }
550
+ catch (fallbackError) {
551
+ console.error('[Datalyr] Failed to set cookie even without domain:', fallbackError);
552
+ }
553
+ }
554
+ }
471
555
  /**
472
556
  * Get stored user ID from previous session
473
557
  */
@@ -561,6 +645,8 @@ var Datalyr = (function () {
561
645
  // Generate new anonymous ID for privacy
562
646
  this.anonymousId = `anon_${generateUUID()}`;
563
647
  storage.set('dl_anonymous_id', this.anonymousId);
648
+ // Update root domain cookie with new ID
649
+ this.setRootDomainCookie('__dl_visitor_id', this.anonymousId);
564
650
  }
565
651
  /**
566
652
  * Get all identity fields for event payload
@@ -1540,24 +1626,40 @@ var Datalyr = (function () {
1540
1626
  }
1541
1627
  /**
1542
1628
  * Collect minimal fingerprint data (privacy-friendly)
1629
+ * Matches browser tag minimal fingerprinting approach
1543
1630
  */
1544
1631
  collectMinimal() {
1545
- return {
1632
+ const fp = {
1546
1633
  timezone: this.getTimezone(),
1547
1634
  language: navigator.language || null,
1548
- platform: navigator.platform || null,
1549
- canvasEnabled: false,
1550
- localStorageAvailable: this.testStorage('localStorage'),
1551
- sessionStorageAvailable: this.testStorage('sessionStorage')
1635
+ screen_bucket: this.getScreenBucket(),
1636
+ dnt: (navigator.doNotTrack === '1' || window.globalPrivacyControl === true) || null,
1637
+ userAgent: navigator.userAgent || null
1552
1638
  };
1639
+ // User-Agent Client Hints (modern browsers)
1640
+ if ('userAgentData' in navigator) {
1641
+ const uaData = navigator.userAgentData;
1642
+ fp.userAgentData = {
1643
+ brands: uaData.brands || [],
1644
+ mobile: uaData.mobile || false,
1645
+ platform: uaData.platform || null
1646
+ };
1647
+ }
1648
+ return fp;
1553
1649
  }
1554
1650
  /**
1555
1651
  * Collect standard fingerprint data
1652
+ * PRIVACY: Minimal data collection matching browser tag approach
1653
+ * Only collects data necessary for basic analytics and attribution
1556
1654
  */
1557
1655
  collectStandard() {
1558
1656
  const fingerprint = {};
1559
1657
  try {
1560
- // Basic browser data
1658
+ // PRIVACY: Only collect minimal data needed for attribution
1659
+ fingerprint.timezone = this.getTimezone();
1660
+ fingerprint.language = navigator.language || null;
1661
+ fingerprint.screen_bucket = this.getScreenBucket();
1662
+ fingerprint.dnt = (navigator.doNotTrack === '1' || window.globalPrivacyControl === true) || null;
1561
1663
  fingerprint.userAgent = navigator.userAgent || null;
1562
1664
  // User-Agent Client Hints (modern browsers)
1563
1665
  if ('userAgentData' in navigator) {
@@ -1568,125 +1670,14 @@ var Datalyr = (function () {
1568
1670
  platform: uaData.platform || null
1569
1671
  };
1570
1672
  }
1571
- // Language settings
1572
- fingerprint.language = navigator.language || null;
1573
- fingerprint.languages = navigator.languages ?
1574
- navigator.languages.slice(0, 2) : null; // Limit to 2 for privacy
1575
- // Platform and browser features
1576
- fingerprint.platform = navigator.platform || null;
1577
- fingerprint.cookieEnabled = navigator.cookieEnabled || null;
1578
- fingerprint.doNotTrack = navigator.doNotTrack || null;
1579
- // Hardware (coarsened for privacy)
1580
- fingerprint.hardwareConcurrency = this.coarsenHardwareConcurrency();
1581
- fingerprint.deviceMemory = this.coarsenDeviceMemory();
1582
- fingerprint.maxTouchPoints = navigator.maxTouchPoints > 0 ? 'touch' : 'no-touch';
1583
- // Screen (coarsened)
1584
- fingerprint.screenResolution = this.getScreenResolution();
1585
- fingerprint.colorDepth = screen.colorDepth || null;
1586
- fingerprint.pixelRatio = this.coarsenPixelRatio();
1587
- // Timezone
1588
- fingerprint.timezone = this.getTimezone();
1589
- fingerprint.timezoneOffset = this.coarsenTimezoneOffset();
1590
- // Canvas fingerprinting disabled for privacy
1591
- fingerprint.canvasEnabled = false;
1592
- // Plugins count only (not details for privacy)
1593
- fingerprint.pluginsCount = navigator.plugins ? navigator.plugins.length : null;
1594
- // Storage availability
1595
- fingerprint.localStorageAvailable = this.testStorage('localStorage');
1596
- fingerprint.sessionStorageAvailable = this.testStorage('sessionStorage');
1597
- fingerprint.indexedDBAvailable = this.testIndexedDB();
1598
- // Add cached heavy fingerprint data if available
1599
- if (this.heavyFingerprintDone) {
1600
- Object.assign(fingerprint, this.fingerprintCache);
1601
- }
1602
1673
  }
1603
1674
  catch (e) {
1604
1675
  console.warn('[Datalyr] Error collecting fingerprint:', e);
1605
1676
  }
1606
1677
  return fingerprint;
1607
1678
  }
1608
- /**
1609
- * Collect heavy fingerprint data (WebGL, Audio)
1610
- * Called lazily on first event to improve page load performance
1611
- */
1612
- collectHeavyFingerprint() {
1613
- return __awaiter(this, void 0, void 0, function* () {
1614
- if (this.heavyFingerprintDone || this.privacyMode === 'strict') {
1615
- return;
1616
- }
1617
- try {
1618
- // WebGL fingerprinting
1619
- const webglData = this.getWebGLFingerprint();
1620
- if (webglData) {
1621
- this.fingerprintCache.webglVendor = webglData.vendor;
1622
- this.fingerprintCache.webglRenderer = webglData.renderer;
1623
- }
1624
- // Audio fingerprinting
1625
- const audioData = yield this.getAudioFingerprint();
1626
- if (audioData) {
1627
- this.fingerprintCache.audioSampleRate = audioData.sampleRate;
1628
- this.fingerprintCache.audioState = audioData.state;
1629
- this.fingerprintCache.audioMaxChannels = audioData.maxChannels;
1630
- }
1631
- this.heavyFingerprintDone = true;
1632
- }
1633
- catch (e) {
1634
- console.warn('[Datalyr] Heavy fingerprinting failed:', e);
1635
- }
1636
- });
1637
- }
1638
- /**
1639
- * Get WebGL fingerprint
1640
- */
1641
- getWebGLFingerprint() {
1642
- try {
1643
- const canvas = document.createElement('canvas');
1644
- const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
1645
- if (!gl)
1646
- return null;
1647
- const debugInfo = gl.getExtension('WEBGL_debug_renderer_info');
1648
- if (!debugInfo) {
1649
- return {
1650
- vendor: gl.getParameter(gl.VENDOR) || 'unknown',
1651
- renderer: gl.getParameter(gl.RENDERER) || 'unknown'
1652
- };
1653
- }
1654
- return {
1655
- vendor: gl.getParameter(debugInfo.UNMASKED_VENDOR_WEBGL) || 'unknown',
1656
- renderer: gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL) || 'unknown'
1657
- };
1658
- }
1659
- catch (_a) {
1660
- return null;
1661
- }
1662
- }
1663
- /**
1664
- * Get audio fingerprint
1665
- */
1666
- getAudioFingerprint() {
1667
- return __awaiter(this, void 0, void 0, function* () {
1668
- var _a;
1669
- try {
1670
- const AudioContext = window.AudioContext || window.webkitAudioContext;
1671
- if (!AudioContext)
1672
- return null;
1673
- const audioCtx = new AudioContext();
1674
- const result = {
1675
- sampleRate: audioCtx.sampleRate || null,
1676
- state: audioCtx.state || null,
1677
- maxChannels: ((_a = audioCtx.destination) === null || _a === void 0 ? void 0 : _a.maxChannelCount) || null
1678
- };
1679
- // Close context to free resources
1680
- if (audioCtx.close) {
1681
- yield audioCtx.close();
1682
- }
1683
- return result;
1684
- }
1685
- catch (_b) {
1686
- return null;
1687
- }
1688
- });
1689
- }
1679
+ // PRIVACY: Heavy fingerprinting (WebGL, Audio) removed
1680
+ // Minimal fingerprinting only for privacy compliance
1690
1681
  /**
1691
1682
  * Get timezone
1692
1683
  */
@@ -1699,10 +1690,13 @@ var Datalyr = (function () {
1699
1690
  }
1700
1691
  }
1701
1692
  /**
1702
- * Get screen resolution (coarsened)
1693
+ * Get coarse screen bucket for basic device classification
1694
+ * Rounds to nearest 100px for privacy
1703
1695
  */
1704
- getScreenResolution() {
1696
+ getScreenBucket() {
1705
1697
  try {
1698
+ if (!window.screen)
1699
+ return null;
1706
1700
  const width = Math.round(screen.width / 100) * 100;
1707
1701
  const height = Math.round(screen.height / 100) * 100;
1708
1702
  return `${width}x${height}`;
@@ -1711,96 +1705,8 @@ var Datalyr = (function () {
1711
1705
  return null;
1712
1706
  }
1713
1707
  }
1714
- /**
1715
- * Coarsen hardware concurrency for privacy
1716
- */
1717
- coarsenHardwareConcurrency() {
1718
- try {
1719
- const cores = navigator.hardwareConcurrency;
1720
- if (!cores)
1721
- return null;
1722
- return cores > 8 ? '8+' : cores;
1723
- }
1724
- catch (_a) {
1725
- return null;
1726
- }
1727
- }
1728
- /**
1729
- * Coarsen device memory for privacy
1730
- */
1731
- coarsenDeviceMemory() {
1732
- try {
1733
- const memory = navigator.deviceMemory;
1734
- if (!memory)
1735
- return null;
1736
- return memory > 4 ? '4+' : memory;
1737
- }
1738
- catch (_a) {
1739
- return null;
1740
- }
1741
- }
1742
- /**
1743
- * Coarsen pixel ratio for privacy
1744
- */
1745
- coarsenPixelRatio() {
1746
- try {
1747
- const ratio = window.devicePixelRatio;
1748
- if (!ratio)
1749
- return null;
1750
- // Round to common values
1751
- if (ratio <= 1)
1752
- return '1';
1753
- if (ratio <= 1.5)
1754
- return '1.5';
1755
- if (ratio <= 2)
1756
- return '2';
1757
- if (ratio <= 3)
1758
- return '3';
1759
- return '3+';
1760
- }
1761
- catch (_a) {
1762
- return null;
1763
- }
1764
- }
1765
- /**
1766
- * Coarsen timezone offset for privacy
1767
- */
1768
- coarsenTimezoneOffset() {
1769
- try {
1770
- const offset = new Date().getTimezoneOffset();
1771
- // Round to nearest 30 minutes
1772
- return Math.round(offset / 30) * 30;
1773
- }
1774
- catch (_a) {
1775
- return null;
1776
- }
1777
- }
1778
- /**
1779
- * Test if storage is available
1780
- */
1781
- testStorage(type) {
1782
- try {
1783
- const storage = window[type];
1784
- const testKey = '__dl_test__';
1785
- storage.setItem(testKey, '1');
1786
- storage.removeItem(testKey);
1787
- return true;
1788
- }
1789
- catch (_a) {
1790
- return false;
1791
- }
1792
- }
1793
- /**
1794
- * Test if IndexedDB is available
1795
- */
1796
- testIndexedDB() {
1797
- try {
1798
- return !!window.indexedDB;
1799
- }
1800
- catch (_a) {
1801
- return false;
1802
- }
1803
- }
1708
+ // PRIVACY: Unused helper methods removed (coarsen functions, storage tests)
1709
+ // Minimal fingerprinting only
1804
1710
  /**
1805
1711
  * Generate fingerprint hash
1806
1712
  */
@@ -2440,13 +2346,7 @@ var Datalyr = (function () {
2440
2346
  if (!this.shouldTrack())
2441
2347
  return;
2442
2348
  try {
2443
- // Collect heavy fingerprint on first event (lazy loading)
2444
- if (!this.heavyFingerprintCollected && this.config.enableFingerprinting) {
2445
- this.heavyFingerprintCollected = true;
2446
- this.fingerprint.collectHeavyFingerprint().catch(err => {
2447
- this.log('Heavy fingerprint collection failed:', err);
2448
- });
2449
- }
2349
+ // PRIVACY: Heavy fingerprinting removed - using minimal fingerprinting only
2450
2350
  // Update session activity
2451
2351
  this.session.updateActivity(eventName);
2452
2352
  // Create event payload