zar 0.3.0 → 0.4.1

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/zar.es.js CHANGED
@@ -1527,7 +1527,9 @@ var NUMBER_POOL_RENEWAL_TIME_MS = 30 * 1e3;
1527
1527
  var getNumberFailureCount = 0;
1528
1528
  var MAX_GET_NUMBER_FAILURES = 3;
1529
1529
  var poolIntervals = {};
1530
- window.zarPoolData = null;
1530
+ window.zarPoolData = window.zarPoolData || null;
1531
+ window.zarPoolDLObserverDone = window.zarPoolDLObserverDone || false;
1532
+ window.zarPoolDataLayer = window.zarPoolDataLayer || [];
1531
1533
  var numberOverlayMap = /* @__PURE__ */ new Map();
1532
1534
  function generateVisitId() {
1533
1535
  return Date.now().toString(36) + "." + Math.random().toString(36).substring(2);
@@ -1555,7 +1557,7 @@ function initIDs() {
1555
1557
  return { vid: vidResult };
1556
1558
  }
1557
1559
  function getDefaultApiUrl() {
1558
- return window.location.host + "/api/v1";
1560
+ return window.location.protocol + "://" + window.location.host + "/api/v2";
1559
1561
  }
1560
1562
  function getIDObj(key) {
1561
1563
  return getSessionStorage(key);
@@ -1609,7 +1611,7 @@ function overlayPhoneNumber({ elems, number, force = false }) {
1609
1611
  var elemNum = extractPhoneNumber({ elem: elems[i2] });
1610
1612
  if (!origNum) {
1611
1613
  origNum = elemNum;
1612
- } else if (!force && (elemNum.number && origNum.number) && origNum.number !== elemNum.number) {
1614
+ } else if (!force && elemNum.number && origNum.number && origNum.number !== elemNum.number) {
1613
1615
  warn("overlaying multiple numbers with a single number", origNum.number, elemNum.number);
1614
1616
  }
1615
1617
  dbg("overlaying", overlayNum, "on", elems[i2]);
@@ -1669,7 +1671,12 @@ function clearPoolIntervals() {
1669
1671
  }
1670
1672
  }
1671
1673
  function getPoolNumber(_0) {
1672
- return __async(this, arguments, function* ({ poolId, apiUrl, number = null, context = null }) {
1674
+ return __async(this, arguments, function* ({
1675
+ poolId,
1676
+ apiUrl,
1677
+ number = null,
1678
+ context = null
1679
+ }) {
1673
1680
  var payload = {
1674
1681
  pool_id: poolId,
1675
1682
  number,
@@ -1682,13 +1689,58 @@ function getPoolNumber(_0) {
1682
1689
  return resp;
1683
1690
  });
1684
1691
  }
1692
+ function poolActive() {
1693
+ return window.zarPoolData && window.zarPoolData.status === NUMBER_POOL_SUCCESS && window.zarPoolData.number;
1694
+ }
1695
+ function drainPoolDataLayer() {
1696
+ if (!window.zarPoolDataLayer || !Array.isArray(window.zarPoolDataLayer)) {
1697
+ return {};
1698
+ }
1699
+ var mergedObject = {};
1700
+ for (var i2 = 0; i2 < window.zarPoolDataLayer.length; i2++) {
1701
+ var obj = window.zarPoolDataLayer[i2];
1702
+ for (var key in obj) {
1703
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
1704
+ mergedObject[key] = obj[key];
1705
+ }
1706
+ }
1707
+ }
1708
+ window.zarPoolDataLayer.length = 0;
1709
+ return mergedObject;
1710
+ }
1711
+ function updateTrackingNumberContext(_0) {
1712
+ return __async(this, arguments, function* ({
1713
+ apiUrl,
1714
+ poolId,
1715
+ number,
1716
+ context
1717
+ }) {
1718
+ if (!poolActive()) {
1719
+ warn("no valid pool data, not updating context");
1720
+ return;
1721
+ }
1722
+ var payload = {
1723
+ pool_id: poolId,
1724
+ number,
1725
+ context,
1726
+ properties: {
1727
+ zar: getStorage()
1728
+ }
1729
+ };
1730
+ var resp = yield httpPost({ url: `${apiUrl}/update_number`, data: payload });
1731
+ return resp;
1732
+ });
1733
+ }
1685
1734
  function getPoolStats(_0) {
1686
1735
  return __async(this, arguments, function* ({ apiUrl, key = null, with_contexts = false }) {
1687
1736
  var params = {
1688
1737
  key,
1689
1738
  with_contexts
1690
1739
  };
1691
- var resp = yield httpGet({ url: `${apiUrl}/number_pool_stats`, params });
1740
+ var resp = yield httpGet({
1741
+ url: `${apiUrl}/number_pool_stats`,
1742
+ params
1743
+ });
1692
1744
  return resp;
1693
1745
  });
1694
1746
  }
@@ -1698,9 +1750,9 @@ function renewTrackingPool() {
1698
1750
  apiUrl = getDefaultApiUrl(),
1699
1751
  contextCallback = null
1700
1752
  } = {}) {
1701
- var context = {};
1753
+ var context = drainPoolDataLayer() || {};
1702
1754
  if (contextCallback) {
1703
- context = contextCallback() || {};
1755
+ context = contextCallback(context) || {};
1704
1756
  }
1705
1757
  const poolId = window.zarPoolData.pool_id;
1706
1758
  const number = window.zarPoolData.number;
@@ -1727,7 +1779,11 @@ function renewTrackingPool() {
1727
1779
  force = true;
1728
1780
  }
1729
1781
  if (overlayElements) {
1730
- overlayPhoneNumber({ elems: overlayElements, number: resp.number, force });
1782
+ overlayPhoneNumber({
1783
+ elems: overlayElements,
1784
+ number: resp.number,
1785
+ force
1786
+ });
1731
1787
  }
1732
1788
  } else {
1733
1789
  if (overlayElements) {
@@ -1746,6 +1802,39 @@ function getPoolId(poolId) {
1746
1802
  return poolId();
1747
1803
  return poolId;
1748
1804
  }
1805
+ function initPoolDataLayerObserver(apiUrl) {
1806
+ if (window.zarPoolDLObserverDone) {
1807
+ return;
1808
+ }
1809
+ if (!poolActive()) {
1810
+ return;
1811
+ }
1812
+ var originalPush = window.zarPoolDataLayer.push;
1813
+ window.zarPoolDataLayer.push = function(...args) {
1814
+ var result = originalPush.apply(this, args);
1815
+ var context2 = drainPoolDataLayer();
1816
+ if (!context2) {
1817
+ return result;
1818
+ }
1819
+ updateTrackingNumberContext({
1820
+ apiUrl,
1821
+ poolId: window.zarPoolData.pool_id,
1822
+ number: window.zarPoolData.number,
1823
+ context: context2
1824
+ });
1825
+ return result;
1826
+ };
1827
+ var context = drainPoolDataLayer();
1828
+ if (context) {
1829
+ updateTrackingNumberContext({
1830
+ apiUrl,
1831
+ poolId: window.zarPoolData.pool_id,
1832
+ number: window.zarPoolData.number,
1833
+ context
1834
+ });
1835
+ }
1836
+ window.zarPoolDLObserverDone = true;
1837
+ }
1749
1838
  function initTrackingPool() {
1750
1839
  return __async(this, arguments, function* ({ poolData, poolConfig, apiUrl } = {}) {
1751
1840
  var msg;
@@ -1757,9 +1846,9 @@ function initTrackingPool() {
1757
1846
  }
1758
1847
  return poolIntervals;
1759
1848
  }
1760
- var context = {};
1849
+ var context = drainPoolDataLayer() || {};
1761
1850
  if (poolConfig.contextCallback) {
1762
- context = poolConfig.contextCallback() || {};
1851
+ context = poolConfig.contextCallback(context) || {};
1763
1852
  }
1764
1853
  var poolId = getPoolId(poolConfig.poolId);
1765
1854
  if (!poolId) {
@@ -1783,7 +1872,12 @@ function initTrackingPool() {
1783
1872
  }
1784
1873
  }
1785
1874
  window.zarPoolData = poolData;
1786
- if (poolData && poolData.status === NUMBER_POOL_SUCCESS && poolData.number) {
1875
+ if (poolActive()) {
1876
+ try {
1877
+ initPoolDataLayerObserver(apiUrl);
1878
+ } catch (e2) {
1879
+ warn("data layer observer error: " + JSON.stringify(e2));
1880
+ }
1787
1881
  afterDOMContentLoaded(function() {
1788
1882
  var overlayElements = document.querySelectorAll(poolConfig.overlayQuerySelector);
1789
1883
  if (!overlayElements) {
@@ -1843,8 +1937,9 @@ function zar({ apiUrl, poolConfig }) {
1843
1937
  var pcfg = config.poolConfig;
1844
1938
  if (pcfg && pcfg.poolId) {
1845
1939
  payload.properties.pool_id = getPoolId(pcfg.poolId);
1940
+ var context = drainPoolDataLayer() || {};
1846
1941
  if (pcfg.contextCallback) {
1847
- payload.properties.pool_context = pcfg.contextCallback() || {};
1942
+ payload.properties.pool_context = pcfg.contextCallback(context) || {};
1848
1943
  }
1849
1944
  }
1850
1945
  } catch (e2) {
@@ -1872,7 +1967,11 @@ function zar({ apiUrl, poolConfig }) {
1872
1967
  }
1873
1968
  }
1874
1969
  if (res && res.pool_data) {
1875
- initTrackingPool({ poolData: res.pool_data, poolConfig: config.poolConfig, apiUrl: config.apiUrl });
1970
+ initTrackingPool({
1971
+ poolData: res.pool_data,
1972
+ poolConfig: config.poolConfig,
1973
+ apiUrl: config.apiUrl
1974
+ });
1876
1975
  }
1877
1976
  });
1878
1977
  },
@@ -1919,6 +2018,16 @@ function zar({ apiUrl, poolConfig }) {
1919
2018
  apiUrl: plugin.apiUrl()
1920
2019
  });
1921
2020
  },
2021
+ updateTrackingNumberContext({ number, context }) {
2022
+ var plugin = this.instance.plugins.zar;
2023
+ var poolConfig2 = plugin.poolConfig();
2024
+ return updateTrackingNumberContext({
2025
+ apiUrl: this.instance.plugins.zar.apiUrl(),
2026
+ poolId: getPoolId(poolConfig2.poolId),
2027
+ number,
2028
+ context
2029
+ });
2030
+ },
1922
2031
  getPoolIntervals() {
1923
2032
  return poolIntervals;
1924
2033
  },
@@ -1943,12 +2052,22 @@ function zar({ apiUrl, poolConfig }) {
1943
2052
  revertOverlayNumbers({ elems: overlayElements });
1944
2053
  },
1945
2054
  getPoolStats({ key = null, with_contexts = false }) {
1946
- return getPoolStats({ apiUrl: this.instance.plugins.zar.apiUrl(), key, with_contexts });
2055
+ return getPoolStats({
2056
+ apiUrl: this.instance.plugins.zar.apiUrl(),
2057
+ key,
2058
+ with_contexts
2059
+ });
1947
2060
  }
1948
2061
  }
1949
2062
  };
1950
2063
  }
1951
- function init({ app, ga4Config = null, facebookConfig = null, apiUrl = null, poolConfig = null }) {
2064
+ function init({
2065
+ app,
2066
+ ga4Config = null,
2067
+ facebookConfig = null,
2068
+ apiUrl = null,
2069
+ poolConfig = null
2070
+ }) {
1952
2071
  if (!apiUrl) {
1953
2072
  apiUrl = getDefaultApiUrl();
1954
2073
  }