@usermaven/nextjs 1.2.7 → 1.3.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.
Files changed (2) hide show
  1. package/lib/index.es.js +70 -9
  2. package/package.json +1 -1
package/lib/index.es.js CHANGED
@@ -1399,8 +1399,8 @@ _safewrap_instance_methods(autocapture);
1399
1399
 
1400
1400
  var VERSION_INFO = {
1401
1401
  env: 'production',
1402
- date: '2023-12-21T09:27:52.618Z',
1403
- version: '1.2.7'
1402
+ date: '2024-01-31T10:00:47.158Z',
1403
+ version: '1.3.0'
1404
1404
  };
1405
1405
  var USERMAVEN_VERSION = VERSION_INFO.version + "/" + VERSION_INFO.env + "@" + VERSION_INFO.date;
1406
1406
  var MAX_AGE_TEN_YEARS = 31622400 * 10;
@@ -1507,8 +1507,34 @@ var browserEnv = {
1507
1507
  doc_encoding: document.characterSet,
1508
1508
  }); },
1509
1509
  getAnonymousId: function (_a) {
1510
- var name = _a.name, domain = _a.domain;
1510
+ var name = _a.name, domain = _a.domain, _b = _a.crossDomainLinking, crossDomainLinking = _b === void 0 ? true : _b;
1511
1511
  expireNonRootCookies(name);
1512
+ // Check if cross domain linking is enabled
1513
+ if (crossDomainLinking) {
1514
+ // Try to extract the '_um' parameter from query string and hash fragment (https://example.com#_um=1~abcde5~)
1515
+ var urlParams = new URLSearchParams(window.location.search);
1516
+ var queryId = urlParams.get('_um');
1517
+ var urlHash = window.location.hash.substring(1);
1518
+ var hashedValues = urlHash.split("~");
1519
+ var fragmentId = hashedValues.length > 1 ? hashedValues[1] : undefined;
1520
+ // If the '_um' parameter is set in both the query string and hash fragment,
1521
+ // prioritize the one in query string
1522
+ var crossDomainAnonymousId = queryId || fragmentId;
1523
+ // If coming from another domain, use the ID from URL parameter
1524
+ if (crossDomainAnonymousId) {
1525
+ getLogger().debug("Existing user id from other domain", crossDomainAnonymousId);
1526
+ // Check if the ID needs to be set as cookie
1527
+ var currentCookie = getCookie(name);
1528
+ if (!currentCookie || currentCookie !== crossDomainAnonymousId) {
1529
+ setCookie(name, crossDomainAnonymousId, {
1530
+ domain: domain,
1531
+ secure: document.location.protocol !== "http:",
1532
+ maxAge: MAX_AGE_TEN_YEARS,
1533
+ });
1534
+ }
1535
+ return crossDomainAnonymousId;
1536
+ }
1537
+ }
1512
1538
  var idCookie = getCookie(name);
1513
1539
  if (idCookie) {
1514
1540
  getLogger().debug("Existing user id", idCookie);
@@ -1728,6 +1754,8 @@ var UsermavenClientImpl = /** @class */ (function () {
1728
1754
  this.idCookieName = "";
1729
1755
  this.randomizeUrl = false;
1730
1756
  this.namespace = "usermaven";
1757
+ this.crossDomainLinking = true;
1758
+ this.domains = [];
1731
1759
  this.apiKey = "";
1732
1760
  this.initialized = false;
1733
1761
  this._3pCookies = {};
@@ -1991,6 +2019,7 @@ var UsermavenClientImpl = /** @class */ (function () {
1991
2019
  ? env.getAnonymousId({
1992
2020
  name: this.idCookieName,
1993
2021
  domain: this.cookieDomain,
2022
+ crossDomainLinking: this.crossDomainLinking,
1994
2023
  })
1995
2024
  : "" }, user), ids: this._getIds(), utc_time: reformatDate(now.toISOString()), local_tz_offset: now.getTimezoneOffset() }, props), getDataFromParams(parseQuery(props.doc_search)));
1996
2025
  // id and name attributes will be checked on backend
@@ -2044,7 +2073,7 @@ var UsermavenClientImpl = /** @class */ (function () {
2044
2073
  };
2045
2074
  UsermavenClientImpl.prototype.init = function (options) {
2046
2075
  var _this = this;
2047
- var _a, _b, _c, _d;
2076
+ var _a, _b, _c, _d, _e;
2048
2077
  if (isWindowAvailable() && !options.force_use_fetch) {
2049
2078
  if (options.fetch) {
2050
2079
  getLogger().warn("Custom fetch implementation is provided to Usermaven. However, it will be ignored since Usermaven runs in browser");
@@ -2103,6 +2132,8 @@ var UsermavenClientImpl = /** @class */ (function () {
2103
2132
  : !!options.compat_mode;
2104
2133
  this.cookieDomain = options.cookie_domain || getCookieDomain();
2105
2134
  this.namespace = options.namespace || "usermaven";
2135
+ this.crossDomainLinking = (_a = options.cross_domain_linking) !== null && _a !== void 0 ? _a : true;
2136
+ this.domains = options.domains ? (options.domains).split(',').map(function (domain) { return domain.trim(); }) : [];
2106
2137
  this.trackingHost = getHostWithProtocol(options["tracking_host"] || "t.usermaven.com");
2107
2138
  this.randomizeUrl = options.randomize_url || false;
2108
2139
  this.apiKey = options.key;
@@ -2128,8 +2159,8 @@ var UsermavenClientImpl = /** @class */ (function () {
2128
2159
  var restored = this.propsPersistance.restore();
2129
2160
  if (restored) {
2130
2161
  this.permanentProperties = restored;
2131
- this.permanentProperties.globalProps = (_a = restored.globalProps) !== null && _a !== void 0 ? _a : {};
2132
- this.permanentProperties.propsPerEvent = (_b = restored.propsPerEvent) !== null && _b !== void 0 ? _b : {};
2162
+ this.permanentProperties.globalProps = (_b = restored.globalProps) !== null && _b !== void 0 ? _b : {};
2163
+ this.permanentProperties.propsPerEvent = (_c = restored.propsPerEvent) !== null && _c !== void 0 ? _c : {};
2133
2164
  }
2134
2165
  getLogger().debug("Restored persistent properties", this.permanentProperties);
2135
2166
  }
@@ -2146,6 +2177,11 @@ var UsermavenClientImpl = /** @class */ (function () {
2146
2177
  getLogger().debug('Default Configuration', this.config);
2147
2178
  // this.manageSession(this.config);
2148
2179
  this.manageAutoCapture(this.config);
2180
+ this.manageCrossDomainLinking({
2181
+ cross_domain_linking: this.crossDomainLinking,
2182
+ domains: this.domains,
2183
+ cookiePolicy: this.cookiePolicy
2184
+ });
2149
2185
  if (options.capture_3rd_party_cookies === false) {
2150
2186
  this._3pCookies = {};
2151
2187
  }
@@ -2175,8 +2211,8 @@ var UsermavenClientImpl = /** @class */ (function () {
2175
2211
  enableAutoPageviews(this);
2176
2212
  }
2177
2213
  this.retryTimeout = [
2178
- (_c = options.min_send_timeout) !== null && _c !== void 0 ? _c : this.retryTimeout[0],
2179
- (_d = options.max_send_timeout) !== null && _d !== void 0 ? _d : this.retryTimeout[1],
2214
+ (_d = options.min_send_timeout) !== null && _d !== void 0 ? _d : this.retryTimeout[0],
2215
+ (_e = options.max_send_timeout) !== null && _e !== void 0 ? _e : this.retryTimeout[1],
2180
2216
  ];
2181
2217
  if (!!options.max_send_attempts) {
2182
2218
  this.maxSendAttempts = options.max_send_attempts;
@@ -2261,13 +2297,38 @@ var UsermavenClientImpl = /** @class */ (function () {
2261
2297
  this.propsPersistance.save(this.permanentProperties);
2262
2298
  }
2263
2299
  };
2300
+ UsermavenClientImpl.prototype.manageCrossDomainLinking = function (options) {
2301
+ if (!isWindowAvailable() || !options.cross_domain_linking || options.domains.length === 0 || options.cookiePolicy === "strict") {
2302
+ return false;
2303
+ }
2304
+ var cookieName = this.idCookieName;
2305
+ var domains = options.domains || [];
2306
+ // Listen for all clicks on the page
2307
+ document.addEventListener('click', function (event) {
2308
+ // Check if the clicked element is a link
2309
+ var target = event.target;
2310
+ if (target.tagName.toLowerCase() === 'a') {
2311
+ // Check if the link is pointing to a different domain
2312
+ var href = target.getAttribute('href');
2313
+ if (href && href.startsWith('http')) {
2314
+ var url = new URL(href);
2315
+ var cookie = getCookie(cookieName);
2316
+ if (domains.includes(url.hostname) && cookie) {
2317
+ // Add the '_um' parameter to the URL
2318
+ url.searchParams.append('_um', cookie);
2319
+ target.setAttribute('href', url.toString());
2320
+ }
2321
+ }
2322
+ }
2323
+ }, false);
2324
+ };
2264
2325
  /**
2265
2326
  * Manage auto-capturing
2266
2327
  * @param options
2267
2328
  */
2268
2329
  UsermavenClientImpl.prototype.manageAutoCapture = function (options) {
2269
2330
  getLogger().debug("Auto Capture Status: ", this.config['autocapture']);
2270
- this.__autocapture_enabled = this.config['autocapture'];
2331
+ this.__autocapture_enabled = this.config['autocapture'] && isWindowAvailable();
2271
2332
  if (!this.__autocapture_enabled) {
2272
2333
  return;
2273
2334
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usermaven/nextjs",
3
- "version": "1.2.7",
3
+ "version": "1.3.0",
4
4
  "description": "Usermaven JavaScript SDK for NextJS",
5
5
  "author": "Usermaven <hello@usermaven.com>",
6
6
  "license": "MIT",