@rakutenanalytics/rat-react 0.3.1-rc.716ed0a

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Rakuten Analytics
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,193 @@
1
+ # Rakuten Analytics React SDK
2
+
3
+ A comprehensive React SDK for Rakuten Analytics tracking, providing React hooks and components for seamless analytics integration in React applications.
4
+
5
+ ## Features
6
+
7
+ - ✅ **React Hooks** - `useTracker`, `useTrackerContext` for easy integration
8
+ - ✅ **Provider Pattern** - `TrackerProvider` for managing multiple trackers across your app
9
+ - ✅ **Visibility Tracking** - `VisibilityTracker` component for impression tracking
10
+ - ✅ **TypeScript Support** - Full TypeScript definitions and type safety
11
+ - ✅ **Multiple Trackers** - Support for multiple tracker instances with unique keys
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ # Install latest production version
17
+ npm install @rakutenanalytics/rat-react
18
+ ```
19
+
20
+ ## Quick Start
21
+
22
+ ### Simple Hook Usage
23
+
24
+ ```tsx
25
+ import React from 'react';
26
+ import { useTracker } from '@rakutenanalytics/rat-react';
27
+
28
+ function ProductPage() {
29
+ // Initialize tracker with your account and service IDs
30
+ const tracker = useTracker({
31
+ accountId: 999, // Replace with your account ID
32
+ serviceId: 1 // Replace with your service ID
33
+ });
34
+
35
+ // Track page view on mount
36
+ React.useEffect(() => {
37
+ if (tracker) {
38
+ tracker.sendPageviewEvent({
39
+ params: {
40
+ pageName: 'product'
41
+ }
42
+ });
43
+ }
44
+ }, [tracker]);
45
+
46
+ const handleClick = () => {
47
+ if (tracker) {
48
+ tracker.sendClickEvent({
49
+ params: {
50
+ itemId: ['product-123']
51
+ }
52
+ });
53
+ }
54
+ };
55
+
56
+ return (
57
+ <div>
58
+ <h1>Product Page</h1>
59
+ <button onClick={handleClick}>
60
+ Add to Cart
61
+ </button>
62
+ </div>
63
+ );
64
+ }
65
+ ```
66
+
67
+ ### Provider Setup
68
+
69
+ Set up TrackerProvider at your application root for multiple trackers:
70
+
71
+ ```tsx
72
+ // main.tsx
73
+ import { StrictMode } from 'react'
74
+ import { createRoot } from 'react-dom/client'
75
+ import { TrackerProvider, ReceiverEndpoint, type TrackerProviderConfig } from '@rakutenanalytics/rat-react'
76
+ import App from './App.tsx'
77
+
78
+ const trackers: TrackerProviderConfig[] = [
79
+ { key: 'default', options: { accountId: 999, serviceId: 999 } },
80
+ { key: 'custom', options: { accountId: 123, serviceId: 123, mainReceiver: ReceiverEndpoint.UK } }
81
+ ];
82
+
83
+ createRoot(document.getElementById('root')!).render(
84
+ <StrictMode>
85
+ <TrackerProvider defaultTrackers={trackers}>
86
+ <App />
87
+ </TrackerProvider>
88
+ </StrictMode>,
89
+ )
90
+ ```
91
+
92
+ ### Multiple Tracker Usage
93
+
94
+ ```tsx
95
+ // Send events to different endpoints
96
+ import { useTrackerContext } from '@rakutenanalytics/rat-react';
97
+
98
+ function Home() {
99
+ const { getTracker } = useTrackerContext();
100
+
101
+ const defaultTracker = getTracker('default');
102
+ const customTracker = getTracker('custom');
103
+
104
+ const handleProductClick = () => {
105
+ // Send click event to default endpoint (Japan)
106
+ if (defaultTracker) {
107
+ defaultTracker.sendClickEvent({
108
+ params: {
109
+ itemId: ['product-123']
110
+ },
111
+ customParams: {
112
+ clktgt: 'product-button'
113
+ }
114
+ });
115
+ }
116
+ };
117
+
118
+ const handlePurchaseEvent = () => {
119
+ // Send conversion event to UK endpoint
120
+ if (customTracker) {
121
+ customTracker.sendClickEvent({
122
+ cvEvents: {
123
+ purchase_gms: 3400,
124
+ purchase_order: 1,
125
+ purchase_shop: 2
126
+ },
127
+ });
128
+ }
129
+ };
130
+
131
+ return (
132
+ <div>
133
+ <button onClick={handleProductClick}>
134
+ Track Product Click (Default Endpoint)
135
+ </button>
136
+ <button onClick={handlePurchaseEvent}>
137
+ Track Purchase (UK Endpoint)
138
+ </button>
139
+ </div>
140
+ );
141
+ }
142
+ ```
143
+
144
+ ## API Reference
145
+
146
+ ### Hooks
147
+
148
+ #### `useTracker(options: TrackerOptions)`
149
+
150
+ Creates and returns a tracker instance for the specified account and service.
151
+
152
+ ```tsx
153
+ import { useTracker } from '@rakutenanalytics/rat-react';
154
+
155
+ const tracker = useTracker({
156
+ accountId: 999,
157
+ serviceId: 999,
158
+ mainReceiver?: ReceiverEndpoint,
159
+ transportMethod?: TransportMethod,
160
+ });
161
+
162
+ tracker.sendPageviewEvent({
163
+ params: {
164
+ pageName: 'top'
165
+ }
166
+ });
167
+ ```
168
+
169
+ **Parameters:**
170
+ - `options: TrackerOptions` - Configuration object for the tracker
171
+
172
+ **Returns:**
173
+ - `Tracker | null` - Tracker instance (null during initialization)
174
+
175
+ #### `useTrackerContext()`
176
+
177
+ Accesses the tracker context provided by `TrackerProvider`. Must be used within a `TrackerProvider`.
178
+
179
+ ```tsx
180
+ import { useTrackerContext } from '@rakutenanalytics/rat-react';
181
+
182
+ const { trackers, addTracker } = useTrackerContext();
183
+
184
+ // Get existing tracker
185
+ const mainTracker = trackers.get('main');
186
+
187
+ // Add new tracker at runtime
188
+ addTracker('newTracker', { accountId: 123, serviceId: 1 });
189
+ ```
190
+
191
+ **Returns:**
192
+ - `trackers: Map<string, Tracker>` - Map of all available trackers
193
+ - `addTracker: (key: string, options: TrackerOptions) => void` - Function to add new trackers