chub-dev 0.1.0 → 0.1.2-beta.0
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 +55 -0
- package/bin/chub-mcp +2 -0
- package/dist/airtable/docs/database/javascript/DOC.md +1437 -0
- package/dist/airtable/docs/database/python/DOC.md +1735 -0
- package/dist/amplitude/docs/analytics/javascript/DOC.md +1282 -0
- package/dist/amplitude/docs/analytics/python/DOC.md +1199 -0
- package/dist/anthropic/docs/claude-api/javascript/DOC.md +503 -0
- package/dist/anthropic/docs/claude-api/python/DOC.md +389 -0
- package/dist/asana/docs/tasks/DOC.md +1396 -0
- package/dist/assemblyai/docs/transcription/DOC.md +1043 -0
- package/dist/atlassian/docs/confluence/javascript/DOC.md +1347 -0
- package/dist/atlassian/docs/confluence/python/DOC.md +1604 -0
- package/dist/auth0/docs/identity/javascript/DOC.md +968 -0
- package/dist/auth0/docs/identity/python/DOC.md +1199 -0
- package/dist/aws/docs/s3/javascript/DOC.md +1773 -0
- package/dist/aws/docs/s3/python/DOC.md +1807 -0
- package/dist/binance/docs/trading/javascript/DOC.md +1315 -0
- package/dist/binance/docs/trading/python/DOC.md +1454 -0
- package/dist/braintree/docs/gateway/javascript/DOC.md +1278 -0
- package/dist/braintree/docs/gateway/python/DOC.md +1179 -0
- package/dist/chromadb/docs/embeddings-db/javascript/DOC.md +1263 -0
- package/dist/chromadb/docs/embeddings-db/python/DOC.md +1707 -0
- package/dist/clerk/docs/auth/javascript/DOC.md +1220 -0
- package/dist/clerk/docs/auth/python/DOC.md +274 -0
- package/dist/cloudflare/docs/workers/javascript/DOC.md +918 -0
- package/dist/cloudflare/docs/workers/python/DOC.md +994 -0
- package/dist/cockroachdb/docs/distributed-db/DOC.md +1500 -0
- package/dist/cohere/docs/llm/DOC.md +1335 -0
- package/dist/datadog/docs/monitoring/javascript/DOC.md +1740 -0
- package/dist/datadog/docs/monitoring/python/DOC.md +1815 -0
- package/dist/deepgram/docs/speech/javascript/DOC.md +885 -0
- package/dist/deepgram/docs/speech/python/DOC.md +685 -0
- package/dist/deepl/docs/translation/javascript/DOC.md +887 -0
- package/dist/deepl/docs/translation/python/DOC.md +944 -0
- package/dist/deepseek/docs/llm/DOC.md +1220 -0
- package/dist/directus/docs/headless-cms/javascript/DOC.md +1128 -0
- package/dist/directus/docs/headless-cms/python/DOC.md +1276 -0
- package/dist/discord/docs/bot/javascript/DOC.md +1090 -0
- package/dist/discord/docs/bot/python/DOC.md +1130 -0
- package/dist/elasticsearch/docs/search/DOC.md +1634 -0
- package/dist/elevenlabs/docs/text-to-speech/javascript/DOC.md +336 -0
- package/dist/elevenlabs/docs/text-to-speech/python/DOC.md +552 -0
- package/dist/firebase/docs/auth/DOC.md +1015 -0
- package/dist/gemini/docs/genai/javascript/DOC.md +691 -0
- package/dist/gemini/docs/genai/python/DOC.md +555 -0
- package/dist/github/docs/octokit/DOC.md +1560 -0
- package/dist/google/docs/bigquery/javascript/DOC.md +1688 -0
- package/dist/google/docs/bigquery/python/DOC.md +1503 -0
- package/dist/hubspot/docs/crm/javascript/DOC.md +1805 -0
- package/dist/hubspot/docs/crm/python/DOC.md +2033 -0
- package/dist/huggingface/docs/transformers/DOC.md +948 -0
- package/dist/intercom/docs/messaging/javascript/DOC.md +1844 -0
- package/dist/intercom/docs/messaging/python/DOC.md +1797 -0
- package/dist/jira/docs/issues/javascript/DOC.md +1420 -0
- package/dist/jira/docs/issues/python/DOC.md +1492 -0
- package/dist/kafka/docs/streaming/javascript/DOC.md +1671 -0
- package/dist/kafka/docs/streaming/python/DOC.md +1464 -0
- package/dist/landingai-ade/docs/api/DOC.md +620 -0
- package/dist/landingai-ade/docs/sdk/python/DOC.md +489 -0
- package/dist/landingai-ade/docs/sdk/typescript/DOC.md +542 -0
- package/dist/landingai-ade/skills/SKILL.md +489 -0
- package/dist/launchdarkly/docs/feature-flags/javascript/DOC.md +1191 -0
- package/dist/launchdarkly/docs/feature-flags/python/DOC.md +1671 -0
- package/dist/linear/docs/tracker/DOC.md +1554 -0
- package/dist/livekit/docs/realtime/javascript/DOC.md +303 -0
- package/dist/livekit/docs/realtime/python/DOC.md +163 -0
- package/dist/mailchimp/docs/marketing/DOC.md +1420 -0
- package/dist/meilisearch/docs/search/DOC.md +1241 -0
- package/dist/microsoft/docs/onedrive/javascript/DOC.md +1421 -0
- package/dist/microsoft/docs/onedrive/python/DOC.md +1549 -0
- package/dist/mongodb/docs/atlas/DOC.md +2041 -0
- package/dist/notion/docs/workspace-api/javascript/DOC.md +1435 -0
- package/dist/notion/docs/workspace-api/python/DOC.md +1400 -0
- package/dist/okta/docs/identity/javascript/DOC.md +1171 -0
- package/dist/okta/docs/identity/python/DOC.md +1401 -0
- package/dist/openai/docs/chat/javascript/DOC.md +407 -0
- package/dist/openai/docs/chat/python/DOC.md +568 -0
- package/dist/paypal/docs/checkout/DOC.md +278 -0
- package/dist/pinecone/docs/sdk/javascript/DOC.md +984 -0
- package/dist/pinecone/docs/sdk/python/DOC.md +1395 -0
- package/dist/plaid/docs/banking/javascript/DOC.md +1163 -0
- package/dist/plaid/docs/banking/python/DOC.md +1203 -0
- package/dist/playwright-community/skills/login-flows/SKILL.md +108 -0
- package/dist/postmark/docs/transactional-email/DOC.md +1168 -0
- package/dist/prisma/docs/orm/javascript/DOC.md +1419 -0
- package/dist/prisma/docs/orm/python/DOC.md +1317 -0
- package/dist/qdrant/docs/vector-search/javascript/DOC.md +1221 -0
- package/dist/qdrant/docs/vector-search/python/DOC.md +1653 -0
- package/dist/rabbitmq/docs/message-queue/javascript/DOC.md +1193 -0
- package/dist/rabbitmq/docs/message-queue/python/DOC.md +1243 -0
- package/dist/razorpay/docs/payments/javascript/DOC.md +1219 -0
- package/dist/razorpay/docs/payments/python/DOC.md +1330 -0
- package/dist/redis/docs/key-value/javascript/DOC.md +1851 -0
- package/dist/redis/docs/key-value/python/DOC.md +2054 -0
- package/dist/registry.json +2817 -0
- package/dist/replicate/docs/model-hosting/DOC.md +1318 -0
- package/dist/resend/docs/email/DOC.md +1271 -0
- package/dist/salesforce/docs/crm/javascript/DOC.md +1241 -0
- package/dist/salesforce/docs/crm/python/DOC.md +1183 -0
- package/dist/search-index.json +1 -0
- package/dist/sendgrid/docs/email-api/javascript/DOC.md +371 -0
- package/dist/sendgrid/docs/email-api/python/DOC.md +656 -0
- package/dist/sentry/docs/error-tracking/javascript/DOC.md +1073 -0
- package/dist/sentry/docs/error-tracking/python/DOC.md +1309 -0
- package/dist/shopify/docs/storefront/DOC.md +457 -0
- package/dist/slack/docs/workspace/javascript/DOC.md +933 -0
- package/dist/slack/docs/workspace/python/DOC.md +271 -0
- package/dist/square/docs/payments/javascript/DOC.md +1855 -0
- package/dist/square/docs/payments/python/DOC.md +1728 -0
- package/dist/stripe/docs/api/DOC.md +1727 -0
- package/dist/stripe/docs/payments/DOC.md +1726 -0
- package/dist/stytch/docs/auth/javascript/DOC.md +1813 -0
- package/dist/stytch/docs/auth/python/DOC.md +1962 -0
- package/dist/supabase/docs/client/DOC.md +1606 -0
- package/dist/twilio/docs/messaging/python/DOC.md +469 -0
- package/dist/twilio/docs/messaging/typescript/DOC.md +946 -0
- package/dist/vercel/docs/platform/DOC.md +1940 -0
- package/dist/weaviate/docs/vector-db/javascript/DOC.md +1268 -0
- package/dist/weaviate/docs/vector-db/python/DOC.md +1388 -0
- package/dist/zendesk/docs/support/javascript/DOC.md +2150 -0
- package/dist/zendesk/docs/support/python/DOC.md +2297 -0
- package/package.json +22 -6
- package/skills/get-api-docs/SKILL.md +84 -0
- package/src/commands/annotate.js +83 -0
- package/src/commands/build.js +12 -1
- package/src/commands/feedback.js +150 -0
- package/src/commands/get.js +83 -42
- package/src/commands/search.js +7 -0
- package/src/index.js +43 -17
- package/src/lib/analytics.js +90 -0
- package/src/lib/annotations.js +57 -0
- package/src/lib/bm25.js +170 -0
- package/src/lib/cache.js +69 -6
- package/src/lib/config.js +8 -3
- package/src/lib/identity.js +99 -0
- package/src/lib/registry.js +103 -20
- package/src/lib/telemetry.js +86 -0
- package/src/mcp/server.js +177 -0
- package/src/mcp/tools.js +251 -0
|
@@ -0,0 +1,1282 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: analytics
|
|
3
|
+
description: "Amplitude Analytics JavaScript SDK for browser-based event tracking and product analytics"
|
|
4
|
+
metadata:
|
|
5
|
+
languages: "javascript"
|
|
6
|
+
versions: "2.30.1"
|
|
7
|
+
updated-on: "2026-03-02"
|
|
8
|
+
source: maintainer
|
|
9
|
+
tags: "amplitude,analytics,events,tracking,product"
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# Amplitude Analytics - JavaScript SDK (@amplitude/analytics-browser)
|
|
13
|
+
|
|
14
|
+
## Golden Rule
|
|
15
|
+
|
|
16
|
+
**ALWAYS** use `@amplitude/analytics-browser` version 2.30.1 or newer for browser-based JavaScript applications.
|
|
17
|
+
|
|
18
|
+
**DO NOT** use the deprecated `amplitude-js` package. It is no longer maintained and lacks modern features.
|
|
19
|
+
|
|
20
|
+
For Node.js/server-side applications, use `@amplitude/analytics-node` instead.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
### Browser SDK (NPM/Yarn)
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @amplitude/analytics-browser
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
yarn add @amplitude/analytics-browser
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Script Loader (CDN)
|
|
37
|
+
|
|
38
|
+
```html
|
|
39
|
+
<script src="https://cdn.amplitude.com/script/AMPLITUDE_API_KEY.js"></script>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Environment Variables Setup
|
|
45
|
+
|
|
46
|
+
Store your API key securely using environment variables:
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
// .env file
|
|
50
|
+
VITE_AMPLITUDE_API_KEY=your_api_key_here
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
// Access in code
|
|
55
|
+
const API_KEY = import.meta.env.VITE_AMPLITUDE_API_KEY; // Vite
|
|
56
|
+
const API_KEY = process.env.REACT_APP_AMPLITUDE_API_KEY; // Create React App
|
|
57
|
+
const API_KEY = process.env.NEXT_PUBLIC_AMPLITUDE_API_KEY; // Next.js
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## Initialization
|
|
63
|
+
|
|
64
|
+
### Basic Initialization
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
import * as amplitude from '@amplitude/analytics-browser';
|
|
68
|
+
|
|
69
|
+
amplitude.init('YOUR_API_KEY');
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Initialize with User ID
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
amplitude.init('YOUR_API_KEY', 'user@example.com');
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Initialize with Configuration
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
amplitude.init('YOUR_API_KEY', 'user@example.com', {
|
|
82
|
+
serverZone: 'US', // or 'EU' for European data residency
|
|
83
|
+
logLevel: amplitude.Types.LogLevel.Debug,
|
|
84
|
+
flushIntervalMillis: 1000,
|
|
85
|
+
flushQueueSize: 30,
|
|
86
|
+
sessionTimeout: 1800000, // 30 minutes
|
|
87
|
+
optOut: false,
|
|
88
|
+
autocapture: true
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Wait for Initialization
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
await amplitude.init('YOUR_API_KEY').promise;
|
|
96
|
+
console.log('Amplitude initialized');
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Angular Applications with Zone.js
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
import { NgZone } from '@angular/core';
|
|
103
|
+
|
|
104
|
+
constructor(private ngZone: NgZone) {
|
|
105
|
+
this.ngZone.runOutsideAngular(() => {
|
|
106
|
+
amplitude.init('YOUR_API_KEY');
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## Configuration Options
|
|
114
|
+
|
|
115
|
+
| Option | Type | Default | Description |
|
|
116
|
+
|--------|------|---------|-------------|
|
|
117
|
+
| `flushIntervalMillis` | number | 1000 | Time between event batch uploads (ms) |
|
|
118
|
+
| `flushQueueSize` | number | 30 | Maximum events before triggering upload |
|
|
119
|
+
| `serverZone` | 'US' \| 'EU' | 'US' | Data residency region |
|
|
120
|
+
| `userId` | string | undefined | User identifier (min 5 characters) |
|
|
121
|
+
| `deviceId` | string | UUID() | Device identifier |
|
|
122
|
+
| `sessionTimeout` | number | 1800000 | Session timeout duration (ms) |
|
|
123
|
+
| `optOut` | boolean | false | Disable event tracking |
|
|
124
|
+
| `autocapture` | boolean \| object | true | Enable automatic event capture |
|
|
125
|
+
| `identityStorage` | string | 'cookie' | Storage method: 'cookie', 'localStorage', 'sessionStorage' |
|
|
126
|
+
| `minIdLength` | number | 5 | Minimum length for user/device IDs |
|
|
127
|
+
| `logLevel` | LogLevel | Warn | Console logging verbosity |
|
|
128
|
+
| `trackingOptions` | object | {} | Disable specific auto-tracked properties |
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## Core API Surfaces
|
|
133
|
+
|
|
134
|
+
### Event Tracking
|
|
135
|
+
|
|
136
|
+
#### Basic Event
|
|
137
|
+
|
|
138
|
+
```javascript
|
|
139
|
+
amplitude.track('Button Clicked');
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### Event with Properties
|
|
143
|
+
|
|
144
|
+
```javascript
|
|
145
|
+
amplitude.track('Button Clicked', {
|
|
146
|
+
buttonColor: 'primary',
|
|
147
|
+
buttonText: 'Submit',
|
|
148
|
+
section: 'header',
|
|
149
|
+
userId: 12345
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### Advanced Event Object
|
|
154
|
+
|
|
155
|
+
```javascript
|
|
156
|
+
const event = {
|
|
157
|
+
event_type: 'Product Viewed',
|
|
158
|
+
event_properties: {
|
|
159
|
+
productId: 'SKU-123',
|
|
160
|
+
category: 'Electronics',
|
|
161
|
+
price: 299.99
|
|
162
|
+
},
|
|
163
|
+
groups: {
|
|
164
|
+
organization: 'acme-corp'
|
|
165
|
+
},
|
|
166
|
+
group_properties: {
|
|
167
|
+
plan: 'enterprise',
|
|
168
|
+
seats: 50
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
amplitude.track(event);
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
#### Event with Callback
|
|
176
|
+
|
|
177
|
+
```javascript
|
|
178
|
+
const result = await amplitude.track('Form Submitted', {
|
|
179
|
+
formName: 'contact'
|
|
180
|
+
}).promise;
|
|
181
|
+
|
|
182
|
+
console.log(result.code); // HTTP status code
|
|
183
|
+
console.log(result.message); // Response message
|
|
184
|
+
console.log(result.event); // Event object
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
### User Identification
|
|
190
|
+
|
|
191
|
+
#### Set User ID
|
|
192
|
+
|
|
193
|
+
```javascript
|
|
194
|
+
amplitude.setUserId('user@example.com');
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
#### Set Device ID
|
|
198
|
+
|
|
199
|
+
```javascript
|
|
200
|
+
amplitude.setDeviceId('custom-device-id-12345');
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
#### Set Session ID
|
|
204
|
+
|
|
205
|
+
```javascript
|
|
206
|
+
amplitude.setSessionId(Date.now());
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
#### Reset User (Logout)
|
|
210
|
+
|
|
211
|
+
```javascript
|
|
212
|
+
// Clears userId and generates new deviceId
|
|
213
|
+
amplitude.reset();
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
### User Properties
|
|
219
|
+
|
|
220
|
+
#### Create Identify Object
|
|
221
|
+
|
|
222
|
+
```javascript
|
|
223
|
+
const identify = new amplitude.Identify();
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
#### Set Properties
|
|
227
|
+
|
|
228
|
+
```javascript
|
|
229
|
+
const identify = new amplitude.Identify();
|
|
230
|
+
identify.set('name', 'John Doe');
|
|
231
|
+
identify.set('email', 'john@example.com');
|
|
232
|
+
identify.set('age', 30);
|
|
233
|
+
identify.set('premium', true);
|
|
234
|
+
|
|
235
|
+
amplitude.identify(identify);
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
#### Set Once (Only if Not Already Set)
|
|
239
|
+
|
|
240
|
+
```javascript
|
|
241
|
+
const identify = new amplitude.Identify();
|
|
242
|
+
identify.setOnce('signup_date', new Date().toISOString());
|
|
243
|
+
identify.setOnce('first_source', 'google');
|
|
244
|
+
|
|
245
|
+
amplitude.identify(identify);
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
#### Add (Increment Numeric Values)
|
|
249
|
+
|
|
250
|
+
```javascript
|
|
251
|
+
const identify = new amplitude.Identify();
|
|
252
|
+
identify.add('login_count', 1);
|
|
253
|
+
identify.add('credits', 100);
|
|
254
|
+
|
|
255
|
+
amplitude.identify(identify);
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
#### Append to Array
|
|
259
|
+
|
|
260
|
+
```javascript
|
|
261
|
+
const identify = new amplitude.Identify();
|
|
262
|
+
identify.append('visited_pages', '/dashboard');
|
|
263
|
+
identify.append('purchased_products', 'SKU-123');
|
|
264
|
+
|
|
265
|
+
amplitude.identify(identify);
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
#### Prepend to Array
|
|
269
|
+
|
|
270
|
+
```javascript
|
|
271
|
+
const identify = new amplitude.Identify();
|
|
272
|
+
identify.prepend('recent_searches', 'laptop');
|
|
273
|
+
|
|
274
|
+
amplitude.identify(identify);
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
#### Post Insert (Add to End if Not Present)
|
|
278
|
+
|
|
279
|
+
```javascript
|
|
280
|
+
const identify = new amplitude.Identify();
|
|
281
|
+
identify.postInsert('tags', 'premium');
|
|
282
|
+
|
|
283
|
+
amplitude.identify(identify);
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
#### Pre Insert (Add to Beginning if Not Present)
|
|
287
|
+
|
|
288
|
+
```javascript
|
|
289
|
+
const identify = new amplitude.Identify();
|
|
290
|
+
identify.preInsert('badges', 'early_adopter');
|
|
291
|
+
|
|
292
|
+
amplitude.identify(identify);
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
#### Remove from Array
|
|
296
|
+
|
|
297
|
+
```javascript
|
|
298
|
+
const identify = new amplitude.Identify();
|
|
299
|
+
identify.remove('blocked_users', 'user123');
|
|
300
|
+
|
|
301
|
+
amplitude.identify(identify);
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
#### Unset Property
|
|
305
|
+
|
|
306
|
+
```javascript
|
|
307
|
+
const identify = new amplitude.Identify();
|
|
308
|
+
identify.unset('temporary_token');
|
|
309
|
+
|
|
310
|
+
amplitude.identify(identify);
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
#### Clear All Properties
|
|
314
|
+
|
|
315
|
+
```javascript
|
|
316
|
+
const identify = new amplitude.Identify();
|
|
317
|
+
identify.clearAll();
|
|
318
|
+
|
|
319
|
+
amplitude.identify(identify);
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
#### Chain Multiple Operations
|
|
323
|
+
|
|
324
|
+
```javascript
|
|
325
|
+
const identify = new amplitude.Identify()
|
|
326
|
+
.set('plan', 'premium')
|
|
327
|
+
.add('login_count', 1)
|
|
328
|
+
.append('visited_pages', '/checkout')
|
|
329
|
+
.setOnce('signup_date', '2025-01-01');
|
|
330
|
+
|
|
331
|
+
amplitude.identify(identify);
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
---
|
|
335
|
+
|
|
336
|
+
### Group Analytics
|
|
337
|
+
|
|
338
|
+
#### Set Single Group
|
|
339
|
+
|
|
340
|
+
```javascript
|
|
341
|
+
amplitude.setGroup('organization', 'acme-corp');
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
#### Set Multiple Groups
|
|
345
|
+
|
|
346
|
+
```javascript
|
|
347
|
+
amplitude.setGroup('teams', ['engineering', 'product', 'design']);
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
#### Set Group Properties
|
|
351
|
+
|
|
352
|
+
```javascript
|
|
353
|
+
const groupIdentify = new amplitude.Identify();
|
|
354
|
+
groupIdentify.set('plan', 'enterprise');
|
|
355
|
+
groupIdentify.set('seats', 100);
|
|
356
|
+
groupIdentify.set('industry', 'technology');
|
|
357
|
+
|
|
358
|
+
amplitude.groupIdentify('organization', 'acme-corp', groupIdentify);
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
#### Event-Level Groups (Non-Persistent)
|
|
362
|
+
|
|
363
|
+
```javascript
|
|
364
|
+
amplitude.track({
|
|
365
|
+
event_type: 'Feature Used',
|
|
366
|
+
event_properties: {
|
|
367
|
+
feature_name: 'export'
|
|
368
|
+
},
|
|
369
|
+
groups: {
|
|
370
|
+
department: 'sales',
|
|
371
|
+
region: 'north-america'
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
---
|
|
377
|
+
|
|
378
|
+
### Revenue Tracking
|
|
379
|
+
|
|
380
|
+
#### Basic Revenue
|
|
381
|
+
|
|
382
|
+
```javascript
|
|
383
|
+
const revenue = new amplitude.Revenue()
|
|
384
|
+
.setProductId('com.company.premium')
|
|
385
|
+
.setPrice(9.99);
|
|
386
|
+
|
|
387
|
+
amplitude.revenue(revenue);
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
#### Advanced Revenue Tracking
|
|
391
|
+
|
|
392
|
+
```javascript
|
|
393
|
+
const revenue = new amplitude.Revenue()
|
|
394
|
+
.setProductId('com.company.product')
|
|
395
|
+
.setPrice(29.99)
|
|
396
|
+
.setQuantity(2)
|
|
397
|
+
.setRevenueType('purchase')
|
|
398
|
+
.setReceipt('receipt-id-12345')
|
|
399
|
+
.setReceiptSignature('signature-abc')
|
|
400
|
+
.setEventProperties({
|
|
401
|
+
region: 'US',
|
|
402
|
+
currency: 'USD',
|
|
403
|
+
paymentMethod: 'credit_card'
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
amplitude.revenue(revenue);
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
---
|
|
410
|
+
|
|
411
|
+
### Autocapture Events
|
|
412
|
+
|
|
413
|
+
#### Enable All Autocapture
|
|
414
|
+
|
|
415
|
+
```javascript
|
|
416
|
+
amplitude.init('YOUR_API_KEY', {
|
|
417
|
+
autocapture: true
|
|
418
|
+
});
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
#### Selective Autocapture
|
|
422
|
+
|
|
423
|
+
```javascript
|
|
424
|
+
amplitude.init('YOUR_API_KEY', {
|
|
425
|
+
autocapture: {
|
|
426
|
+
attribution: true, // UTM parameters, referrer
|
|
427
|
+
pageViews: true, // Page view events
|
|
428
|
+
sessions: true, // Session start/end
|
|
429
|
+
formInteractions: true, // Form submissions
|
|
430
|
+
fileDownloads: true, // File download clicks
|
|
431
|
+
elementInteractions: false, // Element clicks (opt-in)
|
|
432
|
+
webVitals: false // Core Web Vitals (opt-in)
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
#### Page View Tracking
|
|
438
|
+
|
|
439
|
+
```javascript
|
|
440
|
+
amplitude.init('YOUR_API_KEY', {
|
|
441
|
+
autocapture: {
|
|
442
|
+
pageViews: {
|
|
443
|
+
trackOn: () => {
|
|
444
|
+
// Custom logic to determine if page should be tracked
|
|
445
|
+
return window.location.pathname.includes('/app');
|
|
446
|
+
},
|
|
447
|
+
trackHistoryChanges: 'pathOnly', // 'all' or 'pathOnly'
|
|
448
|
+
eventType: 'Page Viewed' // Custom event type name
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
#### Element Interaction Tracking
|
|
455
|
+
|
|
456
|
+
```javascript
|
|
457
|
+
amplitude.init('YOUR_API_KEY', {
|
|
458
|
+
autocapture: {
|
|
459
|
+
elementInteractions: {
|
|
460
|
+
cssSelectorAllowlist: ['.track-click', '[data-amp-track]'],
|
|
461
|
+
pageUrlAllowlist: [/\/dashboard/, /\/checkout/],
|
|
462
|
+
dataAttributePrefix: 'data-amp-track'
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
```html
|
|
469
|
+
<button class="track-click" data-amp-track-name="Checkout Button">
|
|
470
|
+
Checkout
|
|
471
|
+
</button>
|
|
472
|
+
```
|
|
473
|
+
|
|
474
|
+
#### Network Request Tracking
|
|
475
|
+
|
|
476
|
+
```javascript
|
|
477
|
+
amplitude.init('YOUR_API_KEY', {
|
|
478
|
+
autocapture: {
|
|
479
|
+
networkTracking: {
|
|
480
|
+
captureRules: [
|
|
481
|
+
{
|
|
482
|
+
urls: [/api\.example\.com/, /\.googleapis\.com/],
|
|
483
|
+
statusCodeRange: '400-599', // Track errors only
|
|
484
|
+
requestHeaders: ['content-type', 'authorization'],
|
|
485
|
+
responseHeaders: ['x-request-id'],
|
|
486
|
+
responseBody: {
|
|
487
|
+
allowlist: ['error', 'message', 'code']
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
]
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
#### Attribution Tracking
|
|
497
|
+
|
|
498
|
+
```javascript
|
|
499
|
+
amplitude.init('YOUR_API_KEY', {
|
|
500
|
+
autocapture: {
|
|
501
|
+
attribution: {
|
|
502
|
+
excludeReferrers: [
|
|
503
|
+
/yourdomain\.com$/,
|
|
504
|
+
'google.com',
|
|
505
|
+
'facebook.com'
|
|
506
|
+
],
|
|
507
|
+
initialEmptyValue: 'EMPTY',
|
|
508
|
+
resetSessionOnNewCampaign: false
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
**Captured attribution parameters:**
|
|
515
|
+
- `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, `utm_content`
|
|
516
|
+
- `referrer`, `referring_domain`
|
|
517
|
+
- `gclid`, `fbclid`, `dclid`, `ttclid`, `msclkid`
|
|
518
|
+
|
|
519
|
+
---
|
|
520
|
+
|
|
521
|
+
### Multiple Project Tracking
|
|
522
|
+
|
|
523
|
+
```javascript
|
|
524
|
+
// Default instance
|
|
525
|
+
const defaultInstance = amplitude.createInstance();
|
|
526
|
+
defaultInstance.init('API_KEY_DEFAULT');
|
|
527
|
+
|
|
528
|
+
// Production instance
|
|
529
|
+
const prodInstance = amplitude.createInstance();
|
|
530
|
+
prodInstance.init('API_KEY_PROD', { instanceName: 'production' });
|
|
531
|
+
|
|
532
|
+
// Development instance
|
|
533
|
+
const devInstance = amplitude.createInstance();
|
|
534
|
+
devInstance.init('API_KEY_DEV', { instanceName: 'development' });
|
|
535
|
+
|
|
536
|
+
// Track to specific instance
|
|
537
|
+
defaultInstance.track('Default Event');
|
|
538
|
+
prodInstance.track('Production Event');
|
|
539
|
+
devInstance.track('Development Event');
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
---
|
|
543
|
+
|
|
544
|
+
### Plugin System
|
|
545
|
+
|
|
546
|
+
#### Enrichment Plugin
|
|
547
|
+
|
|
548
|
+
```javascript
|
|
549
|
+
const urlEnrichmentPlugin = {
|
|
550
|
+
name: 'url-enricher',
|
|
551
|
+
type: 'enrichment',
|
|
552
|
+
setup: async (config) => {
|
|
553
|
+
console.log('Plugin setup complete');
|
|
554
|
+
},
|
|
555
|
+
execute: async (event) => {
|
|
556
|
+
event.event_properties = {
|
|
557
|
+
...event.event_properties,
|
|
558
|
+
page_url: window.location.href,
|
|
559
|
+
page_title: document.title,
|
|
560
|
+
referrer: document.referrer
|
|
561
|
+
};
|
|
562
|
+
return event;
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
amplitude.add(urlEnrichmentPlugin);
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
#### Destination Plugin
|
|
570
|
+
|
|
571
|
+
```javascript
|
|
572
|
+
const customDestinationPlugin = {
|
|
573
|
+
name: 'custom-analytics',
|
|
574
|
+
type: 'destination',
|
|
575
|
+
setup: async (config) => {
|
|
576
|
+
// Initialize custom analytics
|
|
577
|
+
},
|
|
578
|
+
execute: async (event) => {
|
|
579
|
+
try {
|
|
580
|
+
const response = await fetch('https://api.custom-analytics.com/events', {
|
|
581
|
+
method: 'POST',
|
|
582
|
+
headers: {
|
|
583
|
+
'Content-Type': 'application/json',
|
|
584
|
+
'Authorization': 'Bearer TOKEN'
|
|
585
|
+
},
|
|
586
|
+
body: JSON.stringify(event)
|
|
587
|
+
});
|
|
588
|
+
|
|
589
|
+
return {
|
|
590
|
+
code: response.status,
|
|
591
|
+
event: event,
|
|
592
|
+
message: response.statusText
|
|
593
|
+
};
|
|
594
|
+
} catch (error) {
|
|
595
|
+
return {
|
|
596
|
+
code: 500,
|
|
597
|
+
event: event,
|
|
598
|
+
message: error.message
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
|
|
604
|
+
amplitude.add(customDestinationPlugin);
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
#### Remove Plugin
|
|
608
|
+
|
|
609
|
+
```javascript
|
|
610
|
+
amplitude.remove('plugin-name');
|
|
611
|
+
```
|
|
612
|
+
|
|
613
|
+
---
|
|
614
|
+
|
|
615
|
+
### Session Management
|
|
616
|
+
|
|
617
|
+
#### Get Session ID
|
|
618
|
+
|
|
619
|
+
```javascript
|
|
620
|
+
const sessionId = amplitude.getSessionId();
|
|
621
|
+
console.log('Current session:', sessionId);
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
#### Set Custom Session ID
|
|
625
|
+
|
|
626
|
+
```javascript
|
|
627
|
+
amplitude.setSessionId(Date.now());
|
|
628
|
+
```
|
|
629
|
+
|
|
630
|
+
#### Custom Session Timeout
|
|
631
|
+
|
|
632
|
+
```javascript
|
|
633
|
+
amplitude.init('YOUR_API_KEY', {
|
|
634
|
+
sessionTimeout: 600000 // 10 minutes
|
|
635
|
+
});
|
|
636
|
+
```
|
|
637
|
+
|
|
638
|
+
---
|
|
639
|
+
|
|
640
|
+
### Cross-Domain Tracking
|
|
641
|
+
|
|
642
|
+
#### Site 1: Get User/Device IDs
|
|
643
|
+
|
|
644
|
+
```javascript
|
|
645
|
+
const deviceId = amplitude.getDeviceId();
|
|
646
|
+
const sessionId = amplitude.getSessionId();
|
|
647
|
+
const userId = amplitude.getUserId();
|
|
648
|
+
|
|
649
|
+
// Redirect with IDs
|
|
650
|
+
window.location.href = `https://site2.com?ampDeviceId=${deviceId}&SessionId=${sessionId}`;
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
#### Site 2: Automatically Pick Up IDs
|
|
654
|
+
|
|
655
|
+
```javascript
|
|
656
|
+
// IDs automatically extracted from URL parameters
|
|
657
|
+
amplitude.init('YOUR_API_KEY');
|
|
658
|
+
```
|
|
659
|
+
|
|
660
|
+
---
|
|
661
|
+
|
|
662
|
+
### Opt-Out Control
|
|
663
|
+
|
|
664
|
+
#### Disable Tracking
|
|
665
|
+
|
|
666
|
+
```javascript
|
|
667
|
+
amplitude.setOptOut(true);
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
#### Enable Tracking
|
|
671
|
+
|
|
672
|
+
```javascript
|
|
673
|
+
amplitude.setOptOut(false);
|
|
674
|
+
```
|
|
675
|
+
|
|
676
|
+
---
|
|
677
|
+
|
|
678
|
+
### Buffer Management
|
|
679
|
+
|
|
680
|
+
#### Flush Events Immediately
|
|
681
|
+
|
|
682
|
+
```javascript
|
|
683
|
+
amplitude.flush();
|
|
684
|
+
```
|
|
685
|
+
|
|
686
|
+
#### Flush with Promise
|
|
687
|
+
|
|
688
|
+
```javascript
|
|
689
|
+
await amplitude.flush().promise;
|
|
690
|
+
console.log('All events sent');
|
|
691
|
+
```
|
|
692
|
+
|
|
693
|
+
#### Send Beacon on Page Exit
|
|
694
|
+
|
|
695
|
+
```javascript
|
|
696
|
+
window.addEventListener('pagehide', () => {
|
|
697
|
+
amplitude.setTransport('beacon');
|
|
698
|
+
amplitude.flush();
|
|
699
|
+
});
|
|
700
|
+
```
|
|
701
|
+
|
|
702
|
+
---
|
|
703
|
+
|
|
704
|
+
### Tracking Options
|
|
705
|
+
|
|
706
|
+
```javascript
|
|
707
|
+
amplitude.init('YOUR_API_KEY', {
|
|
708
|
+
trackingOptions: {
|
|
709
|
+
ipAddress: false,
|
|
710
|
+
language: false,
|
|
711
|
+
platform: false,
|
|
712
|
+
osName: false,
|
|
713
|
+
osVersion: false,
|
|
714
|
+
deviceManufacturer: false,
|
|
715
|
+
deviceModel: false,
|
|
716
|
+
carrier: false,
|
|
717
|
+
city: false,
|
|
718
|
+
country: false,
|
|
719
|
+
region: false,
|
|
720
|
+
dma: false
|
|
721
|
+
}
|
|
722
|
+
});
|
|
723
|
+
```
|
|
724
|
+
|
|
725
|
+
---
|
|
726
|
+
|
|
727
|
+
### Identity Storage
|
|
728
|
+
|
|
729
|
+
```javascript
|
|
730
|
+
amplitude.init('YOUR_API_KEY', {
|
|
731
|
+
identityStorage: 'localStorage' // 'cookie', 'localStorage', 'sessionStorage', 'none'
|
|
732
|
+
});
|
|
733
|
+
```
|
|
734
|
+
|
|
735
|
+
---
|
|
736
|
+
|
|
737
|
+
### Custom Storage Provider
|
|
738
|
+
|
|
739
|
+
```javascript
|
|
740
|
+
class CustomStorage {
|
|
741
|
+
isEnabled() {
|
|
742
|
+
return true;
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
get(key) {
|
|
746
|
+
return localStorage.getItem(key);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
set(key, value) {
|
|
750
|
+
localStorage.setItem(key, value);
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
remove(key) {
|
|
754
|
+
localStorage.removeItem(key);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
reset() {
|
|
758
|
+
localStorage.clear();
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
getRaw(key) {
|
|
762
|
+
return this.get(key);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
amplitude.init('YOUR_API_KEY', {
|
|
767
|
+
storageProvider: new CustomStorage()
|
|
768
|
+
});
|
|
769
|
+
```
|
|
770
|
+
|
|
771
|
+
---
|
|
772
|
+
|
|
773
|
+
### Debugging
|
|
774
|
+
|
|
775
|
+
#### Enable Debug Logging
|
|
776
|
+
|
|
777
|
+
```javascript
|
|
778
|
+
amplitude.init('YOUR_API_KEY', {
|
|
779
|
+
logLevel: amplitude.Types.LogLevel.Debug
|
|
780
|
+
});
|
|
781
|
+
```
|
|
782
|
+
|
|
783
|
+
**Available log levels:**
|
|
784
|
+
- `None` (0) - No logging
|
|
785
|
+
- `Error` (1) - Error messages only
|
|
786
|
+
- `Warn` (2) - Warnings and errors
|
|
787
|
+
- `Verbose` (3) - Verbose output
|
|
788
|
+
- `Debug` (4) - All debug information
|
|
789
|
+
|
|
790
|
+
#### Custom Logger
|
|
791
|
+
|
|
792
|
+
```javascript
|
|
793
|
+
class CustomLogger {
|
|
794
|
+
disable() {}
|
|
795
|
+
enable() {}
|
|
796
|
+
log(...args) {
|
|
797
|
+
console.log('[AMPLITUDE]', ...args);
|
|
798
|
+
}
|
|
799
|
+
warn(...args) {
|
|
800
|
+
console.warn('[AMPLITUDE]', ...args);
|
|
801
|
+
}
|
|
802
|
+
error(...args) {
|
|
803
|
+
console.error('[AMPLITUDE]', ...args);
|
|
804
|
+
}
|
|
805
|
+
debug(...args) {
|
|
806
|
+
console.debug('[AMPLITUDE]', ...args);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
amplitude.init('YOUR_API_KEY', {
|
|
811
|
+
loggerProvider: new CustomLogger()
|
|
812
|
+
});
|
|
813
|
+
```
|
|
814
|
+
|
|
815
|
+
---
|
|
816
|
+
|
|
817
|
+
### Transport Configuration
|
|
818
|
+
|
|
819
|
+
```javascript
|
|
820
|
+
amplitude.init('YOUR_API_KEY', {
|
|
821
|
+
transport: 'xhr' // 'xhr', 'beacon', or 'fetch'
|
|
822
|
+
});
|
|
823
|
+
```
|
|
824
|
+
|
|
825
|
+
#### Switch Transport at Runtime
|
|
826
|
+
|
|
827
|
+
```javascript
|
|
828
|
+
amplitude.setTransport('beacon');
|
|
829
|
+
```
|
|
830
|
+
|
|
831
|
+
---
|
|
832
|
+
|
|
833
|
+
### Offline Mode
|
|
834
|
+
|
|
835
|
+
Events are automatically stored when offline and sent when connection is restored. This behavior is enabled by default.
|
|
836
|
+
|
|
837
|
+
```javascript
|
|
838
|
+
amplitude.init('YOUR_API_KEY', {
|
|
839
|
+
offline: true // Default behavior
|
|
840
|
+
});
|
|
841
|
+
```
|
|
842
|
+
|
|
843
|
+
---
|
|
844
|
+
|
|
845
|
+
### Server URL Override
|
|
846
|
+
|
|
847
|
+
```javascript
|
|
848
|
+
amplitude.init('YOUR_API_KEY', {
|
|
849
|
+
serverUrl: 'https://custom-proxy.example.com/amplitude'
|
|
850
|
+
});
|
|
851
|
+
```
|
|
852
|
+
|
|
853
|
+
---
|
|
854
|
+
|
|
855
|
+
### User Agent Parser
|
|
856
|
+
|
|
857
|
+
```javascript
|
|
858
|
+
amplitude.init('YOUR_API_KEY', {
|
|
859
|
+
deviceId: 'custom-device-id',
|
|
860
|
+
platform: 'Web',
|
|
861
|
+
osName: 'MacOS',
|
|
862
|
+
osVersion: '14.2',
|
|
863
|
+
deviceModel: 'MacBook Pro'
|
|
864
|
+
});
|
|
865
|
+
```
|
|
866
|
+
|
|
867
|
+
---
|
|
868
|
+
|
|
869
|
+
### Middleware Pattern
|
|
870
|
+
|
|
871
|
+
```javascript
|
|
872
|
+
const timestampMiddleware = (payload) => {
|
|
873
|
+
payload.events.forEach(event => {
|
|
874
|
+
event.event_properties = {
|
|
875
|
+
...event.event_properties,
|
|
876
|
+
middleware_timestamp: Date.now()
|
|
877
|
+
};
|
|
878
|
+
});
|
|
879
|
+
return payload;
|
|
880
|
+
};
|
|
881
|
+
|
|
882
|
+
// Middleware is applied via plugins
|
|
883
|
+
const middlewarePlugin = {
|
|
884
|
+
name: 'middleware',
|
|
885
|
+
type: 'before',
|
|
886
|
+
execute: async (event) => {
|
|
887
|
+
return timestampMiddleware(event);
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
|
|
891
|
+
amplitude.add(middlewarePlugin);
|
|
892
|
+
```
|
|
893
|
+
|
|
894
|
+
---
|
|
895
|
+
|
|
896
|
+
### Error Handling
|
|
897
|
+
|
|
898
|
+
```javascript
|
|
899
|
+
try {
|
|
900
|
+
const result = await amplitude.track('Event Name', {
|
|
901
|
+
property: 'value'
|
|
902
|
+
}).promise;
|
|
903
|
+
|
|
904
|
+
if (result.code === 200) {
|
|
905
|
+
console.log('Event tracked successfully');
|
|
906
|
+
} else {
|
|
907
|
+
console.error('Failed to track event:', result.message);
|
|
908
|
+
}
|
|
909
|
+
} catch (error) {
|
|
910
|
+
console.error('Error tracking event:', error);
|
|
911
|
+
}
|
|
912
|
+
```
|
|
913
|
+
|
|
914
|
+
---
|
|
915
|
+
|
|
916
|
+
### TypeScript Types
|
|
917
|
+
|
|
918
|
+
```typescript
|
|
919
|
+
import * as amplitude from '@amplitude/analytics-browser';
|
|
920
|
+
import {
|
|
921
|
+
BrowserConfig,
|
|
922
|
+
Event,
|
|
923
|
+
Identify,
|
|
924
|
+
Revenue,
|
|
925
|
+
Types
|
|
926
|
+
} from '@amplitude/analytics-browser';
|
|
927
|
+
|
|
928
|
+
const config: BrowserConfig = {
|
|
929
|
+
serverZone: 'US',
|
|
930
|
+
flushQueueSize: 30,
|
|
931
|
+
logLevel: Types.LogLevel.Warn
|
|
932
|
+
};
|
|
933
|
+
|
|
934
|
+
amplitude.init('YOUR_API_KEY', config);
|
|
935
|
+
|
|
936
|
+
const event: Event = {
|
|
937
|
+
event_type: 'Custom Event',
|
|
938
|
+
event_properties: {
|
|
939
|
+
key: 'value'
|
|
940
|
+
}
|
|
941
|
+
};
|
|
942
|
+
|
|
943
|
+
amplitude.track(event);
|
|
944
|
+
```
|
|
945
|
+
|
|
946
|
+
---
|
|
947
|
+
|
|
948
|
+
### React Integration
|
|
949
|
+
|
|
950
|
+
```javascript
|
|
951
|
+
import { useEffect } from 'react';
|
|
952
|
+
import * as amplitude from '@amplitude/analytics-browser';
|
|
953
|
+
|
|
954
|
+
function App() {
|
|
955
|
+
useEffect(() => {
|
|
956
|
+
amplitude.init(process.env.REACT_APP_AMPLITUDE_API_KEY);
|
|
957
|
+
}, []);
|
|
958
|
+
|
|
959
|
+
const handleClick = () => {
|
|
960
|
+
amplitude.track('Button Clicked', {
|
|
961
|
+
component: 'App',
|
|
962
|
+
action: 'click'
|
|
963
|
+
});
|
|
964
|
+
};
|
|
965
|
+
|
|
966
|
+
return <button onClick={handleClick}>Track Event</button>;
|
|
967
|
+
}
|
|
968
|
+
```
|
|
969
|
+
|
|
970
|
+
---
|
|
971
|
+
|
|
972
|
+
### Vue Integration
|
|
973
|
+
|
|
974
|
+
```javascript
|
|
975
|
+
import { onMounted } from 'vue';
|
|
976
|
+
import * as amplitude from '@amplitude/analytics-browser';
|
|
977
|
+
|
|
978
|
+
export default {
|
|
979
|
+
setup() {
|
|
980
|
+
onMounted(() => {
|
|
981
|
+
amplitude.init(import.meta.env.VITE_AMPLITUDE_API_KEY);
|
|
982
|
+
});
|
|
983
|
+
|
|
984
|
+
const trackEvent = () => {
|
|
985
|
+
amplitude.track('Button Clicked', {
|
|
986
|
+
component: 'MyComponent'
|
|
987
|
+
});
|
|
988
|
+
};
|
|
989
|
+
|
|
990
|
+
return { trackEvent };
|
|
991
|
+
}
|
|
992
|
+
};
|
|
993
|
+
```
|
|
994
|
+
|
|
995
|
+
---
|
|
996
|
+
|
|
997
|
+
### Angular Integration
|
|
998
|
+
|
|
999
|
+
```typescript
|
|
1000
|
+
import { Injectable, NgZone } from '@angular/core';
|
|
1001
|
+
import * as amplitude from '@amplitude/analytics-browser';
|
|
1002
|
+
|
|
1003
|
+
@Injectable({
|
|
1004
|
+
providedIn: 'root'
|
|
1005
|
+
})
|
|
1006
|
+
export class AnalyticsService {
|
|
1007
|
+
constructor(private ngZone: NgZone) {
|
|
1008
|
+
this.ngZone.runOutsideAngular(() => {
|
|
1009
|
+
amplitude.init(environment.amplitudeApiKey);
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
track(eventName: string, properties?: Record<string, any>) {
|
|
1014
|
+
amplitude.track(eventName, properties);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
```
|
|
1018
|
+
|
|
1019
|
+
---
|
|
1020
|
+
|
|
1021
|
+
### Next.js Integration
|
|
1022
|
+
|
|
1023
|
+
```javascript
|
|
1024
|
+
// lib/amplitude.js
|
|
1025
|
+
import * as amplitude from '@amplitude/analytics-browser';
|
|
1026
|
+
|
|
1027
|
+
let initialized = false;
|
|
1028
|
+
|
|
1029
|
+
export const initAmplitude = () => {
|
|
1030
|
+
if (typeof window !== 'undefined' && !initialized) {
|
|
1031
|
+
amplitude.init(process.env.NEXT_PUBLIC_AMPLITUDE_API_KEY);
|
|
1032
|
+
initialized = true;
|
|
1033
|
+
}
|
|
1034
|
+
};
|
|
1035
|
+
|
|
1036
|
+
export const trackEvent = (eventName, properties) => {
|
|
1037
|
+
if (typeof window !== 'undefined') {
|
|
1038
|
+
amplitude.track(eventName, properties);
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
```
|
|
1042
|
+
|
|
1043
|
+
```javascript
|
|
1044
|
+
// pages/_app.js
|
|
1045
|
+
import { useEffect } from 'react';
|
|
1046
|
+
import { initAmplitude } from '../lib/amplitude';
|
|
1047
|
+
|
|
1048
|
+
function MyApp({ Component, pageProps }) {
|
|
1049
|
+
useEffect(() => {
|
|
1050
|
+
initAmplitude();
|
|
1051
|
+
}, []);
|
|
1052
|
+
|
|
1053
|
+
return <Component {...pageProps} />;
|
|
1054
|
+
}
|
|
1055
|
+
```
|
|
1056
|
+
|
|
1057
|
+
---
|
|
1058
|
+
|
|
1059
|
+
### EU Data Residency
|
|
1060
|
+
|
|
1061
|
+
```javascript
|
|
1062
|
+
amplitude.init('YOUR_API_KEY', {
|
|
1063
|
+
serverZone: 'EU'
|
|
1064
|
+
});
|
|
1065
|
+
```
|
|
1066
|
+
|
|
1067
|
+
---
|
|
1068
|
+
|
|
1069
|
+
### Minimum ID Length
|
|
1070
|
+
|
|
1071
|
+
```javascript
|
|
1072
|
+
amplitude.init('YOUR_API_KEY', {
|
|
1073
|
+
minIdLength: 3 // Default is 5
|
|
1074
|
+
});
|
|
1075
|
+
```
|
|
1076
|
+
|
|
1077
|
+
---
|
|
1078
|
+
|
|
1079
|
+
### Partner ID
|
|
1080
|
+
|
|
1081
|
+
```javascript
|
|
1082
|
+
amplitude.init('YOUR_API_KEY', {
|
|
1083
|
+
partnerId: 'your-partner-id'
|
|
1084
|
+
});
|
|
1085
|
+
```
|
|
1086
|
+
|
|
1087
|
+
---
|
|
1088
|
+
|
|
1089
|
+
### App Version Tracking
|
|
1090
|
+
|
|
1091
|
+
```javascript
|
|
1092
|
+
amplitude.init('YOUR_API_KEY', {
|
|
1093
|
+
appVersion: '1.2.3'
|
|
1094
|
+
});
|
|
1095
|
+
```
|
|
1096
|
+
|
|
1097
|
+
---
|
|
1098
|
+
|
|
1099
|
+
## Common Patterns
|
|
1100
|
+
|
|
1101
|
+
### Page View Tracking
|
|
1102
|
+
|
|
1103
|
+
```javascript
|
|
1104
|
+
// Track page view on route change
|
|
1105
|
+
amplitude.track('Page Viewed', {
|
|
1106
|
+
page_url: window.location.href,
|
|
1107
|
+
page_path: window.location.pathname,
|
|
1108
|
+
page_title: document.title,
|
|
1109
|
+
referrer: document.referrer
|
|
1110
|
+
});
|
|
1111
|
+
```
|
|
1112
|
+
|
|
1113
|
+
### Search Tracking
|
|
1114
|
+
|
|
1115
|
+
```javascript
|
|
1116
|
+
amplitude.track('Search Performed', {
|
|
1117
|
+
search_query: 'laptop',
|
|
1118
|
+
search_results_count: 42,
|
|
1119
|
+
search_category: 'electronics'
|
|
1120
|
+
});
|
|
1121
|
+
```
|
|
1122
|
+
|
|
1123
|
+
### Form Submission Tracking
|
|
1124
|
+
|
|
1125
|
+
```javascript
|
|
1126
|
+
amplitude.track('Form Submitted', {
|
|
1127
|
+
form_name: 'contact_us',
|
|
1128
|
+
form_id: 'contact-form',
|
|
1129
|
+
fields_completed: 5
|
|
1130
|
+
});
|
|
1131
|
+
```
|
|
1132
|
+
|
|
1133
|
+
### Error Tracking
|
|
1134
|
+
|
|
1135
|
+
```javascript
|
|
1136
|
+
window.addEventListener('error', (event) => {
|
|
1137
|
+
amplitude.track('JavaScript Error', {
|
|
1138
|
+
error_message: event.message,
|
|
1139
|
+
error_filename: event.filename,
|
|
1140
|
+
error_lineno: event.lineno,
|
|
1141
|
+
error_colno: event.colno
|
|
1142
|
+
});
|
|
1143
|
+
});
|
|
1144
|
+
```
|
|
1145
|
+
|
|
1146
|
+
### Social Share Tracking
|
|
1147
|
+
|
|
1148
|
+
```javascript
|
|
1149
|
+
amplitude.track('Content Shared', {
|
|
1150
|
+
platform: 'twitter',
|
|
1151
|
+
content_type: 'article',
|
|
1152
|
+
content_id: 'article-123'
|
|
1153
|
+
});
|
|
1154
|
+
```
|
|
1155
|
+
|
|
1156
|
+
### Video Playback Tracking
|
|
1157
|
+
|
|
1158
|
+
```javascript
|
|
1159
|
+
// Video started
|
|
1160
|
+
amplitude.track('Video Started', {
|
|
1161
|
+
video_id: 'video-123',
|
|
1162
|
+
video_title: 'Product Demo',
|
|
1163
|
+
video_duration: 300
|
|
1164
|
+
});
|
|
1165
|
+
|
|
1166
|
+
// Video progress
|
|
1167
|
+
amplitude.track('Video Progress', {
|
|
1168
|
+
video_id: 'video-123',
|
|
1169
|
+
percentage_watched: 25
|
|
1170
|
+
});
|
|
1171
|
+
|
|
1172
|
+
// Video completed
|
|
1173
|
+
amplitude.track('Video Completed', {
|
|
1174
|
+
video_id: 'video-123',
|
|
1175
|
+
time_watched: 295
|
|
1176
|
+
});
|
|
1177
|
+
```
|
|
1178
|
+
|
|
1179
|
+
---
|
|
1180
|
+
|
|
1181
|
+
## HTTP API Direct Usage
|
|
1182
|
+
|
|
1183
|
+
### Endpoint
|
|
1184
|
+
|
|
1185
|
+
**US:** `https://api2.amplitude.com/2/httpapi`
|
|
1186
|
+
**EU:** `https://api.eu.amplitude.com/2/httpapi`
|
|
1187
|
+
|
|
1188
|
+
### Basic Request
|
|
1189
|
+
|
|
1190
|
+
```javascript
|
|
1191
|
+
const response = await fetch('https://api2.amplitude.com/2/httpapi', {
|
|
1192
|
+
method: 'POST',
|
|
1193
|
+
headers: {
|
|
1194
|
+
'Content-Type': 'application/json'
|
|
1195
|
+
},
|
|
1196
|
+
body: JSON.stringify({
|
|
1197
|
+
api_key: 'YOUR_API_KEY',
|
|
1198
|
+
events: [{
|
|
1199
|
+
user_id: 'user@example.com',
|
|
1200
|
+
device_id: 'C8F9E604-F01A-4BD9-95C6-8E5357DF265D',
|
|
1201
|
+
event_type: 'page_viewed',
|
|
1202
|
+
time: Date.now(),
|
|
1203
|
+
event_properties: {
|
|
1204
|
+
page: 'homepage'
|
|
1205
|
+
}
|
|
1206
|
+
}]
|
|
1207
|
+
})
|
|
1208
|
+
});
|
|
1209
|
+
|
|
1210
|
+
const result = await response.json();
|
|
1211
|
+
console.log(result);
|
|
1212
|
+
```
|
|
1213
|
+
|
|
1214
|
+
---
|
|
1215
|
+
|
|
1216
|
+
## Constraints & Limits
|
|
1217
|
+
|
|
1218
|
+
- **Minimum ID length:** 5 characters (configurable via `minIdLength`)
|
|
1219
|
+
- **Event batch size:** 30 events by default (`flushQueueSize`)
|
|
1220
|
+
- **Flush interval:** 1000ms by default (`flushIntervalMillis`)
|
|
1221
|
+
- **Session timeout:** 30 minutes by default (`sessionTimeout`)
|
|
1222
|
+
- **User ID and Device ID:** Must be strings
|
|
1223
|
+
- **Event requirements:** Must have `event_type` and at least one of `deviceId` or `userId`
|
|
1224
|
+
|
|
1225
|
+
---
|
|
1226
|
+
|
|
1227
|
+
## Best Practice Code Examples
|
|
1228
|
+
|
|
1229
|
+
### Complete Setup
|
|
1230
|
+
|
|
1231
|
+
```javascript
|
|
1232
|
+
import * as amplitude from '@amplitude/analytics-browser';
|
|
1233
|
+
|
|
1234
|
+
// Initialize with full configuration
|
|
1235
|
+
amplitude.init(process.env.VITE_AMPLITUDE_API_KEY, {
|
|
1236
|
+
serverZone: 'US',
|
|
1237
|
+
logLevel: amplitude.Types.LogLevel.Warn,
|
|
1238
|
+
flushIntervalMillis: 1000,
|
|
1239
|
+
flushQueueSize: 30,
|
|
1240
|
+
sessionTimeout: 1800000,
|
|
1241
|
+
autocapture: {
|
|
1242
|
+
attribution: true,
|
|
1243
|
+
pageViews: true,
|
|
1244
|
+
sessions: true,
|
|
1245
|
+
formInteractions: true,
|
|
1246
|
+
fileDownloads: true
|
|
1247
|
+
},
|
|
1248
|
+
identityStorage: 'cookie'
|
|
1249
|
+
});
|
|
1250
|
+
|
|
1251
|
+
// Set user after login
|
|
1252
|
+
amplitude.setUserId('user123@example.com');
|
|
1253
|
+
|
|
1254
|
+
// Set user properties
|
|
1255
|
+
const identify = new amplitude.Identify()
|
|
1256
|
+
.set('plan', 'premium')
|
|
1257
|
+
.set('email', 'user123@example.com')
|
|
1258
|
+
.setOnce('signup_date', new Date().toISOString());
|
|
1259
|
+
|
|
1260
|
+
amplitude.identify(identify);
|
|
1261
|
+
|
|
1262
|
+
// Track custom events
|
|
1263
|
+
amplitude.track('Feature Used', {
|
|
1264
|
+
feature_name: 'export',
|
|
1265
|
+
feature_category: 'data'
|
|
1266
|
+
});
|
|
1267
|
+
```
|
|
1268
|
+
|
|
1269
|
+
### Cleanup on Logout
|
|
1270
|
+
|
|
1271
|
+
```javascript
|
|
1272
|
+
function handleLogout() {
|
|
1273
|
+
// Track logout event
|
|
1274
|
+
amplitude.track('User Logged Out');
|
|
1275
|
+
|
|
1276
|
+
// Flush remaining events
|
|
1277
|
+
amplitude.flush();
|
|
1278
|
+
|
|
1279
|
+
// Reset user identity
|
|
1280
|
+
amplitude.reset();
|
|
1281
|
+
}
|
|
1282
|
+
```
|