@usermaven/nextjs 1.2.8 → 1.3.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.
Files changed (2) hide show
  1. package/lib/index.es.js +80 -8
  2. package/package.json +1 -1
package/lib/index.es.js CHANGED
@@ -788,6 +788,13 @@ function _copyAndTruncateStrings(object, maxStringLength) {
788
788
  return value;
789
789
  });
790
790
  }
791
+ // Function to find the closest link element
792
+ function _findClosestLink(element) {
793
+ while (element && element.tagName.toLowerCase() !== 'a') {
794
+ element = element.parentNode;
795
+ }
796
+ return element;
797
+ }
791
798
 
792
799
  /*
793
800
  * Get the className of an element, accounting for edge cases where element.className is an object
@@ -1399,8 +1406,8 @@ _safewrap_instance_methods(autocapture);
1399
1406
 
1400
1407
  var VERSION_INFO = {
1401
1408
  env: 'production',
1402
- date: '2024-01-30T10:00:56.526Z',
1403
- version: '1.2.8'
1409
+ date: '2024-02-06T12:31:10.738Z',
1410
+ version: '1.3.1'
1404
1411
  };
1405
1412
  var USERMAVEN_VERSION = VERSION_INFO.version + "/" + VERSION_INFO.env + "@" + VERSION_INFO.date;
1406
1413
  var MAX_AGE_TEN_YEARS = 31622400 * 10;
@@ -1507,8 +1514,34 @@ var browserEnv = {
1507
1514
  doc_encoding: document.characterSet,
1508
1515
  }); },
1509
1516
  getAnonymousId: function (_a) {
1510
- var name = _a.name, domain = _a.domain;
1517
+ var name = _a.name, domain = _a.domain, _b = _a.crossDomainLinking, crossDomainLinking = _b === void 0 ? true : _b;
1511
1518
  expireNonRootCookies(name);
1519
+ // Check if cross domain linking is enabled
1520
+ if (crossDomainLinking) {
1521
+ // Try to extract the '_um' parameter from query string and hash fragment (https://example.com#_um=1~abcde5~)
1522
+ var urlParams = new URLSearchParams(window.location.search);
1523
+ var queryId = urlParams.get('_um');
1524
+ var urlHash = window.location.hash.substring(1);
1525
+ var hashedValues = urlHash.split("~");
1526
+ var fragmentId = hashedValues.length > 1 ? hashedValues[1] : undefined;
1527
+ // If the '_um' parameter is set in both the query string and hash fragment,
1528
+ // prioritize the one in query string
1529
+ var crossDomainAnonymousId = queryId || fragmentId;
1530
+ // If coming from another domain, use the ID from URL parameter
1531
+ if (crossDomainAnonymousId) {
1532
+ getLogger().debug("Existing user id from other domain", crossDomainAnonymousId);
1533
+ // Check if the ID needs to be set as cookie
1534
+ var currentCookie = getCookie(name);
1535
+ if (!currentCookie || currentCookie !== crossDomainAnonymousId) {
1536
+ setCookie(name, crossDomainAnonymousId, {
1537
+ domain: domain,
1538
+ secure: document.location.protocol !== "http:",
1539
+ maxAge: MAX_AGE_TEN_YEARS,
1540
+ });
1541
+ }
1542
+ return crossDomainAnonymousId;
1543
+ }
1544
+ }
1512
1545
  var idCookie = getCookie(name);
1513
1546
  if (idCookie) {
1514
1547
  getLogger().debug("Existing user id", idCookie);
@@ -1728,6 +1761,8 @@ var UsermavenClientImpl = /** @class */ (function () {
1728
1761
  this.idCookieName = "";
1729
1762
  this.randomizeUrl = false;
1730
1763
  this.namespace = "usermaven";
1764
+ this.crossDomainLinking = true;
1765
+ this.domains = [];
1731
1766
  this.apiKey = "";
1732
1767
  this.initialized = false;
1733
1768
  this._3pCookies = {};
@@ -1991,6 +2026,7 @@ var UsermavenClientImpl = /** @class */ (function () {
1991
2026
  ? env.getAnonymousId({
1992
2027
  name: this.idCookieName,
1993
2028
  domain: this.cookieDomain,
2029
+ crossDomainLinking: this.crossDomainLinking,
1994
2030
  })
1995
2031
  : "" }, user), ids: this._getIds(), utc_time: reformatDate(now.toISOString()), local_tz_offset: now.getTimezoneOffset() }, props), getDataFromParams(parseQuery(props.doc_search)));
1996
2032
  // id and name attributes will be checked on backend
@@ -2044,7 +2080,7 @@ var UsermavenClientImpl = /** @class */ (function () {
2044
2080
  };
2045
2081
  UsermavenClientImpl.prototype.init = function (options) {
2046
2082
  var _this = this;
2047
- var _a, _b, _c, _d;
2083
+ var _a, _b, _c, _d, _e;
2048
2084
  if (isWindowAvailable() && !options.force_use_fetch) {
2049
2085
  if (options.fetch) {
2050
2086
  getLogger().warn("Custom fetch implementation is provided to Usermaven. However, it will be ignored since Usermaven runs in browser");
@@ -2103,6 +2139,8 @@ var UsermavenClientImpl = /** @class */ (function () {
2103
2139
  : !!options.compat_mode;
2104
2140
  this.cookieDomain = options.cookie_domain || getCookieDomain();
2105
2141
  this.namespace = options.namespace || "usermaven";
2142
+ this.crossDomainLinking = (_a = options.cross_domain_linking) !== null && _a !== void 0 ? _a : true;
2143
+ this.domains = options.domains ? (options.domains).split(',').map(function (domain) { return domain.trim(); }) : [];
2106
2144
  this.trackingHost = getHostWithProtocol(options["tracking_host"] || "t.usermaven.com");
2107
2145
  this.randomizeUrl = options.randomize_url || false;
2108
2146
  this.apiKey = options.key;
@@ -2128,8 +2166,8 @@ var UsermavenClientImpl = /** @class */ (function () {
2128
2166
  var restored = this.propsPersistance.restore();
2129
2167
  if (restored) {
2130
2168
  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 : {};
2169
+ this.permanentProperties.globalProps = (_b = restored.globalProps) !== null && _b !== void 0 ? _b : {};
2170
+ this.permanentProperties.propsPerEvent = (_c = restored.propsPerEvent) !== null && _c !== void 0 ? _c : {};
2133
2171
  }
2134
2172
  getLogger().debug("Restored persistent properties", this.permanentProperties);
2135
2173
  }
@@ -2146,6 +2184,11 @@ var UsermavenClientImpl = /** @class */ (function () {
2146
2184
  getLogger().debug('Default Configuration', this.config);
2147
2185
  // this.manageSession(this.config);
2148
2186
  this.manageAutoCapture(this.config);
2187
+ this.manageCrossDomainLinking({
2188
+ cross_domain_linking: this.crossDomainLinking,
2189
+ domains: this.domains,
2190
+ cookiePolicy: this.cookiePolicy
2191
+ });
2149
2192
  if (options.capture_3rd_party_cookies === false) {
2150
2193
  this._3pCookies = {};
2151
2194
  }
@@ -2175,8 +2218,8 @@ var UsermavenClientImpl = /** @class */ (function () {
2175
2218
  enableAutoPageviews(this);
2176
2219
  }
2177
2220
  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],
2221
+ (_d = options.min_send_timeout) !== null && _d !== void 0 ? _d : this.retryTimeout[0],
2222
+ (_e = options.max_send_timeout) !== null && _e !== void 0 ? _e : this.retryTimeout[1],
2180
2223
  ];
2181
2224
  if (!!options.max_send_attempts) {
2182
2225
  this.maxSendAttempts = options.max_send_attempts;
@@ -2261,6 +2304,35 @@ var UsermavenClientImpl = /** @class */ (function () {
2261
2304
  this.propsPersistance.save(this.permanentProperties);
2262
2305
  }
2263
2306
  };
2307
+ UsermavenClientImpl.prototype.manageCrossDomainLinking = function (options) {
2308
+ if (!isWindowAvailable() || !options.cross_domain_linking || options.domains.length === 0 || options.cookiePolicy === "strict") {
2309
+ return false;
2310
+ }
2311
+ var cookieName = this.idCookieName;
2312
+ var domains = options.domains || [];
2313
+ // Listen for all clicks on the page
2314
+ document.addEventListener('click', function (event) {
2315
+ // Find the closest link
2316
+ var target = _findClosestLink(event.target);
2317
+ if (target) {
2318
+ // Check if the link is pointing to a different domain
2319
+ var href = target.getAttribute('href');
2320
+ if (href && href.startsWith('http')) {
2321
+ var url = new URL(href);
2322
+ var cookie = getCookie(cookieName);
2323
+ // Skip the link if it's pointing to the current domain
2324
+ if (url.hostname === window.location.hostname) {
2325
+ return;
2326
+ }
2327
+ if (domains.includes(url.hostname) && cookie) {
2328
+ // Add the '_um' parameter to the URL
2329
+ url.searchParams.append('_um', cookie);
2330
+ target.setAttribute('href', url.toString());
2331
+ }
2332
+ }
2333
+ }
2334
+ }, false);
2335
+ };
2264
2336
  /**
2265
2337
  * Manage auto-capturing
2266
2338
  * @param options
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usermaven/nextjs",
3
- "version": "1.2.8",
3
+ "version": "1.3.1",
4
4
  "description": "Usermaven JavaScript SDK for NextJS",
5
5
  "author": "Usermaven <hello@usermaven.com>",
6
6
  "license": "MIT",