@usermaven/nextjs 1.5.0-rc.90 → 1.5.1-rc.91

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.
@@ -2,7 +2,7 @@ import * as React from 'react';
2
2
  import { UsermavenClient } from "@usermaven/sdk-js";
3
3
  import { PropsWithChildren } from "react";
4
4
  export interface UsermavenProviderProps {
5
- client: UsermavenClient;
5
+ client: UsermavenClient | null;
6
6
  }
7
7
  declare const UsermavenProvider: React.FC<PropsWithChildren<UsermavenProviderProps>>;
8
8
  export default UsermavenProvider;
package/lib/client.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import { UsermavenClient, UsermavenOptions } from "@usermaven/sdk-js";
2
- declare function createClient(params: UsermavenOptions): UsermavenClient;
2
+ declare function createClient(params: UsermavenOptions): UsermavenClient | null;
3
3
  export default createClient;
package/lib/client.js CHANGED
@@ -2,6 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const sdk_js_1 = require("@usermaven/sdk-js");
4
4
  function createClient(params) {
5
+ if (typeof window === 'undefined') {
6
+ return null;
7
+ }
5
8
  return (0, sdk_js_1.usermavenClient)(params);
6
9
  }
7
10
  exports.default = createClient;
package/lib/index.es.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as React from 'react';
2
- import { createContext, useContext, useCallback, useEffect, useState } from 'react';
2
+ import { createContext, useContext, useCallback, useRef, useEffect, useState } from 'react';
3
3
 
4
4
  const UsermavenContext = createContext(null);
5
5
 
@@ -1310,20 +1310,61 @@ u() && function(i, e) {
1310
1310
  }(document, window);
1311
1311
 
1312
1312
  function createClient(params) {
1313
+ if (typeof window === 'undefined') {
1314
+ return null;
1315
+ }
1313
1316
  return ke(params);
1314
1317
  }
1315
1318
 
1319
+ /******************************************************************************
1320
+ Copyright (c) Microsoft Corporation.
1321
+
1322
+ Permission to use, copy, modify, and/or distribute this software for any
1323
+ purpose with or without fee is hereby granted.
1324
+
1325
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
1326
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1327
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
1328
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1329
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
1330
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
1331
+ PERFORMANCE OF THIS SOFTWARE.
1332
+ ***************************************************************************** */
1333
+
1334
+ function __awaiter(thisArg, _arguments, P, generator) {
1335
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
1336
+ return new (P || (P = Promise))(function (resolve, reject) {
1337
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
1338
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
1339
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
1340
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
1341
+ });
1342
+ }
1343
+
1344
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
1345
+ var e = new Error(message);
1346
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
1347
+ };
1348
+
1349
+ const createNoopClient = () => ({
1350
+ trackPageView: () => { },
1351
+ id: () => __awaiter(void 0, void 0, void 0, function* () { }),
1352
+ track: () => { },
1353
+ rawTrack: () => { },
1354
+ set: () => { },
1355
+ unset: () => { },
1356
+ });
1316
1357
  function useUsermaven() {
1317
1358
  const client = useContext(UsermavenContext);
1318
1359
  if (!client) {
1319
- throw new Error("Before calling useUsermaven() hook, please wrap your component into <UsermavenProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react");
1360
+ return createNoopClient();
1320
1361
  }
1321
- const id = useCallback((userData, doNotSendEvent) => client === null || client === void 0 ? void 0 : client.id(userData, doNotSendEvent), [client]);
1322
- const trackPageView = useCallback(() => client === null || client === void 0 ? void 0 : client.track('pageview'), [client]);
1323
- const track = useCallback((typeName, payload) => client === null || client === void 0 ? void 0 : client.track(typeName, payload), [client]);
1324
- const rawTrack = useCallback((payload) => client === null || client === void 0 ? void 0 : client.rawTrack(payload), [client]);
1325
- const set = useCallback((properties, opts) => client === null || client === void 0 ? void 0 : client.set(properties, opts), [client]);
1326
- const unset = useCallback((propertyName, opts) => client === null || client === void 0 ? void 0 : client.unset(propertyName, opts), [client]);
1362
+ const id = useCallback((userData, doNotSendEvent) => client.id(userData, doNotSendEvent), [client]);
1363
+ const trackPageView = useCallback(() => client.track('pageview'), [client]);
1364
+ const track = useCallback((typeName, payload) => client.track(typeName, payload), [client]);
1365
+ const rawTrack = useCallback((payload) => client.rawTrack(payload), [client]);
1366
+ const set = useCallback((properties, opts) => client.set(properties, opts), [client]);
1367
+ const unset = useCallback((propertyName, opts) => client.unset(propertyName, opts), [client]);
1327
1368
  return Object.assign(Object.assign({}, client), { id,
1328
1369
  track,
1329
1370
  trackPageView,
@@ -1334,41 +1375,56 @@ function useUsermaven() {
1334
1375
 
1335
1376
  function useUrlChange() {
1336
1377
  const [url, setUrl] = useState('');
1378
+ const isClient = typeof window !== 'undefined';
1337
1379
  useEffect(() => {
1380
+ if (!isClient)
1381
+ return;
1338
1382
  setUrl(window.location.href);
1339
1383
  const handleUrlChange = () => {
1340
1384
  setUrl(window.location.href);
1341
1385
  };
1342
- window.addEventListener('popstate', handleUrlChange);
1343
- const originalPushState = window.history.pushState;
1344
- const originalReplaceState = window.history.replaceState;
1345
- window.history.pushState = function () {
1346
- originalPushState.apply(this, arguments);
1347
- handleUrlChange();
1348
- };
1349
- window.history.replaceState = function () {
1350
- originalReplaceState.apply(this, arguments);
1351
- handleUrlChange();
1386
+ const history = window.history;
1387
+ const originalPushState = history.pushState.bind(history);
1388
+ const originalReplaceState = history.replaceState.bind(history);
1389
+ const wrapHistoryMethod = (original) => {
1390
+ return function (...args) {
1391
+ const result = original.apply(this, args);
1392
+ handleUrlChange();
1393
+ return result;
1394
+ };
1352
1395
  };
1396
+ history.pushState = wrapHistoryMethod(originalPushState);
1397
+ history.replaceState = wrapHistoryMethod(originalReplaceState);
1398
+ window.addEventListener('popstate', handleUrlChange);
1353
1399
  return () => {
1354
1400
  window.removeEventListener('popstate', handleUrlChange);
1355
- window.history.pushState = originalPushState;
1356
- window.history.replaceState = originalReplaceState;
1401
+ history.pushState = originalPushState;
1402
+ history.replaceState = originalReplaceState;
1357
1403
  };
1358
- }, []);
1404
+ }, [isClient]);
1359
1405
  return url;
1360
1406
  }
1361
1407
  function usePageView(usermaven, opts = {}) {
1362
1408
  const url = useUrlChange();
1409
+ const isClient = typeof window !== 'undefined';
1410
+ const lastTrackedUrl = useRef('');
1363
1411
  const trackPageView = useCallback(() => {
1364
- if (typeof window === 'undefined') {
1412
+ if (!isClient || !usermaven)
1365
1413
  return;
1366
- }
1414
+ const currentUrl = window.location.href;
1415
+ if (lastTrackedUrl.current === currentUrl)
1416
+ return;
1417
+ lastTrackedUrl.current = currentUrl;
1367
1418
  if (opts.before) {
1368
1419
  opts.before(usermaven);
1369
1420
  }
1370
- usermaven.track((opts === null || opts === void 0 ? void 0 : opts.typeName) || 'pageview', Object.assign(Object.assign({}, opts.payload), { url: window.location.href, path: window.location.pathname, referrer: document.referrer, title: document.title }));
1371
- }, [usermaven, opts.before, opts.typeName, opts.payload]);
1421
+ try {
1422
+ usermaven.track((opts === null || opts === void 0 ? void 0 : opts.typeName) || 'pageview', Object.assign(Object.assign({}, opts.payload), { url: currentUrl, path: window.location.pathname, referrer: document.referrer || '', title: document.title, timestamp: new Date().toISOString() }));
1423
+ }
1424
+ catch (error) {
1425
+ console.warn('Usermaven pageview tracking error:', error);
1426
+ }
1427
+ }, [usermaven, opts.before, opts.typeName, opts.payload, isClient]);
1372
1428
  useEffect(() => {
1373
1429
  if (url) {
1374
1430
  trackPageView();
@@ -1,7 +1,8 @@
1
1
  import { EventPayload, UsermavenClient } from "@usermaven/sdk-js";
2
- declare function usePageView(usermaven: UsermavenClient, opts?: {
2
+ interface UsePageViewOptions {
3
3
  before?: (usermaven: UsermavenClient) => void;
4
4
  typeName?: string;
5
5
  payload?: EventPayload;
6
- }): UsermavenClient;
6
+ }
7
+ declare function usePageView(usermaven: UsermavenClient | null, opts?: UsePageViewOptions): UsermavenClient | null;
7
8
  export default usePageView;
@@ -3,41 +3,56 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const react_1 = require("react");
4
4
  function useUrlChange() {
5
5
  const [url, setUrl] = (0, react_1.useState)('');
6
+ const isClient = typeof window !== 'undefined';
6
7
  (0, react_1.useEffect)(() => {
8
+ if (!isClient)
9
+ return;
7
10
  setUrl(window.location.href);
8
11
  const handleUrlChange = () => {
9
12
  setUrl(window.location.href);
10
13
  };
11
- window.addEventListener('popstate', handleUrlChange);
12
- const originalPushState = window.history.pushState;
13
- const originalReplaceState = window.history.replaceState;
14
- window.history.pushState = function () {
15
- originalPushState.apply(this, arguments);
16
- handleUrlChange();
17
- };
18
- window.history.replaceState = function () {
19
- originalReplaceState.apply(this, arguments);
20
- handleUrlChange();
14
+ const history = window.history;
15
+ const originalPushState = history.pushState.bind(history);
16
+ const originalReplaceState = history.replaceState.bind(history);
17
+ const wrapHistoryMethod = (original) => {
18
+ return function (...args) {
19
+ const result = original.apply(this, args);
20
+ handleUrlChange();
21
+ return result;
22
+ };
21
23
  };
24
+ history.pushState = wrapHistoryMethod(originalPushState);
25
+ history.replaceState = wrapHistoryMethod(originalReplaceState);
26
+ window.addEventListener('popstate', handleUrlChange);
22
27
  return () => {
23
28
  window.removeEventListener('popstate', handleUrlChange);
24
- window.history.pushState = originalPushState;
25
- window.history.replaceState = originalReplaceState;
29
+ history.pushState = originalPushState;
30
+ history.replaceState = originalReplaceState;
26
31
  };
27
- }, []);
32
+ }, [isClient]);
28
33
  return url;
29
34
  }
30
35
  function usePageView(usermaven, opts = {}) {
31
36
  const url = useUrlChange();
37
+ const isClient = typeof window !== 'undefined';
38
+ const lastTrackedUrl = (0, react_1.useRef)('');
32
39
  const trackPageView = (0, react_1.useCallback)(() => {
33
- if (typeof window === 'undefined') {
40
+ if (!isClient || !usermaven)
34
41
  return;
35
- }
42
+ const currentUrl = window.location.href;
43
+ if (lastTrackedUrl.current === currentUrl)
44
+ return;
45
+ lastTrackedUrl.current = currentUrl;
36
46
  if (opts.before) {
37
47
  opts.before(usermaven);
38
48
  }
39
- usermaven.track((opts === null || opts === void 0 ? void 0 : opts.typeName) || 'pageview', Object.assign(Object.assign({}, opts.payload), { url: window.location.href, path: window.location.pathname, referrer: document.referrer, title: document.title }));
40
- }, [usermaven, opts.before, opts.typeName, opts.payload]);
49
+ try {
50
+ usermaven.track((opts === null || opts === void 0 ? void 0 : opts.typeName) || 'pageview', Object.assign(Object.assign({}, opts.payload), { url: currentUrl, path: window.location.pathname, referrer: document.referrer || '', title: document.title, timestamp: new Date().toISOString() }));
51
+ }
52
+ catch (error) {
53
+ console.warn('Usermaven pageview tracking error:', error);
54
+ }
55
+ }, [usermaven, opts.before, opts.typeName, opts.payload, isClient]);
41
56
  (0, react_1.useEffect)(() => {
42
57
  if (url) {
43
58
  trackPageView();
@@ -1,18 +1,35 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  const react_1 = require("react");
4
13
  const UsermavenContext_1 = require("./UsermavenContext");
14
+ const createNoopClient = () => ({
15
+ trackPageView: () => { },
16
+ id: () => __awaiter(void 0, void 0, void 0, function* () { }),
17
+ track: () => { },
18
+ rawTrack: () => { },
19
+ set: () => { },
20
+ unset: () => { },
21
+ });
5
22
  function useUsermaven() {
6
23
  const client = (0, react_1.useContext)(UsermavenContext_1.default);
7
24
  if (!client) {
8
- throw new Error("Before calling useUsermaven() hook, please wrap your component into <UsermavenProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react");
25
+ return createNoopClient();
9
26
  }
10
- const id = (0, react_1.useCallback)((userData, doNotSendEvent) => client === null || client === void 0 ? void 0 : client.id(userData, doNotSendEvent), [client]);
11
- const trackPageView = (0, react_1.useCallback)(() => client === null || client === void 0 ? void 0 : client.track('pageview'), [client]);
12
- const track = (0, react_1.useCallback)((typeName, payload) => client === null || client === void 0 ? void 0 : client.track(typeName, payload), [client]);
13
- const rawTrack = (0, react_1.useCallback)((payload) => client === null || client === void 0 ? void 0 : client.rawTrack(payload), [client]);
14
- const set = (0, react_1.useCallback)((properties, opts) => client === null || client === void 0 ? void 0 : client.set(properties, opts), [client]);
15
- const unset = (0, react_1.useCallback)((propertyName, opts) => client === null || client === void 0 ? void 0 : client.unset(propertyName, opts), [client]);
27
+ const id = (0, react_1.useCallback)((userData, doNotSendEvent) => client.id(userData, doNotSendEvent), [client]);
28
+ const trackPageView = (0, react_1.useCallback)(() => client.track('pageview'), [client]);
29
+ const track = (0, react_1.useCallback)((typeName, payload) => client.track(typeName, payload), [client]);
30
+ const rawTrack = (0, react_1.useCallback)((payload) => client.rawTrack(payload), [client]);
31
+ const set = (0, react_1.useCallback)((properties, opts) => client.set(properties, opts), [client]);
32
+ const unset = (0, react_1.useCallback)((propertyName, opts) => client.unset(propertyName, opts), [client]);
16
33
  return Object.assign(Object.assign({}, client), { id,
17
34
  track,
18
35
  trackPageView,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usermaven/nextjs",
3
- "version": "1.5.0-rc.90",
3
+ "version": "1.5.1-rc.91",
4
4
  "description": "Usermaven JavaScript SDK for NextJS",
5
5
  "author": "Usermaven <hello@usermaven.com>",
6
6
  "license": "MIT",
@@ -18,7 +18,7 @@
18
18
  "access": "public"
19
19
  },
20
20
  "dependencies": {
21
- "@usermaven/sdk-js": "1.5.0-rc.90",
21
+ "@usermaven/sdk-js": "1.5.1-rc.91",
22
22
  "cookie": "^0.5.0"
23
23
  },
24
24
  "peerDependencies": {
@@ -33,7 +33,8 @@
33
33
  "next": "^12.1.5",
34
34
  "react": "^17.0.2",
35
35
  "rollup": "^2.70.1",
36
- "rollup-plugin-peer-deps-external": "^2.2.4"
36
+ "rollup-plugin-peer-deps-external": "^2.2.4",
37
+ "tslib": "^2.3.1"
37
38
  },
38
39
  "repository": {
39
40
  "type": "git",