You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
3. Replace `YOUR_API_KEY_HERE` with the API Key given to you.
25
27
4. To track an event anywhere on the page, call:
26
28
27
-
amplitude.logEvent("EVENT_IDENTIFIER_HERE");
29
+
```javascript
30
+
amplitude.logEvent('EVENT_IDENTIFIER_HERE');
31
+
```
28
32
29
33
5. Events are uploaded immediately and saved to the browser's local storage until the server confirms the upload. After calling logEvent in your app, you will immediately see data appear on Amplitude.
30
34
@@ -36,33 +40,31 @@ It's important to think about what types of events you care about as a developer
36
40
37
41
If your app has its own login system that you want to track users with, you can call `setUserId` at any time:
38
42
39
-
amplitude.setUserId("USER_ID_HERE");
43
+
```javascript
44
+
amplitude.setUserId('USER_ID_HERE');
45
+
```
40
46
41
47
A user's data will be merged on the backend so that any events up to that point from the same browser will be tracked under the same user.
42
48
43
49
You can also add the user ID as an argument to the `init` call:
The SDK supports the operations set, setOnce, unset, and add on individual user properties. The operations are declared via a provided `Identify` interface. Multiple operations can be chained together in a single `Identify` object. The `Identify` object is then passed to the Amplitude client to send to the server. The results of the operations will be visible immediately in the dashboard, and take effect for events logged after.
67
+
The SDK supports the operations `set`, `setOnce`, `unset`, and `add` on individual user properties. The operations are declared via a provided `Identify` interface. Multiple operations can be chained together in a single `Identify` object. The `Identify` object is then passed to the Amplitude client to send to the server. The results of the operations will be visible immediately in the dashboard, and take effect for events logged after.
66
68
67
69
1.`set`: this sets the value of a user property.
68
70
@@ -98,15 +100,32 @@ The SDK supports the operations set, setOnce, unset, and add on individual user
98
100
Note:if a user property is used in multiple operations on the same `Identify` object, only the first operation will be saved, and the rest will be ignored. Inthis example, only the set operation will be saved, and the add and unset will be ignored:
99
101
100
102
```javascript
101
-
var identify = new amplitude.Identify().set('karma', 10).add('karma', 1).unset('karma');
You may use `setUserProperties` shorthand to set multiple user properties at once. This method is simply a wrapper around `Identify.set` and `identify`.
113
+
114
+
```javascript
115
+
var userProperties = {
116
+
gender: 'female',
117
+
age: 20
118
+
};
119
+
amplitude.setUserProperties(userProperties);
120
+
```
121
+
105
122
# Tracking Revenue #
106
123
107
124
To track revenue from a user, call
108
125
109
-
amplitude.logRevenue(9.99, 1, "product");
126
+
```javascript
127
+
amplitude.logRevenue(9.99, 1, 'product');
128
+
```
110
129
111
130
The function takes a unit price, a quantity, and a product identifier. Quantity and product identifier are optional parameters.
112
131
@@ -116,27 +135,33 @@ This allows us to automatically display data relevant to revenue on the Amplitud
116
135
117
136
You can turn off logging for a given user:
118
137
119
-
amplitude.setOptOut(true);
138
+
```javascript
139
+
amplitude.setOptOut(true);
140
+
```
120
141
121
142
No events will be saved or sent to the server while opt out is enabled. The opt out
122
143
setting will persist across page loads. Calling
123
144
124
-
setOptOut(false)
145
+
```javascript
146
+
amplitude.setOptOut(false);
147
+
```
125
148
126
149
will reenable logging.
127
150
128
151
# Configuration Options #
129
152
130
153
You can configure Amplitude by passing an object as the third argument to the `init`:
The status and response from the server are passed to the callback function, which you might find useful. An example of a callback function which redirects the browser to another site after a response:
173
204
174
205
```javascript
175
-
var callback_function = function(status, response) {
206
+
varcallback_function=function(status, response) {
176
207
if (status ===200&& response ==='success') {
177
-
// do something here
208
+
// do something here
178
209
}
179
210
window.location.replace('URL_OF_OTHER_SITE');
180
-
};
211
+
};
181
212
```
182
213
183
214
You can also use this to track outbound links on your website. For example you would have a link like this:
And then you would define a function that is called when the link is clicked like this:
188
221
189
222
```javascript
190
223
vartrackClickLinkA=function() {
191
-
amplitude.logEvent('Clicked Link A', null, function() {
192
-
window.location="LINK_A_URL";
193
-
});
194
-
}
224
+
amplitude.logEvent('Clicked Link A', null, function() {
225
+
window.location='LINK_A_URL';
226
+
});
227
+
};
195
228
```
196
229
You can also pass a callback function to init, which will get called after the SDK finishes its asynchronous loading. Note: no values are passed to the init callback function:
In the case that `optOut` is true, then no event will be logged, but the callback will be called. In the case that `batchEvents` is true, if the batch requirements `eventUploadThreshold` and `eventUploadPeriodMillis` are not met when `logEvent` is called, then no request is sent, but the callback is still called. In these cases, the callback will be called with an input status of 0 and response 'No request sent'.
0 commit comments