react-event-tracking 1.0.2 → 1.0.3

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,4 +1,5 @@
1
- A convenient React context for tracking analytics events.
1
+ # react-event-tracking [![NPM Version](https://img.shields.io/npm/v/react-event-tracking)](https://www.npmjs.com/package/react-event-tracking)
2
+ A convenient React context for tracking analytics events.
2
3
 
3
4
  ## Features
4
5
 
@@ -9,7 +10,7 @@ A convenient React context for tracking analytics events.
9
10
  ## Installation
10
11
 
11
12
  ```
12
- npm install treact-event-tracking
13
+ npm install react-event-tracking
13
14
  ```
14
15
  ```
15
16
  yarn add react-event-tracking
@@ -55,18 +56,18 @@ const MyButton = () => {
55
56
  };
56
57
  ```
57
58
 
58
- ## Example
59
+ ## Built-in Hooks
59
60
 
60
- ### Page View
61
+ ### useMountEvent
62
+
63
+ Sends an event when the component mounts.
61
64
 
62
65
  ```tsx
63
- export function PageView(props) {
64
- const { sendEvent } = useTracker();
66
+ import { useMountEvent } from 'react-event-tracking';
65
67
 
66
- useEffect(() => {
67
- sendEvent('page_view');
68
- }, []);
68
+ export function DashboardScreen(props) {
69
+ useMountEvent('page_view', { screen: 'dashboard' });
69
70
 
70
- return <>{props.children}</>
71
+ return <div>Dashboard</div>;
71
72
  }
72
- ```
73
+ ```
package/dist/index.cjs CHANGED
@@ -47,6 +47,19 @@ const TrackProvider = ({
47
47
  return /* @__PURE__ */ React__default.createElement(TrackContext.Provider, { value }, children);
48
48
  };
49
49
 
50
+ function useMountEvent(eventName, params) {
51
+ const { sendEvent } = useTracker();
52
+ const counterRef = React.useRef(0);
53
+ React.useEffect(() => {
54
+ if (counterRef.current > 0) {
55
+ return;
56
+ }
57
+ counterRef.current++;
58
+ sendEvent(eventName, params);
59
+ }, []);
60
+ }
61
+
50
62
  exports.TrackProvider = TrackProvider;
51
63
  exports.TrackRoot = TrackRoot;
64
+ exports.useMountEvent = useMountEvent;
52
65
  exports.useTracker = useTracker;
package/dist/index.d.cts CHANGED
@@ -2,9 +2,10 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { PropsWithChildren } from 'react';
3
3
 
4
4
  type EventParams = Record<string, any>;
5
- interface TrackContextValue {
5
+ type TrackContextValue = {
6
6
  sendEvent: (eventName: string, params?: EventParams) => void;
7
- }
7
+ };
8
+
8
9
  declare const useTracker: () => TrackContextValue;
9
10
  declare const TrackRoot: ({ onEvent, children }: PropsWithChildren<{
10
11
  onEvent: (eventName: string, params?: EventParams) => void;
@@ -13,4 +14,6 @@ declare const TrackProvider: ({ params, children }: PropsWithChildren<{
13
14
  params: EventParams;
14
15
  }>) => react_jsx_runtime.JSX.Element;
15
16
 
16
- export { TrackProvider, TrackRoot, useTracker };
17
+ declare function useMountEvent(eventName: string, params?: EventParams): void;
18
+
19
+ export { TrackProvider, TrackRoot, useMountEvent, useTracker };
package/dist/index.d.mts CHANGED
@@ -2,9 +2,10 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { PropsWithChildren } from 'react';
3
3
 
4
4
  type EventParams = Record<string, any>;
5
- interface TrackContextValue {
5
+ type TrackContextValue = {
6
6
  sendEvent: (eventName: string, params?: EventParams) => void;
7
- }
7
+ };
8
+
8
9
  declare const useTracker: () => TrackContextValue;
9
10
  declare const TrackRoot: ({ onEvent, children }: PropsWithChildren<{
10
11
  onEvent: (eventName: string, params?: EventParams) => void;
@@ -13,4 +14,6 @@ declare const TrackProvider: ({ params, children }: PropsWithChildren<{
13
14
  params: EventParams;
14
15
  }>) => react_jsx_runtime.JSX.Element;
15
16
 
16
- export { TrackProvider, TrackRoot, useTracker };
17
+ declare function useMountEvent(eventName: string, params?: EventParams): void;
18
+
19
+ export { TrackProvider, TrackRoot, useMountEvent, useTracker };
package/dist/index.d.ts CHANGED
@@ -2,9 +2,10 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { PropsWithChildren } from 'react';
3
3
 
4
4
  type EventParams = Record<string, any>;
5
- interface TrackContextValue {
5
+ type TrackContextValue = {
6
6
  sendEvent: (eventName: string, params?: EventParams) => void;
7
- }
7
+ };
8
+
8
9
  declare const useTracker: () => TrackContextValue;
9
10
  declare const TrackRoot: ({ onEvent, children }: PropsWithChildren<{
10
11
  onEvent: (eventName: string, params?: EventParams) => void;
@@ -13,4 +14,6 @@ declare const TrackProvider: ({ params, children }: PropsWithChildren<{
13
14
  params: EventParams;
14
15
  }>) => react_jsx_runtime.JSX.Element;
15
16
 
16
- export { TrackProvider, TrackRoot, useTracker };
17
+ declare function useMountEvent(eventName: string, params?: EventParams): void;
18
+
19
+ export { TrackProvider, TrackRoot, useMountEvent, useTracker };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useContext, useRef, useCallback, useMemo } from 'react';
1
+ import React, { useContext, useRef, useCallback, useMemo, useEffect } from 'react';
2
2
 
3
3
  const TrackContext = React.createContext(null);
4
4
  const useTracker = () => {
@@ -41,4 +41,16 @@ const TrackProvider = ({
41
41
  return /* @__PURE__ */ React.createElement(TrackContext.Provider, { value }, children);
42
42
  };
43
43
 
44
- export { TrackProvider, TrackRoot, useTracker };
44
+ function useMountEvent(eventName, params) {
45
+ const { sendEvent } = useTracker();
46
+ const counterRef = useRef(0);
47
+ useEffect(() => {
48
+ if (counterRef.current > 0) {
49
+ return;
50
+ }
51
+ counterRef.current++;
52
+ sendEvent(eventName, params);
53
+ }, []);
54
+ }
55
+
56
+ export { TrackProvider, TrackRoot, useMountEvent, useTracker };
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "react-event-tracking",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "files": [
5
5
  "dist"
6
6
  ],
7
+ "repository": "git@github.com:zeeeeby/react-event-tracking.git",
7
8
  "exports": {
8
9
  ".": {
9
10
  "types": "./dist/index.d.ts",
@@ -13,7 +14,8 @@
13
14
  },
14
15
  "scripts": {
15
16
  "build": "tsc --noEmit && unbuild",
16
- "test": "vitest"
17
+ "test": "vitest",
18
+ "toc": "npx markdown-toc README.md -i"
17
19
  },
18
20
  "author": "",
19
21
  "license": "ISC",