@reservamos/browser-analytics 1.1.1 → 1.1.2

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.
@@ -13395,7 +13395,7 @@ function loadNoop(_src, onload) {
13395
13395
  onload();
13396
13396
  }
13397
13397
  var mixpanel = init_as_module(loadNoop);
13398
- const version = "1.1.1";
13398
+ const version = "1.1.2";
13399
13399
  const MIXPANEL_DISTINCT_ID_CACHE_KEY = "mp_distinct_id";
13400
13400
  const EVENT_SOURCE_SUPER_PROPERTY = {
13401
13401
  "Event Source": "sdk"
@@ -13840,7 +13840,7 @@ async function init$1(config) {
13840
13840
  } catch (error) {
13841
13841
  console.error("Error initializing identification service:", error);
13842
13842
  }
13843
- geolocationService.init();
13843
+ await geolocationService.init();
13844
13844
  configService.setEnvironment(isSandbox ? "sandbox" : "prod");
13845
13845
  f.setConfig(configService.getCoreAPIConfig());
13846
13846
  onLoaded();
@@ -13849,10 +13849,11 @@ function isTrackerReady() {
13849
13849
  return mixpanelService.isReady();
13850
13850
  }
13851
13851
  let currentCoordinates = null;
13852
+ let currentSource = null;
13852
13853
  const GEOLOCATION_PERMISSION_KEY = "geolocationPermissionTimestamp";
13853
13854
  const GEOLOCATION_CACHE_KEY = "geolocationCache";
13854
13855
  const GEOLOCATION_TRACK_EVENT = "Geolocation Requested";
13855
- const SEVEN_DAYS_IN_MS = 7 * 24 * 60 * 60 * 1e3;
13856
+ const ONE_HOUR_IN_MS = 60 * 60 * 1e3;
13856
13857
  const GEOLOCATION_REQUEST_TIMEOUT_MS = 1e4;
13857
13858
  const GEOLOCATION_OPTIONS = {
13858
13859
  enableHighAccuracy: true,
@@ -13872,6 +13873,12 @@ function safeSetItem(key, value) {
13872
13873
  } catch {
13873
13874
  }
13874
13875
  }
13876
+ function safeRemoveItem(key) {
13877
+ try {
13878
+ localStorage.removeItem(key);
13879
+ } catch {
13880
+ }
13881
+ }
13875
13882
  function toISO(timestamp) {
13876
13883
  return new Date(timestamp).toISOString();
13877
13884
  }
@@ -13909,16 +13916,25 @@ function restoreCoordinatesFromStorage() {
13909
13916
  const cache = loadCoordinatesFromStorage();
13910
13917
  if (cache) {
13911
13918
  currentCoordinates = { lat: cache.lat, long: cache.long };
13919
+ currentSource = "browser";
13912
13920
  }
13913
13921
  }
13914
13922
  function isPermissionTimestampValid(lastPermissionTimestamp) {
13915
13923
  if (!lastPermissionTimestamp) return false;
13916
13924
  const now = Date.now();
13917
- return now - parseInt(lastPermissionTimestamp, 10) < SEVEN_DAYS_IN_MS;
13925
+ return now - parseInt(lastPermissionTimestamp, 10) < ONE_HOUR_IN_MS;
13918
13926
  }
13919
13927
  function updatePermissionTimestamp(timestamp) {
13920
13928
  safeSetItem(GEOLOCATION_PERMISSION_KEY, timestamp.toString());
13921
13929
  }
13930
+ function repairLegacyGeolocationStorage() {
13931
+ if (!safeGetItem(GEOLOCATION_PERMISSION_KEY)) {
13932
+ return;
13933
+ }
13934
+ if (!loadCoordinatesFromStorage()) {
13935
+ safeRemoveItem(GEOLOCATION_PERMISSION_KEY);
13936
+ }
13937
+ }
13922
13938
  function getGeolocationStatus(error) {
13923
13939
  return error.code === error.PERMISSION_DENIED ? "denied" : "error";
13924
13940
  }
@@ -13933,10 +13949,12 @@ function buildGeolocationEventProperties(data) {
13933
13949
  if (data.errorCode !== void 0) {
13934
13950
  properties["Geolocation Error Code"] = data.errorCode;
13935
13951
  }
13936
- const coordinates = getCoordinates();
13937
- if (coordinates) {
13938
- properties.$latitude = coordinates.lat;
13939
- properties.$longitude = coordinates.long;
13952
+ if ((data.status === "granted" || data.status === "skipped_cache") && data.lat !== void 0 && data.long !== void 0) {
13953
+ properties.latitude = data.lat;
13954
+ properties.longitude = data.long;
13955
+ }
13956
+ if (data.source !== void 0) {
13957
+ properties.geolocation_source = data.source;
13940
13958
  }
13941
13959
  return properties;
13942
13960
  }
@@ -13945,10 +13963,12 @@ function buildGeolocationProfileProperties(data) {
13945
13963
  "Geolocation Updated At": data.updatedAt,
13946
13964
  "Geolocation Status": data.status
13947
13965
  };
13948
- const coordinates = getCoordinates();
13949
- if (coordinates) {
13950
- properties.$latitude = coordinates.lat;
13951
- properties.$longitude = coordinates.long;
13966
+ if ((data.status === "granted" || data.status === "skipped_cache") && data.lat !== void 0 && data.long !== void 0) {
13967
+ properties.latitude = data.lat;
13968
+ properties.longitude = data.long;
13969
+ }
13970
+ if (data.source !== void 0) {
13971
+ properties.geolocation_source = data.source;
13952
13972
  }
13953
13973
  return properties;
13954
13974
  }
@@ -13962,20 +13982,25 @@ function syncGeolocationToMixpanel(data) {
13962
13982
  mixpanelService.setPeopleProperties(profileProperties);
13963
13983
  }
13964
13984
  function init() {
13985
+ repairLegacyGeolocationStorage();
13965
13986
  restoreCoordinatesFromStorage();
13966
13987
  return new Promise((resolve) => {
13967
13988
  var _a;
13968
13989
  const lastPermissionTimestamp = safeGetItem(GEOLOCATION_PERMISSION_KEY);
13969
13990
  if (isPermissionTimestampValid(lastPermissionTimestamp)) {
13970
13991
  const cache = loadCoordinatesFromStorage();
13971
- const updatedAt2 = (cache == null ? void 0 : cache.updatedAt) ?? (lastPermissionTimestamp ? toISO(parseInt(lastPermissionTimestamp, 10)) : (/* @__PURE__ */ new Date()).toISOString());
13972
- syncGeolocationToMixpanel({
13973
- status: "skipped_cache",
13974
- updatedAt: updatedAt2,
13975
- accuracy: cache == null ? void 0 : cache.accuracy
13976
- });
13977
- resolve(currentCoordinates);
13978
- return;
13992
+ if (cache) {
13993
+ syncGeolocationToMixpanel({
13994
+ status: "skipped_cache",
13995
+ updatedAt: cache.updatedAt,
13996
+ lat: cache.lat,
13997
+ long: cache.long,
13998
+ accuracy: cache.accuracy,
13999
+ source: "browser"
14000
+ });
14001
+ resolve(currentCoordinates);
14002
+ return;
14003
+ }
13979
14004
  }
13980
14005
  const now = Date.now();
13981
14006
  const updatedAt = toISO(now);
@@ -13988,18 +14013,21 @@ function init() {
13988
14013
  (geolocation) => {
13989
14014
  const { latitude: lat, longitude: long, accuracy } = geolocation.coords;
13990
14015
  currentCoordinates = { lat, long };
14016
+ currentSource = "browser";
13991
14017
  saveCoordinatesToStorage(currentCoordinates, updatedAt, accuracy);
13992
14018
  updatePermissionTimestamp(now);
13993
14019
  syncGeolocationToMixpanel({
13994
14020
  status: "granted",
13995
14021
  updatedAt,
13996
- accuracy
14022
+ lat,
14023
+ long,
14024
+ accuracy,
14025
+ source: "browser"
13997
14026
  });
13998
14027
  resolve(currentCoordinates);
13999
14028
  },
14000
14029
  (error) => {
14001
14030
  const status = getGeolocationStatus(error);
14002
- updatePermissionTimestamp(now);
14003
14031
  syncGeolocationToMixpanel({
14004
14032
  status,
14005
14033
  updatedAt,
@@ -14014,9 +14042,13 @@ function init() {
14014
14042
  function getCoordinates() {
14015
14043
  return currentCoordinates;
14016
14044
  }
14045
+ function getGeolocationSource() {
14046
+ return currentSource;
14047
+ }
14017
14048
  const geolocationService = {
14018
14049
  init,
14019
- getCoordinates
14050
+ getCoordinates,
14051
+ getGeolocationSource
14020
14052
  };
14021
14053
  const FP_TRIGGER_EVENTS = ["Search", "View Results"];
14022
14054
  const EXTRA_UTM_FIELDS = {
@@ -14087,9 +14119,13 @@ async function trackEvent(eventName, eventProperties, meta = {}) {
14087
14119
  const fingerprint = await fingerprintService.getFingerprint(cacheOnly);
14088
14120
  const defaultCoordinates = {};
14089
14121
  const coordinates = geolocationService.getCoordinates();
14122
+ const geolocationSource = geolocationService.getGeolocationSource();
14090
14123
  if (coordinates) {
14091
- defaultCoordinates.$latitude = coordinates.lat;
14092
- defaultCoordinates.$longitude = coordinates.long;
14124
+ defaultCoordinates.latitude = coordinates.lat;
14125
+ defaultCoordinates.longitude = coordinates.long;
14126
+ }
14127
+ if (geolocationSource) {
14128
+ defaultCoordinates.geolocation_source = geolocationSource;
14093
14129
  }
14094
14130
  const defaultProperties = {
14095
14131
  "User Fingerprint": fingerprint