zar 0.3.0 → 0.4.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/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,30 @@ 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 context = drainPoolDataLayer();
1816
+ if (!context) {
1817
+ return result;
1818
+ }
1819
+ updateTrackingNumberContext({
1820
+ apiUrl,
1821
+ poolId: window.zarPoolData.pool_id,
1822
+ number: window.zarPoolData.number,
1823
+ context
1824
+ });
1825
+ return result;
1826
+ };
1827
+ window.zarPoolDLObserverDone = true;
1828
+ }
1749
1829
  function initTrackingPool() {
1750
1830
  return __async(this, arguments, function* ({ poolData, poolConfig, apiUrl } = {}) {
1751
1831
  var msg;
@@ -1757,9 +1837,9 @@ function initTrackingPool() {
1757
1837
  }
1758
1838
  return poolIntervals;
1759
1839
  }
1760
- var context = {};
1840
+ var context = drainPoolDataLayer() || {};
1761
1841
  if (poolConfig.contextCallback) {
1762
- context = poolConfig.contextCallback() || {};
1842
+ context = poolConfig.contextCallback(context) || {};
1763
1843
  }
1764
1844
  var poolId = getPoolId(poolConfig.poolId);
1765
1845
  if (!poolId) {
@@ -1783,7 +1863,12 @@ function initTrackingPool() {
1783
1863
  }
1784
1864
  }
1785
1865
  window.zarPoolData = poolData;
1786
- if (poolData && poolData.status === NUMBER_POOL_SUCCESS && poolData.number) {
1866
+ if (poolActive()) {
1867
+ try {
1868
+ initPoolDataLayerObserver(apiUrl);
1869
+ } catch (e2) {
1870
+ warn("data layer observer error: " + JSON.stringify(e2));
1871
+ }
1787
1872
  afterDOMContentLoaded(function() {
1788
1873
  var overlayElements = document.querySelectorAll(poolConfig.overlayQuerySelector);
1789
1874
  if (!overlayElements) {
@@ -1843,8 +1928,9 @@ function zar({ apiUrl, poolConfig }) {
1843
1928
  var pcfg = config.poolConfig;
1844
1929
  if (pcfg && pcfg.poolId) {
1845
1930
  payload.properties.pool_id = getPoolId(pcfg.poolId);
1931
+ var context = drainPoolDataLayer() || {};
1846
1932
  if (pcfg.contextCallback) {
1847
- payload.properties.pool_context = pcfg.contextCallback() || {};
1933
+ payload.properties.pool_context = pcfg.contextCallback(context) || {};
1848
1934
  }
1849
1935
  }
1850
1936
  } catch (e2) {
@@ -1872,7 +1958,11 @@ function zar({ apiUrl, poolConfig }) {
1872
1958
  }
1873
1959
  }
1874
1960
  if (res && res.pool_data) {
1875
- initTrackingPool({ poolData: res.pool_data, poolConfig: config.poolConfig, apiUrl: config.apiUrl });
1961
+ initTrackingPool({
1962
+ poolData: res.pool_data,
1963
+ poolConfig: config.poolConfig,
1964
+ apiUrl: config.apiUrl
1965
+ });
1876
1966
  }
1877
1967
  });
1878
1968
  },
@@ -1919,6 +2009,16 @@ function zar({ apiUrl, poolConfig }) {
1919
2009
  apiUrl: plugin.apiUrl()
1920
2010
  });
1921
2011
  },
2012
+ updateTrackingNumberContext({ number, context }) {
2013
+ var plugin = this.instance.plugins.zar;
2014
+ var poolConfig2 = plugin.poolConfig();
2015
+ return updateTrackingNumberContext({
2016
+ apiUrl: this.instance.plugins.zar.apiUrl(),
2017
+ poolId: getPoolId(poolConfig2.poolId),
2018
+ number,
2019
+ context
2020
+ });
2021
+ },
1922
2022
  getPoolIntervals() {
1923
2023
  return poolIntervals;
1924
2024
  },
@@ -1943,12 +2043,22 @@ function zar({ apiUrl, poolConfig }) {
1943
2043
  revertOverlayNumbers({ elems: overlayElements });
1944
2044
  },
1945
2045
  getPoolStats({ key = null, with_contexts = false }) {
1946
- return getPoolStats({ apiUrl: this.instance.plugins.zar.apiUrl(), key, with_contexts });
2046
+ return getPoolStats({
2047
+ apiUrl: this.instance.plugins.zar.apiUrl(),
2048
+ key,
2049
+ with_contexts
2050
+ });
1947
2051
  }
1948
2052
  }
1949
2053
  };
1950
2054
  }
1951
- function init({ app, ga4Config = null, facebookConfig = null, apiUrl = null, poolConfig = null }) {
2055
+ function init({
2056
+ app,
2057
+ ga4Config = null,
2058
+ facebookConfig = null,
2059
+ apiUrl = null,
2060
+ poolConfig = null
2061
+ }) {
1952
2062
  if (!apiUrl) {
1953
2063
  apiUrl = getDefaultApiUrl();
1954
2064
  }