@esri/telemetry-amazon 6.0.0-beta.3 → 6.0.0-beta.5

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.
Files changed (39) hide show
  1. package/dist/esm/index.js +2 -2
  2. package/dist/esm/index.js.map +1 -1
  3. package/dist/esm/index.test.js +17 -0
  4. package/dist/esm/index.test.js.map +1 -1
  5. package/dist/esm/plugins/esri.js +320 -80
  6. package/dist/esm/plugins/esri.js.map +1 -1
  7. package/dist/esm/plugins/esri.test.js +436 -29
  8. package/dist/esm/plugins/esri.test.js.map +1 -1
  9. package/dist/esm/plugins/shared.js +1 -0
  10. package/dist/esm/plugins/shared.js.map +1 -1
  11. package/dist/esm/plugins/utils/browser.js +97 -0
  12. package/dist/esm/plugins/utils/browser.js.map +1 -0
  13. package/dist/esm/plugins/utils/os.js +42 -0
  14. package/dist/esm/plugins/utils/os.js.map +1 -0
  15. package/dist/node/index.js +2 -2
  16. package/dist/node/index.js.map +1 -1
  17. package/dist/node/index.test.js +17 -0
  18. package/dist/node/index.test.js.map +1 -1
  19. package/dist/node/plugins/esri.js +319 -79
  20. package/dist/node/plugins/esri.js.map +1 -1
  21. package/dist/node/plugins/esri.test.js +437 -29
  22. package/dist/node/plugins/esri.test.js.map +1 -1
  23. package/dist/node/plugins/shared.js +3 -1
  24. package/dist/node/plugins/shared.js.map +1 -1
  25. package/dist/node/plugins/utils/browser.js +101 -0
  26. package/dist/node/plugins/utils/browser.js.map +1 -0
  27. package/dist/node/plugins/utils/os.js +46 -0
  28. package/dist/node/plugins/utils/os.js.map +1 -0
  29. package/dist/types/index.d.ts +2 -2
  30. package/dist/types/plugins/esri.d.ts +2 -1
  31. package/dist/types/plugins/shared.d.ts +1 -0
  32. package/dist/types/plugins/utils/browser.d.ts +12 -0
  33. package/dist/types/plugins/utils/os.d.ts +5 -0
  34. package/dist/types/types.d.ts +7 -2
  35. package/dist/umd/telemetry-amazon.js +458 -81
  36. package/dist/umd/telemetry-amazon.js.map +1 -1
  37. package/dist/umd/telemetry-amazon.min.js +458 -81
  38. package/dist/umd/telemetry-amazon.min.js.map +1 -1
  39. package/package.json +1 -1
@@ -1667,6 +1667,144 @@
1667
1667
  return [HOST_SERVICES[service] || service, region || '']
1668
1668
  }
1669
1669
 
1670
+ function parseOS(userAgent) {
1671
+ var _a;
1672
+ const cleanAgent = userAgent.toLowerCase().replace(/ /g, '');
1673
+ const osMatchers = [
1674
+ {
1675
+ name: 'Windows',
1676
+ regex: /windowsnt/i,
1677
+ versionRegex: /windowsnt([\d\.]+)/i,
1678
+ },
1679
+ {
1680
+ name: 'MacOS',
1681
+ regex: /macos|macosx|macintosh|macintel|darwin/i,
1682
+ versionRegex: /osx([\d_\.]+)/i,
1683
+ },
1684
+ {
1685
+ name: 'Android',
1686
+ regex: /android/i,
1687
+ versionRegex: /android([\d\.]+)/i,
1688
+ },
1689
+ {
1690
+ name: 'iOS',
1691
+ regex: /iphone|ipad|ipod/i,
1692
+ versionRegex: /os([\d_\.]+)/i,
1693
+ },
1694
+ {
1695
+ name: 'Linux',
1696
+ regex: /linux/i,
1697
+ },
1698
+ ];
1699
+ const matched = osMatchers.find((entry) => entry.regex.test(cleanAgent));
1700
+ if (!matched) {
1701
+ return {};
1702
+ }
1703
+ const version = matched.versionRegex
1704
+ ? (_a = (cleanAgent.match(matched.versionRegex) || [])[1]) === null || _a === void 0 ? void 0 : _a.replace(/_/g, '.')
1705
+ : undefined;
1706
+ return {
1707
+ name: matched.name,
1708
+ version,
1709
+ };
1710
+ }
1711
+
1712
+ function getNavigatorSafe() {
1713
+ if (typeof window === 'undefined' || !window.navigator) {
1714
+ return undefined;
1715
+ }
1716
+ return window.navigator;
1717
+ }
1718
+ function getLanguageInfo(nav) {
1719
+ if (!nav) {
1720
+ return '';
1721
+ }
1722
+ const lang = nav.language ||
1723
+ nav.browserLanguage ||
1724
+ (Array.isArray(nav.languages) && nav.languages.length > 0
1725
+ ? nav.languages[0]
1726
+ : 'en_US') ||
1727
+ '';
1728
+ return String(lang).toLowerCase().replace('-', '_');
1729
+ }
1730
+ function getBrowserTimezone() {
1731
+ const tzMatch = /\(([A-Za-z\s].*)\)/.exec(new Date().toString());
1732
+ return tzMatch ? tzMatch[1] || '' : '';
1733
+ }
1734
+ function parseBrowserType(userAgent) {
1735
+ const operaMatch = /.+(Opera[\s[A-Z]*|OPR[\sA-Z]*)\/([0-9\.]+).*/i.exec(userAgent);
1736
+ if (operaMatch) {
1737
+ return {
1738
+ type: operaMatch[1],
1739
+ version: operaMatch[2],
1740
+ };
1741
+ }
1742
+ const edgeOrTridentMatch = /.+(Trident|Edge)\/([0-9\.]+).*/i.exec(userAgent);
1743
+ if (edgeOrTridentMatch) {
1744
+ return {
1745
+ type: edgeOrTridentMatch[1],
1746
+ version: edgeOrTridentMatch[2],
1747
+ };
1748
+ }
1749
+ const headlessMatch = /(headlesschrome)(?:\/([\w\.]+)| )/i.exec(userAgent);
1750
+ if (headlessMatch) {
1751
+ return {
1752
+ type: headlessMatch[1],
1753
+ version: headlessMatch[2],
1754
+ };
1755
+ }
1756
+ const browserMatch = /.+(Chrome|Firefox|FxiOS)\/([0-9\.]+).*/i.exec(userAgent);
1757
+ if (browserMatch) {
1758
+ return {
1759
+ type: browserMatch[1],
1760
+ version: browserMatch[2],
1761
+ };
1762
+ }
1763
+ const safariMatch = /.+(Safari)\/([0-9\.]+).*/i.exec(userAgent);
1764
+ if (safariMatch) {
1765
+ return {
1766
+ type: safariMatch[1],
1767
+ version: safariMatch[2],
1768
+ };
1769
+ }
1770
+ const webkitMatch = /.+(AppleWebKit)\/([0-9\.]+).*/i.exec(userAgent);
1771
+ if (webkitMatch) {
1772
+ return {
1773
+ type: webkitMatch[1],
1774
+ version: webkitMatch[2],
1775
+ };
1776
+ }
1777
+ const anyMatch = /.*([A-Z]+)\/([0-9\.]+).*/i.exec(userAgent);
1778
+ const fallback = anyMatch || ['', 'NA', '0.0.0'];
1779
+ return {
1780
+ type: fallback[1],
1781
+ version: fallback[2],
1782
+ };
1783
+ }
1784
+ function getBrowserClientInfo() {
1785
+ const nav = getNavigatorSafe();
1786
+ if (!nav) {
1787
+ return {};
1788
+ }
1789
+ const userAgent = nav.userAgent || '';
1790
+ const browser = parseBrowserType(userAgent);
1791
+ const isBrave = (nav.brave && nav.brave.isBrave) || false;
1792
+ const type = isBrave ? 'Brave' : browser.type;
1793
+ const os = parseOS(userAgent);
1794
+ const vendorToken = (nav.vendor || '').split(' ').filter(Boolean)[0];
1795
+ const make = type === 'Brave' ? type : vendorToken || nav.product || '';
1796
+ return {
1797
+ language: getLanguageInfo(nav),
1798
+ make,
1799
+ model: type,
1800
+ version: browser.version,
1801
+ name: [type, browser.version].filter(Boolean).join('/'),
1802
+ timezone: getBrowserTimezone(),
1803
+ platform: nav.platform,
1804
+ os,
1805
+ };
1806
+ }
1807
+
1670
1808
  // Shared utilities for plugins
1671
1809
  const PINPOINT_ENDPOINT_KEY = 'TELEMETRY_PINPOINT_ENDPOINT_ID';
1672
1810
  const LOG_PREFIX = '[telemetry-amazon]';
@@ -1718,6 +1856,10 @@
1718
1856
  const ESRI_SIGNING_SERVICE = 'execute-api';
1719
1857
  const ESRI_SIGNING_REGION = 'us-east-1';
1720
1858
  const ESRI_ENDPOINT_URL = (appId) => `https://tedev.arcgis.com/v1/apps/${appId}/events`;
1859
+ const SESSION_START_EVENT = '_session.start';
1860
+ const SESSION_STOP_EVENT = '_session.stop';
1861
+ const DEFAULT_IDLE_TIMEOUT_MS = 30 * 60 * 1000;
1862
+ const DEFAULT_DEBOUNCE_MS = 5000;
1721
1863
  /** Converts a string to a number, guarded against invalid values */
1722
1864
  function getNumber(value, fallback = 0) {
1723
1865
  const numeric = Number(value);
@@ -1759,53 +1901,89 @@
1759
1901
  metrics,
1760
1902
  };
1761
1903
  }
1762
- function buildBatchEventPayload({ eventName, payload, trackerName, appName, appVersion, }) {
1763
- var _a, _b, _c, _d;
1904
+ function buildBatchEventPayload({ queuedEvents, trackerName, appName, appVersion, }) {
1905
+ var _a, _b, _c, _d, _e, _f;
1906
+ const endpointEvent = queuedEvents[queuedEvents.length - 1] ||
1907
+ {
1908
+ payload: {},
1909
+ browserInfo: {},
1910
+ };
1911
+ const payload = endpointEvent.payload || {};
1912
+ const browserInfo = endpointEvent.browserInfo || {};
1764
1913
  const endpointId = getEndpointId();
1765
- const sessionId = `${Date.now()}-${Math.random().toString(16).slice(2)}`;
1766
- const timestamp = new Date().toISOString();
1767
- const { attributes, metrics } = splitEventData(payload);
1914
+ const location = {};
1915
+ if (payload === null || payload === void 0 ? void 0 : payload.city) {
1916
+ location.City = payload.city;
1917
+ }
1918
+ if (payload === null || payload === void 0 ? void 0 : payload.country) {
1919
+ location.Country = payload.country;
1920
+ }
1921
+ if ((payload === null || payload === void 0 ? void 0 : payload.lat) !== undefined && (payload === null || payload === void 0 ? void 0 : payload.lat) !== null) {
1922
+ location.Lat = getNumber(payload.lat);
1923
+ }
1924
+ if ((payload === null || payload === void 0 ? void 0 : payload.lon) !== undefined && (payload === null || payload === void 0 ? void 0 : payload.lon) !== null) {
1925
+ location.Lon = getNumber(payload.lon);
1926
+ }
1768
1927
  return {
1769
1928
  BatchEvent: {
1770
1929
  Endpoint: {
1771
1930
  Id: endpointId,
1931
+ Demographic: Object.assign({ Locale: (browserInfo === null || browserInfo === void 0 ? void 0 : browserInfo.language) || '', Make: (payload === null || payload === void 0 ? void 0 : payload.deviceMake) || (browserInfo === null || browserInfo === void 0 ? void 0 : browserInfo.make) || '', Model: (payload === null || payload === void 0 ? void 0 : payload.deviceModel) || (browserInfo === null || browserInfo === void 0 ? void 0 : browserInfo.model) || '', ModelVersion: (browserInfo === null || browserInfo === void 0 ? void 0 : browserInfo.version) || '', Platform: ((_a = browserInfo === null || browserInfo === void 0 ? void 0 : browserInfo.os) === null || _a === void 0 ? void 0 : _a.name) || (browserInfo === null || browserInfo === void 0 ? void 0 : browserInfo.platform) || '' }, (((_b = browserInfo === null || browserInfo === void 0 ? void 0 : browserInfo.os) === null || _b === void 0 ? void 0 : _b.version)
1932
+ ? { PlatformVersion: browserInfo.os.version }
1933
+ : {})),
1772
1934
  Device: {
1773
- Make: (payload === null || payload === void 0 ? void 0 : payload.deviceMake) || '',
1774
- Model: (payload === null || payload === void 0 ? void 0 : payload.deviceModel) || '',
1775
- },
1776
- Location: {
1777
- City: (payload === null || payload === void 0 ? void 0 : payload.city) || '',
1778
- Country: (payload === null || payload === void 0 ? void 0 : payload.country) || '',
1779
- Lat: getNumber(payload === null || payload === void 0 ? void 0 : payload.lat),
1780
- Lon: getNumber(payload === null || payload === void 0 ? void 0 : payload.lon),
1935
+ Make: (payload === null || payload === void 0 ? void 0 : payload.deviceMake) || (browserInfo === null || browserInfo === void 0 ? void 0 : browserInfo.make) || '',
1936
+ Model: (payload === null || payload === void 0 ? void 0 : payload.deviceModel) || (browserInfo === null || browserInfo === void 0 ? void 0 : browserInfo.model) || '',
1781
1937
  },
1938
+ Location: location,
1782
1939
  Application: {
1783
- AppTitle: (_b = (_a = payload === null || payload === void 0 ? void 0 : payload.appTitle) !== null && _a !== void 0 ? _a : appName) !== null && _b !== void 0 ? _b : trackerName,
1784
- AppVersion: (_c = payload === null || payload === void 0 ? void 0 : payload.appVersion) !== null && _c !== void 0 ? _c : appVersion,
1785
- ClientSdkVersion: (_d = payload === null || payload === void 0 ? void 0 : payload.clientSdkVersion) !== null && _d !== void 0 ? _d : trackerName,
1940
+ AppTitle: (_d = (_c = payload === null || payload === void 0 ? void 0 : payload.appTitle) !== null && _c !== void 0 ? _c : appName) !== null && _d !== void 0 ? _d : trackerName,
1941
+ AppVersion: (_e = payload === null || payload === void 0 ? void 0 : payload.appVersion) !== null && _e !== void 0 ? _e : appVersion,
1942
+ ClientSdkVersion: (_f = payload === null || payload === void 0 ? void 0 : payload.clientSdkVersion) !== null && _f !== void 0 ? _f : trackerName,
1786
1943
  },
1787
1944
  },
1788
- Events: [
1789
- {
1790
- EventType: eventName,
1945
+ Events: queuedEvents.map((queuedEvent) => {
1946
+ console.log('queued event', queuedEvent.eventName);
1947
+ const timestamp = queuedEvent.timestamp;
1948
+ const { attributes, metrics } = splitEventData(queuedEvent.payload);
1949
+ const sessionContext = queuedEvent.session ||
1950
+ {
1951
+ id: `${queuedEvent.unixMs}-${Math.random().toString(16).slice(2)}`,
1952
+ startTimestamp: timestamp,
1953
+ startUnixMs: queuedEvent.unixMs,
1954
+ };
1955
+ const isStopEvent = queuedEvent.eventName === SESSION_STOP_EVENT;
1956
+ return {
1957
+ EventType: queuedEvent.eventName,
1791
1958
  Timestamp: timestamp,
1792
1959
  Session: {
1793
- Id: sessionId,
1794
- StartTimestamp: timestamp,
1960
+ Id: sessionContext.id,
1961
+ StartTimestamp: sessionContext.startTimestamp,
1795
1962
  EndTimestamp: timestamp,
1796
- Duration: 0,
1963
+ Duration: isStopEvent
1964
+ ? Math.max(0, queuedEvent.unixMs - sessionContext.startUnixMs)
1965
+ : 0,
1797
1966
  },
1798
1967
  Attributes: attributes,
1799
1968
  Metrics: metrics,
1800
- },
1801
- ],
1969
+ };
1970
+ }),
1802
1971
  },
1803
1972
  };
1804
1973
  }
1805
- function createEsriPlugin({ trackerName, appName, appId, appVersion, userPoolID, fips, }) {
1974
+ function createEsriPlugin({ trackerName, appName, appId, appVersion, userPoolID, fips, debounceMs = DEFAULT_DEBOUNCE_MS, }) {
1806
1975
  const url = ESRI_ENDPOINT_URL(appId);
1807
1976
  let isDisabled = false;
1808
1977
  let hasWarned = false;
1978
+ let hasSessionStarted = false;
1979
+ let sessionStopSent = false;
1980
+ let activeSession;
1981
+ let idleTimerId;
1982
+ let debounceTimerId;
1983
+ let lifecycleListenersAttached = false;
1984
+ const removeLifecycleListeners = [];
1985
+ let pendingEvents = [];
1986
+ const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
1809
1987
  const warnOnce = (message, details) => {
1810
1988
  if (hasWarned) {
1811
1989
  return;
@@ -1835,64 +2013,263 @@
1835
2013
  trackerName,
1836
2014
  });
1837
2015
  }
1838
- return {
1839
- track: (eventName, payload) => {
1840
- if (isDisabled || !url || !userPoolID) {
1841
- debugLog('Skipping esri track call', {
1842
- eventName,
1843
- reason: 'plugin-disabled',
1844
- });
1845
- return false;
2016
+ const clearIdleTimer = () => {
2017
+ if (!idleTimerId) {
2018
+ return;
2019
+ }
2020
+ clearTimeout(idleTimerId);
2021
+ idleTimerId = undefined;
2022
+ };
2023
+ const clearDebounceTimer = () => {
2024
+ if (!debounceTimerId) {
2025
+ return;
2026
+ }
2027
+ clearTimeout(debounceTimerId);
2028
+ debounceTimerId = undefined;
2029
+ };
2030
+ const clearPendingEvents = () => {
2031
+ pendingEvents = [];
2032
+ };
2033
+ const enqueueEvent = (event) => {
2034
+ pendingEvents.push(event);
2035
+ };
2036
+ const dequeuePendingEvents = () => {
2037
+ if (pendingEvents.length === 0) {
2038
+ return [];
2039
+ }
2040
+ const queuedEvents = pendingEvents;
2041
+ pendingEvents = [];
2042
+ return queuedEvents;
2043
+ };
2044
+ const sendQueuedEvents = (queuedEvents) => {
2045
+ var _a;
2046
+ if (queuedEvents.length === 0) {
2047
+ return;
2048
+ }
2049
+ if (isDisabled || !url || !userPoolID) {
2050
+ debugLog('Skipping esri track call', {
2051
+ eventName: (_a = queuedEvents[queuedEvents.length - 1]) === null || _a === void 0 ? void 0 : _a.eventName,
2052
+ reason: 'plugin-disabled',
2053
+ });
2054
+ return;
2055
+ }
2056
+ debugLog('Sending esri endpoint batch event', {
2057
+ trackerName,
2058
+ url,
2059
+ eventCount: queuedEvents.length,
2060
+ });
2061
+ void getCredentials(userPoolID, { fips })
2062
+ .then((credentials) => {
2063
+ const accessKeyId = (credentials === null || credentials === void 0 ? void 0 : credentials.AccessKeyId) || (credentials === null || credentials === void 0 ? void 0 : credentials.accessKeyId);
2064
+ const secretAccessKey = (credentials === null || credentials === void 0 ? void 0 : credentials.SecretKey) || (credentials === null || credentials === void 0 ? void 0 : credentials.secretAccessKey);
2065
+ const sessionToken = (credentials === null || credentials === void 0 ? void 0 : credentials.SessionToken) || (credentials === null || credentials === void 0 ? void 0 : credentials.sessionToken);
2066
+ if (!accessKeyId || !secretAccessKey) {
2067
+ throw new Error('Signed endpoint credentials are missing required key fields.');
1846
2068
  }
1847
- debugLog('Sending esri endpoint event', {
1848
- eventName,
1849
- trackerName,
1850
- url,
2069
+ const client = new AwsClient({
2070
+ accessKeyId,
2071
+ secretAccessKey,
2072
+ sessionToken,
2073
+ service: ESRI_SIGNING_SERVICE,
2074
+ region: ESRI_SIGNING_REGION,
1851
2075
  });
1852
- void getCredentials(userPoolID, { fips })
1853
- .then((credentials) => {
1854
- const accessKeyId = (credentials === null || credentials === void 0 ? void 0 : credentials.AccessKeyId) || (credentials === null || credentials === void 0 ? void 0 : credentials.accessKeyId);
1855
- const secretAccessKey = (credentials === null || credentials === void 0 ? void 0 : credentials.SecretKey) || (credentials === null || credentials === void 0 ? void 0 : credentials.secretAccessKey);
1856
- const sessionToken = (credentials === null || credentials === void 0 ? void 0 : credentials.SessionToken) || (credentials === null || credentials === void 0 ? void 0 : credentials.sessionToken);
1857
- if (!accessKeyId || !secretAccessKey) {
1858
- throw new Error('Signed endpoint credentials are missing required key fields.');
1859
- }
1860
- const client = new AwsClient({
1861
- accessKeyId,
1862
- secretAccessKey,
1863
- sessionToken,
1864
- service: ESRI_SIGNING_SERVICE,
1865
- region: ESRI_SIGNING_REGION,
1866
- });
1867
- return client.fetch(url, {
1868
- method: 'POST',
1869
- headers: {
1870
- 'Content-Type': 'application/json',
1871
- },
1872
- body: JSON.stringify(buildBatchEventPayload({
1873
- eventName,
1874
- payload: payload,
1875
- trackerName,
1876
- appName,
1877
- appVersion,
1878
- })),
1879
- });
1880
- })
1881
- .then(() => {
1882
- debugLog('Endpoint event sent', {
1883
- eventName,
1884
- trackerName,
1885
- });
1886
- })
1887
- .catch((error) => {
1888
- isDisabled = true;
1889
- warnOnce('Esri telemetry disabled after endpoint track failure', {
1890
- eventName,
2076
+ return client.fetch(url, {
2077
+ method: 'POST',
2078
+ headers: {
2079
+ 'Content-Type': 'application/json',
2080
+ },
2081
+ body: JSON.stringify(buildBatchEventPayload({
2082
+ queuedEvents,
1891
2083
  trackerName,
1892
- message: (error === null || error === void 0 ? void 0 : error.message) || String(error),
1893
- });
2084
+ appName,
2085
+ appVersion,
2086
+ })),
2087
+ });
2088
+ })
2089
+ .then(() => {
2090
+ debugLog('Endpoint event sent', {
2091
+ trackerName,
2092
+ eventCount: queuedEvents.length,
2093
+ });
2094
+ })
2095
+ .catch((error) => {
2096
+ var _a;
2097
+ isDisabled = true;
2098
+ clearIdleTimer();
2099
+ clearDebounceTimer();
2100
+ clearPendingEvents();
2101
+ removeAllLifecycleListeners();
2102
+ warnOnce('Esri telemetry disabled after endpoint track failure', {
2103
+ eventName: (_a = queuedEvents[queuedEvents.length - 1]) === null || _a === void 0 ? void 0 : _a.eventName,
2104
+ trackerName,
2105
+ message: (error === null || error === void 0 ? void 0 : error.message) || String(error),
2106
+ });
2107
+ });
2108
+ };
2109
+ const flushPendingEvents = (eventToAppend) => {
2110
+ clearDebounceTimer();
2111
+ const queuedEvents = dequeuePendingEvents();
2112
+ if (eventToAppend) {
2113
+ queuedEvents.push(eventToAppend);
2114
+ }
2115
+ console.log('flushing', eventToAppend);
2116
+ console.log('stack', queuedEvents);
2117
+ sendQueuedEvents(queuedEvents);
2118
+ };
2119
+ const scheduleDebounceFlush = () => {
2120
+ clearDebounceTimer();
2121
+ debounceTimerId = setTimeout(() => {
2122
+ flushPendingEvents();
2123
+ }, debounceMs);
2124
+ };
2125
+ const removeAllLifecycleListeners = () => {
2126
+ removeLifecycleListeners.splice(0).forEach((removeListener) => {
2127
+ removeListener();
2128
+ });
2129
+ lifecycleListenersAttached = false;
2130
+ };
2131
+ const createSession = () => {
2132
+ const now = Date.now();
2133
+ return {
2134
+ id: `${now}-${Math.random().toString(16).slice(2)}`,
2135
+ startTimestamp: new Date(now).toISOString(),
2136
+ startUnixMs: now,
2137
+ };
2138
+ };
2139
+ const sendEvent = (eventName, payload, sessionOverride, options, forceImmediate = false) => {
2140
+ if (isDisabled || !url || !userPoolID) {
2141
+ debugLog('Skipping esri track call', {
2142
+ eventName,
2143
+ reason: 'plugin-disabled',
1894
2144
  });
1895
- return true;
2145
+ return false;
2146
+ }
2147
+ debugLog('Queueing esri endpoint event', {
2148
+ eventName,
2149
+ trackerName,
2150
+ immediate: Boolean((options === null || options === void 0 ? void 0 : options.immediate) || forceImmediate),
2151
+ });
2152
+ const queuedEvent = {
2153
+ eventName,
2154
+ payload: payload,
2155
+ session: sessionOverride || activeSession,
2156
+ browserInfo: getBrowserClientInfo(),
2157
+ timestamp: new Date().toISOString(),
2158
+ unixMs: Date.now(),
2159
+ };
2160
+ if ((options === null || options === void 0 ? void 0 : options.immediate) || forceImmediate) {
2161
+ flushPendingEvents(queuedEvent);
2162
+ }
2163
+ else {
2164
+ enqueueEvent(queuedEvent);
2165
+ scheduleDebounceFlush();
2166
+ }
2167
+ return true;
2168
+ };
2169
+ const sendSessionLifecycleEvent = (eventName, reason, sessionOverride) => {
2170
+ return sendEvent(eventName, {
2171
+ reason,
2172
+ }, sessionOverride, undefined, true);
2173
+ };
2174
+ const stopSession = (reason) => {
2175
+ console.log('stopping session');
2176
+ if (!hasSessionStarted || sessionStopSent) {
2177
+ return false;
2178
+ }
2179
+ const sessionToStop = activeSession;
2180
+ const didStop = sendSessionLifecycleEvent(SESSION_STOP_EVENT, reason, sessionToStop);
2181
+ if (didStop) {
2182
+ sessionStopSent = true;
2183
+ hasSessionStarted = false;
2184
+ clearIdleTimer();
2185
+ activeSession = undefined;
2186
+ }
2187
+ return didStop;
2188
+ };
2189
+ const startSession = (reason) => {
2190
+ if (hasSessionStarted) {
2191
+ return false;
2192
+ }
2193
+ activeSession = activeSession || createSession();
2194
+ const sessionToStart = activeSession;
2195
+ const didStart = sendSessionLifecycleEvent(SESSION_START_EVENT, reason, sessionToStart);
2196
+ if (didStart) {
2197
+ hasSessionStarted = true;
2198
+ sessionStopSent = false;
2199
+ }
2200
+ else {
2201
+ activeSession = undefined;
2202
+ }
2203
+ return didStart;
2204
+ };
2205
+ const resetIdleTimer = () => {
2206
+ if (!isBrowser || isDisabled || !hasSessionStarted) {
2207
+ return;
2208
+ }
2209
+ clearIdleTimer();
2210
+ idleTimerId = setTimeout(() => {
2211
+ stopSession('idle');
2212
+ }, DEFAULT_IDLE_TIMEOUT_MS);
2213
+ };
2214
+ const handleActivity = () => {
2215
+ if (isDisabled) {
2216
+ return;
2217
+ }
2218
+ if (!hasSessionStarted) {
2219
+ startSession('wakeup');
2220
+ }
2221
+ resetIdleTimer();
2222
+ };
2223
+ const addLifecycleListener = (target, eventName, listener) => {
2224
+ target.addEventListener(eventName, listener);
2225
+ removeLifecycleListeners.push(() => {
2226
+ target.removeEventListener(eventName, listener);
2227
+ });
2228
+ };
2229
+ const attachLifecycleListeners = () => {
2230
+ if (!isBrowser || lifecycleListenersAttached || isDisabled) {
2231
+ return;
2232
+ }
2233
+ const activityEvents = ['pointerdown', 'keydown', 'scroll', 'focus'];
2234
+ activityEvents.forEach((eventName) => {
2235
+ addLifecycleListener(window, eventName, handleActivity);
2236
+ });
2237
+ addLifecycleListener(window, 'beforeunload', () => {
2238
+ stopSession('beforeunload');
2239
+ });
2240
+ lifecycleListenersAttached = true;
2241
+ };
2242
+ return {
2243
+ track: (eventName, payload, options) => {
2244
+ attachLifecycleListeners();
2245
+ const isSessionStopEvent = eventName === SESSION_STOP_EVENT;
2246
+ if (!hasSessionStarted &&
2247
+ eventName !== SESSION_START_EVENT &&
2248
+ !isSessionStopEvent) {
2249
+ startSession('init');
2250
+ }
2251
+ if (eventName === SESSION_START_EVENT) {
2252
+ activeSession = activeSession || createSession();
2253
+ hasSessionStarted = true;
2254
+ sessionStopSent = false;
2255
+ }
2256
+ if (isSessionStopEvent) {
2257
+ sessionStopSent = true;
2258
+ hasSessionStarted = false;
2259
+ clearIdleTimer();
2260
+ }
2261
+ if (hasSessionStarted && !activeSession && !isSessionStopEvent) {
2262
+ activeSession = createSession();
2263
+ }
2264
+ if (hasSessionStarted && !isSessionStopEvent) {
2265
+ resetIdleTimer();
2266
+ }
2267
+ const sessionForEvent = activeSession;
2268
+ const didSend = sendEvent(eventName, payload, sessionForEvent, options, eventName === SESSION_START_EVENT || eventName === SESSION_STOP_EVENT);
2269
+ if (isSessionStopEvent && didSend) {
2270
+ activeSession = undefined;
2271
+ }
2272
+ return didSend;
1896
2273
  },
1897
2274
  };
1898
2275
  }
@@ -1966,14 +2343,14 @@
1966
2343
  const result = this.analytics.track('pageView', telemetryPayload);
1967
2344
  return typeof result === 'boolean' ? result : true;
1968
2345
  }
1969
- logEvent(event = {}) {
2346
+ logEvent(event = {}, trackOptions) {
1970
2347
  const telemetryPayload = createEventLog({
1971
2348
  event,
1972
2349
  dimensionLookup: this.dimensions,
1973
2350
  metricLookup: this.metrics,
1974
2351
  });
1975
2352
  const { name } = telemetryPayload;
1976
- const result = this.analytics.track(name, telemetryPayload);
2353
+ const result = this.analytics.track(name, telemetryPayload, trackOptions);
1977
2354
  return typeof result === 'boolean' ? result : true;
1978
2355
  }
1979
2356
  }