@jitsu/jitsu-react 0.0.0-alpha.94

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 ADDED
@@ -0,0 +1,123 @@
1
+ # jitsu-react
2
+
3
+ >
4
+
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)
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install --save @jitsu/jitsu-react
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ 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
+
29
+ Then use `useJitsu` hook in components where you want to track events.
30
+ ```tsx
31
+ import * as React from "react";
32
+ import { useJitsu } from "@jitsu/jitsu-react";
33
+
34
+ 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
+ );
44
+ }
45
+ ```
46
+
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
+ ```
71
+
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
+ ```tsx
75
+ import * as React from "react";
76
+ import { useRouter } from "next/router";
77
+ import { useJitsu } from "@jitsu/jitsu-react";
78
+
79
+ export default function App() {
80
+ useJitsu({
81
+ // Enable auto page tracking with Next.JS router
82
+ autoPageTracking: {
83
+ nextjsRouter: useRouter
84
+ },
85
+ });
86
+
87
+ return (
88
+ <div>
89
+ ...
90
+ </div>
91
+ );
92
+ }
93
+ ```
94
+
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
+ ```
@@ -0,0 +1,4 @@
1
+ /// <reference types="react" />
2
+ import { AnalyticsInterface } from "@jitsu/types/analytics";
3
+ declare const JitsuContext: import("react").Context<AnalyticsInterface | null>;
4
+ export default JitsuContext;
@@ -0,0 +1,7 @@
1
+ import * as React from "react";
2
+ import { PropsWithChildren } from "react";
3
+ import { JitsuOptions } from "@jitsu/js";
4
+ declare const JitsuProvider: React.FC<PropsWithChildren<{
5
+ options: JitsuOptions;
6
+ }>>;
7
+ export default JitsuProvider;
@@ -0,0 +1,4 @@
1
+ import { JitsuOptions } from "@jitsu/js";
2
+ import { AnalyticsInterface } from "@jitsu/types/analytics";
3
+ declare function createClient(params: JitsuOptions): AnalyticsInterface;
4
+ export default createClient;
@@ -0,0 +1,3 @@
1
+ export { default as JitsuContext } from "./JitsuContext";
2
+ export { default as JitsuProvider } from "./JitsuProvider";
3
+ export { default as useJitsu } from "./useJitsu";
@@ -0,0 +1,47 @@
1
+ import { createContext, createElement, useContext, useEffect } from 'react';
2
+ import { jitsuAnalytics } from '@jitsu/js';
3
+
4
+ var JitsuContext = createContext(null);
5
+
6
+ var JitsuProvider = function JitsuProvider(props) {
7
+ var client = jitsuAnalytics(props.options);
8
+ return createElement(JitsuContext.Provider, {
9
+ value: client
10
+ }, props.children);
11
+ };
12
+
13
+ function useJitsu(opts) {
14
+ var _opts$autoPageTrackin;
15
+ var cl = useContext(JitsuContext);
16
+ if (!cl) {
17
+ if (opts !== null && opts !== void 0 && opts.host) {
18
+ cl = jitsuAnalytics(opts);
19
+ } else {
20
+ throw new Error("Before calling useJitsu() hook, please wrap your component into <JitsuProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react");
21
+ }
22
+ } else if (opts !== null && opts !== void 0 && opts.host) {
23
+ 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");
24
+ }
25
+ var client = cl;
26
+ var reactLocationHook = opts !== null && opts !== void 0 && opts.autoPageTracking && opts.autoPageTracking.reactRouter ? opts.autoPageTracking.reactRouter() : null;
27
+ var before = opts !== null && opts !== void 0 && opts.autoPageTracking && opts.autoPageTracking.before ? opts.autoPageTracking.before : null;
28
+ var nextjsLocationHook = opts !== null && opts !== void 0 && opts.autoPageTracking && opts.autoPageTracking.nextjsRouter ? opts.autoPageTracking.nextjsRouter() : null;
29
+ useEffect(function () {
30
+ if (before) {
31
+ before(client);
32
+ }
33
+ }, (opts === null || opts === void 0 ? void 0 : (_opts$autoPageTrackin = opts.autoPageTracking) === null || _opts$autoPageTrackin === void 0 ? void 0 : _opts$autoPageTrackin.beforeDeps) || []);
34
+ useEffect(function () {
35
+ if (reactLocationHook) {
36
+ client.page();
37
+ } else if (nextjsLocationHook) {
38
+ client.page();
39
+ }
40
+ }, [reactLocationHook, nextjsLocationHook]);
41
+ return {
42
+ analytics: client
43
+ };
44
+ }
45
+
46
+ export { JitsuContext, JitsuProvider, useJitsu };
47
+ //# sourceMappingURL=index.es.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.es.js","sources":["../src/JitsuContext.tsx","../src/JitsuProvider.tsx","../src/useJitsu.ts"],"sourcesContent":["import { createContext } from 'react'\nimport { AnalyticsInterface } from \"@jitsu/types/analytics\";\n\n\nconst JitsuContext = createContext<AnalyticsInterface | null>(null)\n\nexport default JitsuContext","import * as React from \"react\";\nimport { PropsWithChildren } from \"react\";\nimport JitsuContext from \"./JitsuContext\";\nimport { jitsuAnalytics, JitsuOptions } from \"@jitsu/js\";\n\nconst JitsuProvider: React.FC<PropsWithChildren<{ options: JitsuOptions }>> = function (props) {\n const client = jitsuAnalytics(props.options);\n return <JitsuContext.Provider value={client}>{props.children}</JitsuContext.Provider>;\n};\n\nexport default JitsuProvider;\n","import { useContext, useEffect } from \"react\";\nimport JitsuContext from \"./JitsuContext\";\nimport { AnalyticsInterface } from \"@jitsu/types/analytics\";\nimport type { useLocation } from \"react-router-dom\";\nimport type { useRouter } from \"next/router\";\n\nimport { Union } from \"ts-toolbelt\";\nimport { jitsuAnalytics, JitsuOptions } from \"@jitsu/js\";\n\ninterface ReactRouterOptions {\n reactRouter: typeof useLocation;\n}\n\ninterface NextJsRouterOptions {\n nextjsRouter: typeof useRouter;\n}\n\ninterface AutoPageTrackingBefore {\n before: (analytics: AnalyticsInterface) => any;\n beforeDeps: any[];\n}\ntype RouterOptions = Union.Strict<ReactRouterOptions | NextJsRouterOptions>;\n\ntype ExtendedJitsuOptions = JitsuOptions & {\n autoPageTracking?: Union.Strict<(RouterOptions & AutoPageTrackingBefore) | RouterOptions>;\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 cl = useContext(JitsuContext);\n if (!cl) {\n if (opts?.host) {\n cl = jitsuAnalytics(opts);\n } else {\n throw new Error(\n \"Before calling useJitsu() hook, please wrap your component into <JitsuProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react\"\n );\n }\n } else if (opts?.host) {\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 }\n const client = cl;\n\n const reactLocationHook =\n opts?.autoPageTracking && opts.autoPageTracking.reactRouter ? opts.autoPageTracking.reactRouter() : null;\n const before = opts?.autoPageTracking && opts.autoPageTracking.before ? opts.autoPageTracking.before : null;\n const nextjsLocationHook =\n opts?.autoPageTracking && opts.autoPageTracking.nextjsRouter ? opts.autoPageTracking.nextjsRouter() : null;\n useEffect(() => {\n if (before) {\n before(client);\n }\n }, opts?.autoPageTracking?.beforeDeps || []);\n\n useEffect(() => {\n if (reactLocationHook) {\n client.page();\n } else if (nextjsLocationHook) {\n client.page();\n }\n }, [reactLocationHook, nextjsLocationHook]);\n\n return { analytics: client };\n}\n\nexport default useJitsu;\n"],"names":["JitsuContext","createContext","JitsuProvider","props","client","jitsuAnalytics","options","React","Provider","value","children","useJitsu","opts","cl","useContext","host","Error","reactLocationHook","autoPageTracking","reactRouter","before","nextjsLocationHook","nextjsRouter","useEffect","beforeDeps","page","analytics"],"mappings":";;;AAIA,IAAMA,YAAY,GAAGC,aAAa,CAA4B,IAAI,CAAC;;ACCnE,IAAMC,aAAa,GAA2D,SAAxEA,aAAa,CAAqEC,KAAK;EAC3F,IAAMC,MAAM,GAAGC,cAAc,CAACF,KAAK,CAACG,OAAO,CAAC;EAC5C,OAAOC,cAACP,YAAY,CAACQ,QAAQ;IAACC,KAAK,EAAEL;KAASD,KAAK,CAACO,QAAQ,CAAyB;AACvF,CAAC;;ACsBD,SAASC,QAAQ,CAACC,IAA2B;;EAC3C,IAAIC,EAAE,GAAGC,UAAU,CAACd,YAAY,CAAC;EACjC,IAAI,CAACa,EAAE,EAAE;IACP,IAAID,IAAI,aAAJA,IAAI,eAAJA,IAAI,CAAE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;;;;"}
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ var React = require('react');
2
+ var js = require('@jitsu/js');
3
+
4
+ var JitsuContext = React.createContext(null);
5
+
6
+ var JitsuProvider = function JitsuProvider(props) {
7
+ var client = js.jitsuAnalytics(props.options);
8
+ return React.createElement(JitsuContext.Provider, {
9
+ value: client
10
+ }, props.children);
11
+ };
12
+
13
+ function useJitsu(opts) {
14
+ var _opts$autoPageTrackin;
15
+ var cl = React.useContext(JitsuContext);
16
+ if (!cl) {
17
+ if (opts !== null && opts !== void 0 && opts.host) {
18
+ cl = js.jitsuAnalytics(opts);
19
+ } else {
20
+ throw new Error("Before calling useJitsu() hook, please wrap your component into <JitsuProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react");
21
+ }
22
+ } else if (opts !== null && opts !== void 0 && opts.host) {
23
+ 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");
24
+ }
25
+ var client = cl;
26
+ var reactLocationHook = opts !== null && opts !== void 0 && opts.autoPageTracking && opts.autoPageTracking.reactRouter ? opts.autoPageTracking.reactRouter() : null;
27
+ var before = opts !== null && opts !== void 0 && opts.autoPageTracking && opts.autoPageTracking.before ? opts.autoPageTracking.before : null;
28
+ var nextjsLocationHook = opts !== null && opts !== void 0 && opts.autoPageTracking && opts.autoPageTracking.nextjsRouter ? opts.autoPageTracking.nextjsRouter() : null;
29
+ React.useEffect(function () {
30
+ if (before) {
31
+ before(client);
32
+ }
33
+ }, (opts === null || opts === void 0 ? void 0 : (_opts$autoPageTrackin = opts.autoPageTracking) === null || _opts$autoPageTrackin === void 0 ? void 0 : _opts$autoPageTrackin.beforeDeps) || []);
34
+ React.useEffect(function () {
35
+ if (reactLocationHook) {
36
+ client.page();
37
+ } else if (nextjsLocationHook) {
38
+ client.page();
39
+ }
40
+ }, [reactLocationHook, nextjsLocationHook]);
41
+ return {
42
+ analytics: client
43
+ };
44
+ }
45
+
46
+ exports.JitsuContext = JitsuContext;
47
+ exports.JitsuProvider = JitsuProvider;
48
+ exports.useJitsu = useJitsu;
49
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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/types/analytics\";\n\nconst JitsuContext = createContext<AnalyticsInterface | null>(null);\n\nexport default JitsuContext;\n","import * as React from \"react\";\nimport { PropsWithChildren } from \"react\";\nimport JitsuContext from \"./JitsuContext\";\nimport { jitsuAnalytics, JitsuOptions } from \"@jitsu/js\";\n\nconst JitsuProvider: React.FC<PropsWithChildren<{ options: JitsuOptions }>> = function (props) {\n const client = jitsuAnalytics(props.options);\n return <JitsuContext.Provider value={client}>{props.children}</JitsuContext.Provider>;\n};\n\nexport default JitsuProvider;\n","import { useContext, useEffect } from \"react\";\nimport JitsuContext from \"./JitsuContext\";\nimport { AnalyticsInterface } from \"@jitsu/types/analytics\";\nimport type { useLocation } from \"react-router-dom\";\nimport type { useRouter } from \"next/router\";\n\nimport { Union } from \"ts-toolbelt\";\nimport { jitsuAnalytics, JitsuOptions } from \"@jitsu/js\";\n\ninterface ReactRouterOptions {\n reactRouter: typeof useLocation;\n}\n\ninterface NextJsRouterOptions {\n nextjsRouter: typeof useRouter;\n}\n\ninterface AutoPageTrackingBefore {\n before: (analytics: AnalyticsInterface) => any;\n beforeDeps: any[];\n}\ntype RouterOptions = Union.Strict<ReactRouterOptions | NextJsRouterOptions>;\n\ntype ExtendedJitsuOptions = JitsuOptions & {\n autoPageTracking?: Union.Strict<(RouterOptions & AutoPageTrackingBefore) | RouterOptions>;\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 cl = useContext(JitsuContext);\n if (!cl) {\n if (opts?.host) {\n cl = jitsuAnalytics(opts);\n } else {\n throw new Error(\n \"Before calling useJitsu() hook, please wrap your component into <JitsuProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react\"\n );\n }\n } else if (opts?.host) {\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 }\n const client = cl;\n\n const reactLocationHook =\n opts?.autoPageTracking && opts.autoPageTracking.reactRouter ? opts.autoPageTracking.reactRouter() : null;\n const before = opts?.autoPageTracking && opts.autoPageTracking.before ? opts.autoPageTracking.before : null;\n const nextjsLocationHook =\n opts?.autoPageTracking && opts.autoPageTracking.nextjsRouter ? opts.autoPageTracking.nextjsRouter() : null;\n useEffect(() => {\n if (before) {\n before(client);\n }\n }, opts?.autoPageTracking?.beforeDeps || []);\n\n useEffect(() => {\n if (reactLocationHook) {\n client.page();\n } else if (nextjsLocationHook) {\n client.page();\n }\n }, [reactLocationHook, nextjsLocationHook]);\n\n return { analytics: client };\n}\n\nexport default useJitsu;\n"],"names":["JitsuContext","createContext","JitsuProvider","props","client","jitsuAnalytics","options","React","Provider","value","children","useJitsu","opts","cl","useContext","host","Error","reactLocationHook","autoPageTracking","reactRouter","before","nextjsLocationHook","nextjsRouter","useEffect","beforeDeps","page","analytics"],"mappings":";;;AAGA,IAAMA,YAAY,GAAGC,mBAAa,CAA4B,IAAI,CAAC;;ACEnE,IAAMC,aAAa,GAA2D,SAAxEA,aAAa,CAAqEC,KAAK;EAC3F,IAAMC,MAAM,GAAGC,iBAAc,CAACF,KAAK,CAACG,OAAO,CAAC;EAC5C,OAAOC,oBAACP,YAAY,CAACQ,QAAQ;IAACC,KAAK,EAAEL;KAASD,KAAK,CAACO,QAAQ,CAAyB;AACvF,CAAC;;ACsBD,SAASC,QAAQ,CAACC,IAA2B;;EAC3C,IAAIC,EAAE,GAAGC,gBAAU,CAACd,YAAY,CAAC;EACjC,IAAI,CAACa,EAAE,EAAE;IACP,IAAID,IAAI,aAAJ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SAAS,EAAEtB;GAAQ;AAC9B;;;;;;"}
@@ -0,0 +1,47 @@
1
+ import { createContext, createElement, useContext, useEffect } from 'react';
2
+ import { jitsuAnalytics } from '@jitsu/js';
3
+
4
+ var JitsuContext = createContext(null);
5
+
6
+ var JitsuProvider = function JitsuProvider(props) {
7
+ var client = jitsuAnalytics(props.options);
8
+ return createElement(JitsuContext.Provider, {
9
+ value: client
10
+ }, props.children);
11
+ };
12
+
13
+ function useJitsu(opts) {
14
+ var _opts$autoPageTrackin;
15
+ var cl = useContext(JitsuContext);
16
+ if (!cl) {
17
+ if (opts !== null && opts !== void 0 && opts.host) {
18
+ cl = jitsuAnalytics(opts);
19
+ } else {
20
+ throw new Error("Before calling useJitsu() hook, please wrap your component into <JitsuProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react");
21
+ }
22
+ } else if (opts !== null && opts !== void 0 && opts.host) {
23
+ 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");
24
+ }
25
+ var client = cl;
26
+ var reactLocationHook = opts !== null && opts !== void 0 && opts.autoPageTracking && opts.autoPageTracking.reactRouter ? opts.autoPageTracking.reactRouter() : null;
27
+ var before = opts !== null && opts !== void 0 && opts.autoPageTracking && opts.autoPageTracking.before ? opts.autoPageTracking.before : null;
28
+ var nextjsLocationHook = opts !== null && opts !== void 0 && opts.autoPageTracking && opts.autoPageTracking.nextjsRouter ? opts.autoPageTracking.nextjsRouter() : null;
29
+ useEffect(function () {
30
+ if (before) {
31
+ before(client);
32
+ }
33
+ }, (opts === null || opts === void 0 ? void 0 : (_opts$autoPageTrackin = opts.autoPageTracking) === null || _opts$autoPageTrackin === void 0 ? void 0 : _opts$autoPageTrackin.beforeDeps) || []);
34
+ useEffect(function () {
35
+ if (reactLocationHook) {
36
+ client.page();
37
+ } else if (nextjsLocationHook) {
38
+ client.page();
39
+ }
40
+ }, [reactLocationHook, nextjsLocationHook]);
41
+ return {
42
+ analytics: client
43
+ };
44
+ }
45
+
46
+ export { JitsuContext, JitsuProvider, useJitsu };
47
+ //# sourceMappingURL=index.modern.js.map
@@ -0,0 +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/types/analytics\";\n\nconst JitsuContext = createContext<AnalyticsInterface | null>(null);\n\nexport default JitsuContext;\n","import * as React from \"react\";\nimport { PropsWithChildren } from \"react\";\nimport JitsuContext from \"./JitsuContext\";\nimport { jitsuAnalytics, JitsuOptions } from \"@jitsu/js\";\n\nconst JitsuProvider: React.FC<PropsWithChildren<{ options: JitsuOptions }>> = function (props) {\n const client = jitsuAnalytics(props.options);\n return <JitsuContext.Provider value={client}>{props.children}</JitsuContext.Provider>;\n};\n\nexport default JitsuProvider;\n","import { useContext, useEffect } from \"react\";\nimport JitsuContext from \"./JitsuContext\";\nimport { AnalyticsInterface } from \"@jitsu/types/analytics\";\nimport type { useLocation } from \"react-router-dom\";\nimport type { useRouter } from \"next/router\";\n\nimport { Union } from \"ts-toolbelt\";\nimport { jitsuAnalytics, JitsuOptions } from \"@jitsu/js\";\n\ninterface ReactRouterOptions {\n reactRouter: typeof useLocation;\n}\n\ninterface NextJsRouterOptions {\n nextjsRouter: typeof useRouter;\n}\n\ninterface AutoPageTrackingBefore {\n before: (analytics: AnalyticsInterface) => any;\n beforeDeps: any[];\n}\ntype RouterOptions = Union.Strict<ReactRouterOptions | NextJsRouterOptions>;\n\ntype ExtendedJitsuOptions = JitsuOptions & {\n autoPageTracking?: Union.Strict<(RouterOptions & AutoPageTrackingBefore) | RouterOptions>;\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 cl = useContext(JitsuContext);\n if (!cl) {\n if (opts?.host) {\n cl = jitsuAnalytics(opts);\n } else {\n throw new Error(\n \"Before calling useJitsu() hook, please wrap your component into <JitsuProvider />. Read more in http://jitsu.com/docs/sending-data/js-sdk/react\"\n );\n }\n } else if (opts?.host) {\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 }\n const client = cl;\n\n const reactLocationHook =\n opts?.autoPageTracking && opts.autoPageTracking.reactRouter ? opts.autoPageTracking.reactRouter() : null;\n const before = opts?.autoPageTracking && opts.autoPageTracking.before ? opts.autoPageTracking.before : null;\n const nextjsLocationHook =\n opts?.autoPageTracking && opts.autoPageTracking.nextjsRouter ? opts.autoPageTracking.nextjsRouter() : null;\n useEffect(() => {\n if (before) {\n before(client);\n }\n }, opts?.autoPageTracking?.beforeDeps || []);\n\n useEffect(() => {\n if (reactLocationHook) {\n client.page();\n } else if (nextjsLocationHook) {\n client.page();\n }\n }, [reactLocationHook, nextjsLocationHook]);\n\n return { analytics: client };\n}\n\nexport default useJitsu;\n"],"names":["JitsuContext","createContext","JitsuProvider","props","client","jitsuAnalytics","options","React","Provider","value","children","useJitsu","opts","cl","useContext","host","Error","reactLocationHook","autoPageTracking","reactRouter","before","nextjsLocationHook","nextjsRouter","useEffect","beforeDeps","page","analytics"],"mappings":";;;AAGA,IAAMA,YAAY,GAAGC,aAAa,CAA4B,IAAI,CAAC;;ACEnE,IAAMC,aAAa,GAA2D,SAAxEA,aAAa,CAAqEC,KAAK;EAC3F,IAAMC,MAAM,GAAGC,cAAc,CAACF,KAAK,CAACG,OAAO,CAAC;EAC5C,OAAOC,cAACP,YAAY,CAACQ,QAAQ;IAACC,KAAK,EAAEL;KAASD,KAAK,CAACO,QAAQ,CAAyB;AACvF,CAAC;;ACsBD,SAASC,QAAQ,CAACC,IAA2B;;EAC3C,IAAIC,EAAE,GAAGC,UAAU,CAACd,YAAY,CAAC;EACjC,IAAI,CAACa,EAAE,EAAE;IACP,IAAID,IAAI,aAAJA,IAAI,eAAJA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AAQ;AAC9B;;;;"}
@@ -0,0 +1,26 @@
1
+ import { AnalyticsInterface } from "@jitsu/types/analytics";
2
+ import type { useLocation } from "react-router-dom";
3
+ import type { useRouter } from "next/router";
4
+ import { Union } from "ts-toolbelt";
5
+ import { JitsuOptions } from "@jitsu/js";
6
+ interface ReactRouterOptions {
7
+ reactRouter: typeof useLocation;
8
+ }
9
+ interface NextJsRouterOptions {
10
+ nextjsRouter: typeof useRouter;
11
+ }
12
+ interface AutoPageTrackingBefore {
13
+ before: (analytics: AnalyticsInterface) => any;
14
+ beforeDeps: any[];
15
+ }
16
+ declare type RouterOptions = Union.Strict<ReactRouterOptions | NextJsRouterOptions>;
17
+ declare type ExtendedJitsuOptions = JitsuOptions & {
18
+ autoPageTracking?: Union.Strict<(RouterOptions & AutoPageTrackingBefore) | RouterOptions>;
19
+ };
20
+ /**
21
+ * See for details http://jitsu.com/docs/sending-data/js-sdk/react
22
+ */
23
+ declare function useJitsu(opts?: ExtendedJitsuOptions): {
24
+ analytics: AnalyticsInterface;
25
+ };
26
+ export default useJitsu;
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@jitsu/jitsu-react",
3
+ "version": "0.0.0-alpha.94",
4
+ "description": "",
5
+ "license": "MIT",
6
+ "main": "dist/index.js",
7
+ "module": "dist/index.modern.js",
8
+ "types": "dist/index.d.ts",
9
+ "source": "src/index.ts",
10
+ "engines": {
11
+ "node": ">=10"
12
+ },
13
+ "dependencies": {
14
+ "@jitsu/js": "0.0.0-alpha.94"
15
+ },
16
+ "peerDependencies": {
17
+ "react": "15.x || 16.x || 17.x || 18.x",
18
+ "react-router-dom": "5.x || 6.x",
19
+ "next": "12.x || 13.x"
20
+ },
21
+ "peerDependenciesMeta": {
22
+ "react-router-dom": {
23
+ "optional": true
24
+ },
25
+ "next": {
26
+ "optional": true
27
+ }
28
+ },
29
+ "devDependencies": {
30
+ "ts-toolbelt": "^9.6.0",
31
+ "@testing-library/jest-dom": "^5.16.5",
32
+ "@testing-library/react": "^13.4.0",
33
+ "@testing-library/user-event": "^14.4.3",
34
+ "@types/jest": "^29.2.3",
35
+ "@types/node": "^18.11.9",
36
+ "@types/react": "^18.0.25",
37
+ "next": "13.0.6",
38
+ "@types/react-router-dom": "^5.3.3",
39
+ "microbundle-crl": "^0.13.11",
40
+ "react": "^18.2.0",
41
+ "typescript": "^4.9.3",
42
+ "@jitsu/js": "0.0.0-alpha.94",
43
+ "@jitsu/types": "0.0.0-alpha.94"
44
+ },
45
+ "files": [
46
+ "dist"
47
+ ],
48
+ "scripts": {
49
+ "build": "microbundle-crl --no-compress --format es,cjs",
50
+ "compile": "tsc -p ."
51
+ }
52
+ }