@jitsu/jitsu-react 1.0.0-canary-20230219230011 → 1.1.0-canary.14.20230602211353

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/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # jitsu-react
2
2
 
3
- >
4
3
 
5
- [![NPM](https://img.shields.io/npm/v/jitsu-react2.svg)](https://www.npmjs.com/package/jitsu-react2) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
4
+ [![NPM](https://img.shields.io/npm/v/@jitsu/jitsu-react.svg)](https://www.npmjs.com/package/@jitsu/jitsu-react)
6
5
 
7
6
  ## Install
8
7
 
@@ -13,111 +12,41 @@ npm install --save @jitsu/jitsu-react
13
12
  ## Usage
14
13
 
15
14
  To setup Jitsu-React library you need to add `JitsuProvider` component close to the root level of your app:
16
- ```tsx
17
- import React, { Component } from 'react'
18
- import { JitsuProvider } from "@jitsu/jitsu-react";
19
-
20
- class Example extends Component {
21
- render() {
22
- return <JitsuProvider options={{ host: "https://mysiteid.d.jitsu.com" }} >
23
- <App />
24
- </JitsuProvider>
25
- }
26
- }
27
- ```
28
15
 
29
- Then use `useJitsu` hook in components where you want to track events.
30
16
  ```tsx
31
- import * as React from "react";
32
- import { useJitsu } from "@jitsu/jitsu-react";
17
+ import React from "react";
18
+ import { JitsuProvider } from "@jitsu/jitsu-react";
33
19
 
34
20
  export default function App() {
35
- const { analytics } = useJitsu();
36
- // manually track page view
37
- analytics.page();
38
-
39
- return (
40
- <div>
41
- ...
42
- </div>
43
- );
21
+ return <JitsuProvider options={{ host: "https://<id>.d.jitsu.com" }}>
22
+ <Page />
23
+ </JitsuProvider>;
44
24
  }
45
25
  ```
46
26
 
47
- ## Auto Page Tracking
48
-
49
- ### For react-router:
50
- To enable auto page tracking with react-router, pass `useLocation` hook from `react-router-dom` to `autoPageTracking` option:
51
- ```tsx
52
- import * as React from "react";
53
- import { useLocation } from "react-router-dom";
54
- import { useJitsu } from "@jitsu/jitsu-react";
55
-
56
- export default function App() {
57
- useJitsu({
58
- // Enable auto page tracking with react-router
59
- autoPageTracking: {
60
- reactRouter: useLocation
61
- },
62
- });
63
-
64
- return (
65
- <div>
66
- ...
67
- </div>
68
- );
69
- }
70
- ```
27
+ Then use `useJitsu` hook in components where you want to track events.
71
28
 
72
- ### For Next.JS router:
73
- To enable auto page tracking with Next.JS router, pass `useRouter` hook from `next/router` to `autoPageTracking` option:
74
29
  ```tsx
75
30
  import * as React from "react";
76
- import { useRouter } from "next/router";
77
31
  import { useJitsu } from "@jitsu/jitsu-react";
32
+ import { useEffect } from "react";
78
33
 
79
- export default function App() {
80
- useJitsu({
81
- // Enable auto page tracking with Next.JS router
82
- autoPageTracking: {
83
- nextjsRouter: useRouter
84
- },
85
- });
34
+ export default function Page() {
35
+ const { analytics } = useJitsu();
36
+ useEffect(() => {
37
+ // Track page view
38
+ analytics.track("event", { prop: "value" });
39
+ }, [location]);
86
40
 
87
41
  return (
88
42
  <div>
89
- ...
43
+ <button onClick={() => analytics.track("button-click")}>Click me!</button>
90
44
  </div>
91
45
  );
92
46
  }
93
47
  ```
94
48
 
95
-
96
- ## User 'identify' events
97
-
98
- Please check `../../examples/react-app` for a full example of how to automatically send `identify` event when user logins on your site.
99
-
100
- Partial snippet:
101
- ```tsx
102
- export default function App() {
103
- const user = useUser();
104
-
105
- useJitsu({
106
- autoPageTracking: {
107
- reactRouter: useLocation,
108
- before: function (analytics) {
109
- if (user && user.id) {
110
- analytics.identify(user.id, { name: user.name, email: user.email });
111
- }
112
- },
113
- beforeDeps: [user],
114
- },
115
- });
116
-
117
- return (
118
- <div>
119
- ...
120
- </div>
121
- );
122
- }
123
- ```
49
+ As `location` you should use an value that changes on every navigation. Examples:
50
+ * React Router: `useLocationHook()`
51
+ * Next.js: `const rounter = useRouter()`; then `router.asPath`
52
+ * Others: `[window.location.pathname, window.location.search, window.location.hash]`
@@ -1,23 +1,7 @@
1
1
  /// <reference types="react" />
2
2
  import { AnalyticsInterface } from "@jitsu/js";
3
- import { ExtendedJitsuOptions } from "./useJitsu";
4
- /**
5
- * An instance if Jitsu that is kept in the context. It's one of:
6
- * - AnalyticsInterface - for CSR, if Jitsu is initialized in browser
7
- * - ExtendedJitsuOptions - for SSR, if provider can't access context
8
- */
9
3
  export declare type JitsuInstance = {
10
4
  analytics: AnalyticsInterface;
11
- initOptions?: never;
12
- } | {
13
- analytics?: never;
14
- initOptions: ExtendedJitsuOptions;
15
5
  };
16
- declare const JitsuContext: import("react").Context<{
17
- analytics: AnalyticsInterface;
18
- initOptions?: undefined;
19
- } | {
20
- analytics?: undefined;
21
- initOptions: ExtendedJitsuOptions;
22
- } | null>;
6
+ declare const JitsuContext: import("react").Context<JitsuInstance | null>;
23
7
  export default JitsuContext;
package/dist/index.js CHANGED
@@ -4,46 +4,33 @@ var js = require('@jitsu/js');
4
4
  var JitsuContext = React.createContext(null);
5
5
 
6
6
  var JitsuProvider = function JitsuProvider(props) {
7
- if (props.options.disabled) {
8
- return React.createElement(React.Fragment, null, props.children);
9
- }
10
- if (!props.options.host) {
11
- var msg = "<JitsuProvider />. Jitsu host is not defined. Jitsu will not be initialized";
12
- console.error("%c" + msg, "color: red; font-weight: bold;");
13
- console.error(msg);
14
- return React.createElement(React.Fragment, null, props.children);
15
- }
16
- return React.createElement(JitsuContext.Provider, {
17
- value: js.isInBrowser() ? {
18
- analytics: js.jitsuAnalytics(props.options)
19
- } : {
20
- initOptions: props.options
7
+ var instance = React.useMemo(function () {
8
+ if (props.options.disabled) {
9
+ return {
10
+ analytics: js.emptyAnalytics
11
+ };
12
+ } else if (!props.options.host) {
13
+ var msg = "<JitsuProvider />. Jitsu host is not defined. Jitsu will not be initialized";
14
+ console.error("%c" + msg, "color: red; font-weight: bold;");
15
+ return {
16
+ analytics: js.emptyAnalytics
17
+ };
18
+ } else {
19
+ return {
20
+ analytics: js.jitsuAnalytics(props.options)
21
+ };
21
22
  }
23
+ }, [props.options.disabled, props.options.host]);
24
+ return React.createElement(JitsuContext.Provider, {
25
+ value: instance
22
26
  }, props.children);
23
27
  };
24
28
 
25
- var emptyAnalytics = {
26
- track: function track() {
27
- return Promise.resolve();
28
- },
29
- page: function page() {
30
- return Promise.resolve();
31
- },
32
- user: function user() {
33
- return {};
34
- },
35
- identify: function identify() {
36
- return Promise.resolve({});
37
- },
38
- reset: function reset() {
39
- return Promise.resolve({});
40
- }
41
- };
42
29
  function useJitsu(opts) {
43
30
  var jitsuInstance = React.useContext(JitsuContext);
44
31
  if (opts !== null && opts !== void 0 && opts.disabled) {
45
32
  return {
46
- analytics: emptyAnalytics
33
+ analytics: js.emptyAnalytics
47
34
  };
48
35
  }
49
36
  if (!jitsuInstance) {
@@ -53,22 +40,17 @@ function useJitsu(opts) {
53
40
  };
54
41
  } else {
55
42
  var msg = "Before calling useJitsu() hook, please wrap your component into <JitsuProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react";
56
- console.error("%c" + msg, "color: red; font-size: 16px; font-weight: bold;");
43
+ console.error("%c" + msg, "color: red; font-weight: bold;");
57
44
  return {
58
- analytics: emptyAnalytics
45
+ analytics: js.emptyAnalytics
59
46
  };
60
47
  }
61
48
  } else if (opts !== null && opts !== void 0 && opts.host || opts !== null && opts !== void 0 && opts.echoEvents) {
62
49
  throw new Error("Jitsu client already set up with <JitsuProvider /> and cannot be overridden. Read more in http://jitsu.com/docs/sending-data/js-sdk/react");
63
50
  } else if (jitsuInstance.analytics) {
64
- return {
65
- analytics: jitsuInstance.analytics
66
- };
51
+ return jitsuInstance;
67
52
  } else {
68
- var analytics = jitsuInstance.initOptions.disabled ? emptyAnalytics : js.jitsuAnalytics(jitsuInstance.initOptions);
69
- return {
70
- analytics: analytics
71
- };
53
+ throw new Error("<JitsuProvider /> is not initialized with undefined analytics instance");
72
54
  }
73
55
  }
74
56
 
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/JitsuContext.tsx","../src/JitsuProvider.tsx","../src/useJitsu.ts"],"sourcesContent":["import { createContext } from \"react\";\nimport { AnalyticsInterface } from \"@jitsu/js\";\nimport { ExtendedJitsuOptions } from \"./useJitsu\";\n\n/**\n * An instance if Jitsu that is kept in the context. It's one of:\n * - AnalyticsInterface - for CSR, if Jitsu is initialized in browser\n * - ExtendedJitsuOptions - for SSR, if provider can't access context\n */\nexport type JitsuInstance =\n | { analytics: AnalyticsInterface; initOptions?: never }\n | { analytics?: never; initOptions: ExtendedJitsuOptions };\n\nconst JitsuContext = createContext<JitsuInstance | null>(null);\n\nexport default JitsuContext;\n","import * as React from \"react\";\nimport { PropsWithChildren } from \"react\";\nimport JitsuContext from \"./JitsuContext\";\nimport { jitsuAnalytics, isInBrowser } from \"@jitsu/js\";\nimport { ExtendedJitsuOptions } from \"./useJitsu\";\n\nconst JitsuProvider: React.FC<PropsWithChildren<{ options: ExtendedJitsuOptions }>> = props => {\n if (props.options.disabled) {\n return <React.Fragment>{props.children}</React.Fragment>;\n }\n if (!props.options.host) {\n const msg = \"<JitsuProvider />. Jitsu host is not defined. Jitsu will not be initialized\";\n console.error(`%c${msg}`, \"color: red; font-weight: bold;\");\n console.error(msg);\n return <React.Fragment>{props.children}</React.Fragment>;\n }\n return (\n <JitsuContext.Provider\n value={isInBrowser() ? { analytics: jitsuAnalytics(props.options) } : { initOptions: props.options }}\n >\n {props.children}\n </JitsuContext.Provider>\n );\n};\n\nexport default JitsuProvider;\n","import { useContext } from \"react\";\nimport JitsuContext from \"./JitsuContext\";\n\nimport { AnalyticsInterface, jitsuAnalytics, JitsuOptions } from \"@jitsu/js\";\n\nexport interface BeforeEffect {\n effect: (analytics: AnalyticsInterface) => any;\n deps: any[];\n}\n\nexport type ExtendedJitsuOptions =\n | (Omit<JitsuOptions, \"host\"> & { host: string } & {\n disabled?: false | undefined;\n echoEvents?: boolean;\n })\n | { disabled: true };\n\nconst emptyAnalytics = {\n track: () => Promise.resolve(),\n page: () => Promise.resolve(),\n user: () => ({}),\n identify: () => Promise.resolve({}),\n reset: () => Promise.resolve({}),\n};\n\n/**\n * See for details http://jitsu.com/docs/sending-data/js-sdk/react\n */\nfunction useJitsu(opts?: ExtendedJitsuOptions): { analytics: AnalyticsInterface } {\n let jitsuInstance = useContext(JitsuContext);\n if (opts?.disabled) {\n return { analytics: emptyAnalytics };\n }\n //useJitsu() isn't wrapped into JitsuProvider. Return new instance of Jitsu provider if config is provided\n if (!jitsuInstance) {\n if (opts?.host || opts?.echoEvents) {\n return { analytics: jitsuAnalytics(opts) };\n } else {\n const msg =\n \"Before calling useJitsu() hook, please wrap your component into <JitsuProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react\";\n console.error(`%c${msg}`, \"color: red; font-size: 16px; font-weight: bold;\");\n return { analytics: emptyAnalytics };\n }\n } else if (opts?.host || opts?.echoEvents) {\n throw new Error(\n \"Jitsu client already set up with <JitsuProvider /> and cannot be overridden. Read more in http://jitsu.com/docs/sending-data/js-sdk/react\"\n );\n //Jitsu analytics is initialized inside provider\n } else if (jitsuInstance.analytics) {\n return { analytics: jitsuInstance.analytics };\n } else {\n const analytics = jitsuInstance.initOptions.disabled ? emptyAnalytics : jitsuAnalytics(jitsuInstance.initOptions);\n return { analytics };\n }\n}\n\nexport default useJitsu;\nexport { AnalyticsInterface, JitsuOptions };\n"],"names":["JitsuContext","createContext","JitsuProvider","props","options","disabled","React","children","host","msg","console","error","Provider","value","isInBrowser","analytics","jitsuAnalytics","initOptions","emptyAnalytics","track","Promise","resolve","page","user","identify","reset","useJitsu","opts","jitsuInstance","useContext","echoEvents","Error"],"mappings":";;;AAaA,IAAMA,YAAY,GAAGC,mBAAa,CAAuB,IAAI,CAAC;;ACP9D,IAAMC,aAAa,GAAmE,SAAhFA,aAAa,CAAmEC,KAAK;EACzF,IAAIA,KAAK,CAACC,OAAO,CAACC,QAAQ,EAAE;IAC1B,OAAOC,oBAACA,cAAc,QAAEH,KAAK,CAACI,QAAQ,CAAkB;;EAE1D,I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GAAG,GACP,iJAAiJ;MACnJC,OAAO,CAACC,KAAK,QAAMF,GAAG,EAAI,iDAAiD,CAAC;MAC5E,OAAO;QAAEM,SAAS,EAAEG;OAAgB;;GAEvC,MAAM,IAAIS,IAAI,aAAJA,IAAI,eAAJA,IAAI,CAAEnB,IAAI,IAAImB,IAAI,aAAJA,IAAI,eAAJA,IAAI,CAAEG,UAAU,EAAE;IACzC,MAAM,IAAIC,KAAK,CACb,2IAA2I,CAC5I;GAEF,MAAM,IAAIH,aAAa,CAACb,SAAS,EAAE;IAClC,OAAO;MAAEA,SAAS,EAAEa,aAAa,CAACb;KAAW;GAC9C,MAAM;IACL,IAAMA,SAAS,GAAGa,aAAa,CAACX,WAAW,CAACZ,QAAQ,GAAGa,cAAc,GAAGF,iBAAc,CAACY,aAAa,CAACX,WAAW,CAAC;IACjH,OAAO;MAAEF,SAAS,EAATA;KAAW;;AAExB;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/JitsuContext.tsx","../src/JitsuProvider.tsx","../src/useJitsu.ts"],"sourcesContent":["import { createContext } from \"react\";\nimport { AnalyticsInterface } from \"@jitsu/js\";\n\nexport type JitsuInstance = { analytics: AnalyticsInterface };\n\nconst JitsuContext = createContext<JitsuInstance | null>(null);\n\nexport default JitsuContext;\n","import * as React from \"react\";\nimport { PropsWithChildren, useMemo } from \"react\";\nimport JitsuContext, { JitsuInstance } from \"./JitsuContext\";\nimport { emptyAnalytics, jitsuAnalytics } from \"@jitsu/js\";\nimport { ExtendedJitsuOptions } from \"./useJitsu\";\n\nconst JitsuProvider: React.FC<PropsWithChildren<{ options: ExtendedJitsuOptions }>> = props => {\n const instance: JitsuInstance = useMemo(() => {\n if (props.options.disabled) {\n return { analytics: emptyAnalytics };\n } else if (!props.options.host) {\n const msg = \"<JitsuProvider />. Jitsu host is not defined. Jitsu will not be initialized\";\n console.error(`%c${msg}`, \"color: red; font-weight: bold;\");\n return { analytics: emptyAnalytics };\n } else {\n return { analytics: jitsuAnalytics(props.options) };\n }\n }, [props.options.disabled, props.options.host]);\n return <JitsuContext.Provider value={instance}>{props.children}</JitsuContext.Provider>;\n};\n\nexport default JitsuProvider;\n","import { useContext } from \"react\";\nimport JitsuContext from \"./JitsuContext\";\n\nimport { AnalyticsInterface, jitsuAnalytics, JitsuOptions, emptyAnalytics } from \"@jitsu/js\";\n\nexport interface BeforeEffect {\n effect: (analytics: AnalyticsInterface) => any;\n deps: any[];\n}\n\nexport type ExtendedJitsuOptions =\n | (Omit<JitsuOptions, \"host\"> & { host: string } & {\n disabled?: false | undefined;\n echoEvents?: boolean;\n })\n | { disabled: true; host?: undefined };\n\n/**\n * See for details http://jitsu.com/docs/sending-data/js-sdk/react\n */\nfunction useJitsu(opts?: ExtendedJitsuOptions): { analytics: AnalyticsInterface } {\n let jitsuInstance = useContext(JitsuContext);\n if (opts?.disabled) {\n return { analytics: emptyAnalytics };\n }\n if (!jitsuInstance) {\n if (opts?.host || opts?.echoEvents) {\n return { analytics: jitsuAnalytics(opts) };\n } else {\n const msg =\n \"Before calling useJitsu() hook, please wrap your component into <JitsuProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react\";\n console.error(`%c${msg}`, \"color: red; font-weight: bold;\");\n return { analytics: emptyAnalytics };\n }\n } else if (opts?.host || opts?.echoEvents) {\n throw new Error(\n \"Jitsu client already set up with <JitsuProvider /> and cannot be overridden. Read more in http://jitsu.com/docs/sending-data/js-sdk/react\"\n );\n //Jitsu analytics is initialized inside provider\n } else if (jitsuInstance.analytics) {\n return jitsuInstance;\n } else {\n throw new Error(`<JitsuProvider /> is not initialized with undefined analytics instance`);\n }\n}\n\nexport default useJitsu;\nexport { AnalyticsInterface, JitsuOptions };\n"],"names":["JitsuContext","createContext","JitsuProvider","props","instance","useMemo","options","disabled","analytics","emptyAnalytics","host","msg","console","error","jitsuAnalytics","React","Provider","value","children","useJitsu","opts","jitsuInstance","useContext","echoEvents","Error"],"mappings":";;;AAKA,IAAMA,YAAY,GAAGC,mBAAa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EC;OAAgB;;GAEvC,MAAM,IAAIW,IAAI,aAAJA,IAAI,eAAJA,IAAI,CAAEV,IAAI,IAAIU,IAAI,aAAJA,IAAI,eAAJA,IAAI,CAAEG,UAAU,EAAE;IACzC,MAAM,IAAIC,KAAK,CACb,2IAA2I,CAC5I;GAEF,MAAM,IAAIH,aAAa,CAACb,SAAS,EAAE;IAClC,OAAOa,aAAa;GACrB,MAAM;IACL,MAAM,IAAIG,KAAK,0EAA0E;;AAE7F;;;;;;"}
@@ -1,44 +1,31 @@
1
- import { createContext, createElement, Fragment, useContext } from 'react';
2
- import { isInBrowser, jitsuAnalytics } from '@jitsu/js';
1
+ import { createContext, useMemo, createElement, useContext } from 'react';
2
+ import { emptyAnalytics, jitsuAnalytics } from '@jitsu/js';
3
3
 
4
4
  var JitsuContext = createContext(null);
5
5
 
6
6
  var JitsuProvider = function JitsuProvider(props) {
7
- if (props.options.disabled) {
8
- return createElement(Fragment, null, props.children);
9
- }
10
- if (!props.options.host) {
11
- var msg = "<JitsuProvider />. Jitsu host is not defined. Jitsu will not be initialized";
12
- console.error("%c" + msg, "color: red; font-weight: bold;");
13
- console.error(msg);
14
- return createElement(Fragment, null, props.children);
15
- }
16
- return createElement(JitsuContext.Provider, {
17
- value: isInBrowser() ? {
18
- analytics: jitsuAnalytics(props.options)
19
- } : {
20
- initOptions: props.options
7
+ var instance = useMemo(function () {
8
+ if (props.options.disabled) {
9
+ return {
10
+ analytics: emptyAnalytics
11
+ };
12
+ } else if (!props.options.host) {
13
+ var msg = "<JitsuProvider />. Jitsu host is not defined. Jitsu will not be initialized";
14
+ console.error("%c" + msg, "color: red; font-weight: bold;");
15
+ return {
16
+ analytics: emptyAnalytics
17
+ };
18
+ } else {
19
+ return {
20
+ analytics: jitsuAnalytics(props.options)
21
+ };
21
22
  }
23
+ }, [props.options.disabled, props.options.host]);
24
+ return createElement(JitsuContext.Provider, {
25
+ value: instance
22
26
  }, props.children);
23
27
  };
24
28
 
25
- var emptyAnalytics = {
26
- track: function track() {
27
- return Promise.resolve();
28
- },
29
- page: function page() {
30
- return Promise.resolve();
31
- },
32
- user: function user() {
33
- return {};
34
- },
35
- identify: function identify() {
36
- return Promise.resolve({});
37
- },
38
- reset: function reset() {
39
- return Promise.resolve({});
40
- }
41
- };
42
29
  function useJitsu(opts) {
43
30
  var jitsuInstance = useContext(JitsuContext);
44
31
  if (opts !== null && opts !== void 0 && opts.disabled) {
@@ -53,7 +40,7 @@ function useJitsu(opts) {
53
40
  };
54
41
  } else {
55
42
  var msg = "Before calling useJitsu() hook, please wrap your component into <JitsuProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react";
56
- console.error("%c" + msg, "color: red; font-size: 16px; font-weight: bold;");
43
+ console.error("%c" + msg, "color: red; font-weight: bold;");
57
44
  return {
58
45
  analytics: emptyAnalytics
59
46
  };
@@ -61,14 +48,9 @@ function useJitsu(opts) {
61
48
  } else if (opts !== null && opts !== void 0 && opts.host || opts !== null && opts !== void 0 && opts.echoEvents) {
62
49
  throw new Error("Jitsu client already set up with <JitsuProvider /> and cannot be overridden. Read more in http://jitsu.com/docs/sending-data/js-sdk/react");
63
50
  } else if (jitsuInstance.analytics) {
64
- return {
65
- analytics: jitsuInstance.analytics
66
- };
51
+ return jitsuInstance;
67
52
  } else {
68
- var analytics = jitsuInstance.initOptions.disabled ? emptyAnalytics : jitsuAnalytics(jitsuInstance.initOptions);
69
- return {
70
- analytics: analytics
71
- };
53
+ throw new Error("<JitsuProvider /> is not initialized with undefined analytics instance");
72
54
  }
73
55
  }
74
56
 
@@ -1 +1 @@
1
- {"version":3,"file":"index.modern.js","sources":["../src/JitsuContext.tsx","../src/JitsuProvider.tsx","../src/useJitsu.ts"],"sourcesContent":["import { createContext } from \"react\";\nimport { AnalyticsInterface } from \"@jitsu/js\";\nimport { ExtendedJitsuOptions } from \"./useJitsu\";\n\n/**\n * An instance if Jitsu that is kept in the context. It's one of:\n * - AnalyticsInterface - for CSR, if Jitsu is initialized in browser\n * - ExtendedJitsuOptions - for SSR, if provider can't access context\n */\nexport type JitsuInstance =\n | { analytics: AnalyticsInterface; initOptions?: never }\n | { analytics?: never; initOptions: ExtendedJitsuOptions };\n\nconst JitsuContext = createContext<JitsuInstance | null>(null);\n\nexport default JitsuContext;\n","import * as React from \"react\";\nimport { PropsWithChildren } from \"react\";\nimport JitsuContext from \"./JitsuContext\";\nimport { jitsuAnalytics, isInBrowser } from \"@jitsu/js\";\nimport { ExtendedJitsuOptions } from \"./useJitsu\";\n\nconst JitsuProvider: React.FC<PropsWithChildren<{ options: ExtendedJitsuOptions }>> = props => {\n if (props.options.disabled) {\n return <React.Fragment>{props.children}</React.Fragment>;\n }\n if (!props.options.host) {\n const msg = \"<JitsuProvider />. Jitsu host is not defined. Jitsu will not be initialized\";\n console.error(`%c${msg}`, \"color: red; font-weight: bold;\");\n console.error(msg);\n return <React.Fragment>{props.children}</React.Fragment>;\n }\n return (\n <JitsuContext.Provider\n value={isInBrowser() ? { analytics: jitsuAnalytics(props.options) } : { initOptions: props.options }}\n >\n {props.children}\n </JitsuContext.Provider>\n );\n};\n\nexport default JitsuProvider;\n","import { useContext } from \"react\";\nimport JitsuContext from \"./JitsuContext\";\n\nimport { AnalyticsInterface, jitsuAnalytics, JitsuOptions } from \"@jitsu/js\";\n\nexport interface BeforeEffect {\n effect: (analytics: AnalyticsInterface) => any;\n deps: any[];\n}\n\nexport type ExtendedJitsuOptions =\n | (Omit<JitsuOptions, \"host\"> & { host: string } & {\n disabled?: false | undefined;\n echoEvents?: boolean;\n })\n | { disabled: true };\n\nconst emptyAnalytics = {\n track: () => Promise.resolve(),\n page: () => Promise.resolve(),\n user: () => ({}),\n identify: () => Promise.resolve({}),\n reset: () => Promise.resolve({}),\n};\n\n/**\n * See for details http://jitsu.com/docs/sending-data/js-sdk/react\n */\nfunction useJitsu(opts?: ExtendedJitsuOptions): { analytics: AnalyticsInterface } {\n let jitsuInstance = useContext(JitsuContext);\n if (opts?.disabled) {\n return { analytics: emptyAnalytics };\n }\n //useJitsu() isn't wrapped into JitsuProvider. Return new instance of Jitsu provider if config is provided\n if (!jitsuInstance) {\n if (opts?.host || opts?.echoEvents) {\n return { analytics: jitsuAnalytics(opts) };\n } else {\n const msg =\n \"Before calling useJitsu() hook, please wrap your component into <JitsuProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react\";\n console.error(`%c${msg}`, \"color: red; font-size: 16px; font-weight: bold;\");\n return { analytics: emptyAnalytics };\n }\n } else if (opts?.host || opts?.echoEvents) {\n throw new Error(\n \"Jitsu client already set up with <JitsuProvider /> and cannot be overridden. Read more in http://jitsu.com/docs/sending-data/js-sdk/react\"\n );\n //Jitsu analytics is initialized inside provider\n } else if (jitsuInstance.analytics) {\n return { analytics: jitsuInstance.analytics };\n } else {\n const analytics = jitsuInstance.initOptions.disabled ? emptyAnalytics : jitsuAnalytics(jitsuInstance.initOptions);\n return { analytics };\n }\n}\n\nexport default useJitsu;\nexport { AnalyticsInterface, JitsuOptions };\n"],"names":["JitsuContext","createContext","JitsuProvider","props","options","disabled","React","children","host","msg","console","error","Provider","value","isInBrowser","analytics","jitsuAnalytics","initOptions","emptyAnalytics","track","Promise","resolve","page","user","identify","reset","useJitsu","opts","jitsuInstance","useContext","echoEvents","Error"],"mappings":";;;AAaA,IAAMA,YAAY,GAAGC,aAAa,CAAuB,IAAI,CAAC;;ACP9D,IAAMC,aAAa,GAAmE,SAAhFA,aAAa,CAAmEC,KAAK;EACzF,IAAIA,KAAK,CAACC,OAAO,CAACC,QAAQ,EAAE;IAC1B,OAAOC,cAACA,QAAc,QAAEH,KAAK,CAACI,QAAQ,CAAkB;;EAE1D,IAAI,CAACJ,KAAK,CAACC,OAAO,CAACI,IAAI,EAAE;IACvB,IAAMC,GAAG,GAAG,6EAA6E;IACzFC,OAAO,CAACC,KAAK,QAAMF,GAAG,EAAI,gCAAgC,CAAC;IAC3DC,OAAO,CAACC,KAAK,CAACF,GAAG,CAAC;IAClB,OAAOH,cAACA,QAAc,QAA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A,IAAI,CAAEG,UAAU,EAAE;IACzC,MAAM,IAAIC,KAAK,CACb,2IAA2I,CAC5I;GAEF,MAAM,IAAIH,aAAa,CAACb,SAAS,EAAE;IAClC,OAAO;MAAEA,SAAS,EAAEa,aAAa,CAACb;KAAW;GAC9C,MAAM;IACL,IAAMA,SAAS,GAAGa,aAAa,CAACX,WAAW,CAACZ,QAAQ,GAAGa,cAAc,GAAGF,cAAc,CAACY,aAAa,CAACX,WAAW,CAAC;IACjH,OAAO;MAAEF,SAAS,EAATA;KAAW;;AAExB;;;;"}
1
+ {"version":3,"file":"index.modern.js","sources":["../src/JitsuContext.tsx","../src/JitsuProvider.tsx","../src/useJitsu.ts"],"sourcesContent":["import { createContext } from \"react\";\nimport { AnalyticsInterface } from \"@jitsu/js\";\n\nexport type JitsuInstance = { analytics: AnalyticsInterface };\n\nconst JitsuContext = createContext<JitsuInstance | null>(null);\n\nexport default JitsuContext;\n","import * as React from \"react\";\nimport { PropsWithChildren, useMemo } from \"react\";\nimport JitsuContext, { JitsuInstance } from \"./JitsuContext\";\nimport { emptyAnalytics, jitsuAnalytics } from \"@jitsu/js\";\nimport { ExtendedJitsuOptions } from \"./useJitsu\";\n\nconst JitsuProvider: React.FC<PropsWithChildren<{ options: ExtendedJitsuOptions }>> = props => {\n const instance: JitsuInstance = useMemo(() => {\n if (props.options.disabled) {\n return { analytics: emptyAnalytics };\n } else if (!props.options.host) {\n const msg = \"<JitsuProvider />. Jitsu host is not defined. Jitsu will not be initialized\";\n console.error(`%c${msg}`, \"color: red; font-weight: bold;\");\n return { analytics: emptyAnalytics };\n } else {\n return { analytics: jitsuAnalytics(props.options) };\n }\n }, [props.options.disabled, props.options.host]);\n return <JitsuContext.Provider value={instance}>{props.children}</JitsuContext.Provider>;\n};\n\nexport default JitsuProvider;\n","import { useContext } from \"react\";\nimport JitsuContext from \"./JitsuContext\";\n\nimport { AnalyticsInterface, jitsuAnalytics, JitsuOptions, emptyAnalytics } from \"@jitsu/js\";\n\nexport interface BeforeEffect {\n effect: (analytics: AnalyticsInterface) => any;\n deps: any[];\n}\n\nexport type ExtendedJitsuOptions =\n | (Omit<JitsuOptions, \"host\"> & { host: string } & {\n disabled?: false | undefined;\n echoEvents?: boolean;\n })\n | { disabled: true; host?: undefined };\n\n/**\n * See for details http://jitsu.com/docs/sending-data/js-sdk/react\n */\nfunction useJitsu(opts?: ExtendedJitsuOptions): { analytics: AnalyticsInterface } {\n let jitsuInstance = useContext(JitsuContext);\n if (opts?.disabled) {\n return { analytics: emptyAnalytics };\n }\n if (!jitsuInstance) {\n if (opts?.host || opts?.echoEvents) {\n return { analytics: jitsuAnalytics(opts) };\n } else {\n const msg =\n \"Before calling useJitsu() hook, please wrap your component into <JitsuProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react\";\n console.error(`%c${msg}`, \"color: red; font-weight: bold;\");\n return { analytics: emptyAnalytics };\n }\n } else if (opts?.host || opts?.echoEvents) {\n throw new Error(\n \"Jitsu client already set up with <JitsuProvider /> and cannot be overridden. Read more in http://jitsu.com/docs/sending-data/js-sdk/react\"\n );\n //Jitsu analytics is initialized inside provider\n } else if (jitsuInstance.analytics) {\n return jitsuInstance;\n } else {\n throw new Error(`<JitsuProvider /> is not initialized with undefined analytics instance`);\n }\n}\n\nexport default useJitsu;\nexport { AnalyticsInterface, JitsuOptions };\n"],"names":["JitsuContext","createContext","JitsuProvider","props","instance","useMemo","options","disabled","analytics","emptyAnalytics","host","msg","console","error","jitsuAnalytics","React","Provider","value","children","useJitsu","opts","jitsuInstance","useContext","echoEvents","Error"],"mappings":";;;AAKA,IAAMA,YAAY,GAAGC,aAAa,CAAuB,IAAI,CAAC;;ACC9D,IAAMC,aAAa,GAAmE,SAAhFA,aAAaA,CAAmEC,KAAK;EACzF,IAAMC,QAAQ,GAAkBC,OAAO,CAAC;IACtC,IAAIF,KAAK,CAACG,OAAO,CAACC,QAAQ,EAAE;MAC1B,OAAO;QAAEC,SAAS,EAAEC;OAAgB;KACrC,MAA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IAClC,OAAOa,aAAa;GACrB,MAAM;IACL,MAAM,IAAIG,KAAK,0EAA0E;;AAE7F;;;;"}
@@ -10,6 +10,7 @@ export declare type ExtendedJitsuOptions = (Omit<JitsuOptions, "host"> & {
10
10
  echoEvents?: boolean;
11
11
  }) | {
12
12
  disabled: true;
13
+ host?: undefined;
13
14
  };
14
15
  /**
15
16
  * See for details http://jitsu.com/docs/sending-data/js-sdk/react
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jitsu/jitsu-react",
3
- "version": "1.0.0-canary-20230219230011",
3
+ "version": "1.1.0-canary.14.20230602211353",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -11,20 +11,16 @@
11
11
  "node": ">=10"
12
12
  },
13
13
  "dependencies": {
14
- "@jitsu/js": "1.0.0-canary-20230219230011"
14
+ "@jitsu/js": "1.1.0-canary.14.20230602211353"
15
15
  },
16
16
  "peerDependencies": {
17
17
  "react": "15.x || 16.x || 17.x || 18.x",
18
18
  "@types/react": "15.x || 16.x || 17.x || 18.x",
19
- "react-router-dom": "5.x || 6.x",
20
- "next": "12.x || 13.x"
19
+ "react-router-dom": "5.x || 6.x"
21
20
  },
22
21
  "peerDependenciesMeta": {
23
22
  "react-router-dom": {
24
23
  "optional": true
25
- },
26
- "next": {
27
- "optional": true
28
24
  }
29
25
  },
30
26
  "devDependencies": {
@@ -35,8 +31,8 @@
35
31
  "@types/jest": "^29.2.3",
36
32
  "@types/node": "^18.11.9",
37
33
  "microbundle-crl": "^0.13.11",
38
- "typescript": "^4.9.3",
39
- "@jitsu/js": "1.0.0-canary-20230219230011"
34
+ "typescript": "^4.9.5",
35
+ "@babel/plugin-transform-regenerator": "^7.20.5"
40
36
  },
41
37
  "files": [
42
38
  "dist"