@visitorquery/react 0.0.16 → 0.0.19

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
@@ -14,20 +14,5 @@ This plugin requires a valid api key (Public api key) and that you provision a s
14
14
  please visit our website
15
15
  at [visitorquery.com](https://visitorquery.com) and pick a suitable plan.
16
16
 
17
- Example usage on our own website (nextjs), on the checkout page, before openning the payment dialog:
18
-
19
- ```typescript
20
- import {useVisitorQuery} from "@visitorquery/react";
21
-
22
- export default function LayoutComponent() {
23
- // visitorquery should be loaded and your visitor assessed
24
- // to check the result, issue a GET request from your backend
25
- const visitorQuery = useVisitorQuery({
26
- // use your project's !!PUBLIC!! api key here
27
- ApiKey : process.env.NEXT_PUBLIC_VISITOR_QUERY_API_KEY!,
28
- SessionId: "unique-session-id-to-later-identify-the-visitor",
29
- });
30
- }
31
- ```
32
-
33
- For more info and up to date documentation, please visit our [official page](https://visitorquery.com/docs).
17
+ For more info and up to date documentation, please visit
18
+ our [official docs](https://docs.visitorquery.com/integrations/react) for this plugin.
package/dist/index.js CHANGED
@@ -1,80 +1,81 @@
1
1
  import { jsx as f } from "react/jsx-runtime";
2
- import * as c from "react";
2
+ import * as l from "react";
3
3
  import { createContext as v, useState as y, useEffect as E, useMemo as S, useContext as w } from "react";
4
- function V(t, u = {}) {
5
- const [i, r] = c.useState("loading"), d = c.useRef(u);
6
- return c.useEffect(() => {
4
+ function V(t, i = {}) {
5
+ const [a, r] = l.useState("loading"), d = l.useRef(i);
6
+ return l.useEffect(() => {
7
7
  let e = document.querySelector(`script[src="${t}"]`);
8
- const n = e == null ? void 0 : e.getAttribute("data-status");
9
- if (n) {
10
- r(n);
8
+ const o = e == null ? void 0 : e.getAttribute("data-status");
9
+ if (o) {
10
+ r(o);
11
11
  return;
12
12
  }
13
13
  if (e === null) {
14
14
  e = document.createElement("script"), e.src = t, e.async = !0, e.setAttribute("data-status", "loading"), document.body.appendChild(e);
15
- const o = () => {
16
- e == null || e.setAttribute("data-status", "ready"), r("ready"), s();
17
- }, a = () => {
18
- e == null || e.setAttribute("data-status", "error"), r("error"), s();
19
- }, s = () => {
20
- e == null || e.removeEventListener("load", o), e == null || e.removeEventListener("error", a);
15
+ const s = () => {
16
+ e == null || e.setAttribute("data-status", "ready"), r("ready"), u();
17
+ }, n = () => {
18
+ e == null || e.setAttribute("data-status", "error"), r("error"), u();
19
+ }, u = () => {
20
+ e == null || e.removeEventListener("load", s), e == null || e.removeEventListener("error", n);
21
21
  };
22
- e.addEventListener("load", o), e.addEventListener("error", a);
23
- const m = d.current.removeOnUnmount;
22
+ e.addEventListener("load", s), e.addEventListener("error", n);
23
+ const c = d.current.removeOnUnmount;
24
24
  return () => {
25
- m === !0 && (e == null || e.remove(), s());
25
+ c === !0 && (e == null || e.remove(), u());
26
26
  };
27
27
  } else
28
28
  r("unknown");
29
- }, [t]), i;
29
+ }, [t]), a;
30
30
  }
31
- const l = v(null);
31
+ const m = v(null);
32
32
  function L({
33
33
  apiKey: t,
34
- sessionId: u,
35
- endpoint: i,
36
- children: r
34
+ sessionId: i,
35
+ endpoint: a,
36
+ children: r,
37
+ trigger: d
37
38
  }) {
38
- const d = "main.check.visitorquery.com", e = V("https://cdn.visitorquery.com/visitorquery.js", {
39
+ const e = "main.check.visitorquery.com", o = V("https://cdn.visitorquery.com/visitorquery.js", {
39
40
  removeOnUnmount: !1
40
- }), [n, o] = y({
41
+ }), [s, n] = y({
41
42
  started: !1,
42
43
  ended: !1
43
44
  });
44
45
  E(() => {
45
46
  typeof window.VisitorQuery < "u" && window.VisitorQuery.run({
46
47
  ApiKey: t,
47
- Endpoint: i || d,
48
- SessionId: u,
48
+ Endpoint: a || e,
49
+ SessionId: i,
49
50
  onOpen: () => {
50
- o({
51
+ n({
51
52
  started: !0,
52
53
  ended: !1
53
54
  });
54
55
  },
55
56
  onClose: () => {
56
- o({
57
+ n({
57
58
  started: !0,
58
59
  ended: !0
59
60
  });
60
61
  },
61
- onError: (s) => {
62
- o({
62
+ onError: (c) => {
63
+ n({
63
64
  started: !0,
64
65
  ended: !0,
65
66
  errored: !0
66
- }), console.error(s);
67
+ }), console.error(c);
67
68
  }
68
69
  });
69
- }, [t, i, u, e]);
70
- const a = S(() => ({
71
- ...n,
72
- isLoading: e === "loading"
73
- }), [n, e]);
74
- return /* @__PURE__ */ f(l.Provider, { value: a, children: r });
70
+ }, [t, a, i, o, d]);
71
+ const u = S(() => ({
72
+ ...s,
73
+ isLoading: o === "loading"
74
+ }), [s, o]);
75
+ return /* @__PURE__ */ f(m.Provider, { value: u, children: r });
75
76
  }
76
77
  function Q() {
77
- const t = w(l);
78
+ const t = w(m);
78
79
  if (!t)
79
80
  throw new Error("useVisitorQuery must be used within a VisitorQueryProvider");
80
81
  return t;
@@ -12,12 +12,26 @@ type VisitorQueryState = {
12
12
  type VisitorQueryContextType = VisitorQueryState & {
13
13
  isLoading: boolean;
14
14
  };
15
- type VisitorQueryProviderProps = {
15
+ export type VisitorQueryProviderProps = {
16
16
  apiKey: string;
17
+ /**
18
+ * A unique identifier for the session. This is used in order to later
19
+ * pull results from the VisitorQuery API.
20
+ */
17
21
  sessionId: string;
22
+ /**
23
+ * The endpoint to use for the VisitorQuery script. Useful in cases when
24
+ * you are using custom/vanity domains
25
+ */
18
26
  endpoint?: string;
19
27
  children: React.ReactNode;
28
+ /**
29
+ * A trigger that can be used to re-run the VisitorQuery script.
30
+ * This could be a route change or any other event that you want to use to
31
+ * re-initialize the VisitorQuery script.
32
+ */
33
+ trigger?: string;
20
34
  };
21
- export declare function VisitorQueryProvider({ apiKey, sessionId, endpoint, children }: VisitorQueryProviderProps): import("react/jsx-runtime").JSX.Element;
35
+ export declare function VisitorQueryProvider({ apiKey, sessionId, endpoint, children, trigger }: VisitorQueryProviderProps): import("react/jsx-runtime").JSX.Element;
22
36
  export declare function useVisitorQuery(): VisitorQueryContextType;
23
37
  export {};
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.16",
2
+ "version": "0.0.19",
3
3
  "name": "@visitorquery/react",
4
4
  "module": "./dist/index.js",
5
5
  "type": "module",