Welcome to mirror list, hosted at ThFree Co, Russian Federation.

browser_sdk.md « instrumentation « product_analytics « user « doc - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6bc9a9ef2345a3bb3598fc70d8f64607c67aa7fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
---
stage: Monitor
group: Analytics Instrumentation
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://handbook.gitlab.com/handbook/product/ux/technical-writing/#assignments
---

# Browser SDK

This SDK is for instrumenting web sites and applications to send data for the GitLab [product analytics functionality](../index.md).

## How to use the Browser SDK

### Using the NPM package

Add the NPM package to your package JSON using your preferred package manager:

::Tabs

:::TabTitle yarn

```shell
yarn add @gitlab/application-sdk-browser
```

:::TabTitle npm

```shell
npm i @gitlab/application-sdk-browser
```

::EndTabs

Then, for browser usage import the client SDK:

```javascript
import { glClientSDK } from '@gitlab/application-sdk-browser';

this.glClient = glClientSDK({ appId, host });
```

### Using the script directly

Add the script to the page and assign the client SDK to `window`:

```html
<script src="https://unpkg.com/@gitlab/application-sdk-browser/dist/gl-sdk.min.js"></script>
<script>
  window.glClient = window.glSDK.glClientSDK({
    appId: 'YOUR_APP_ID',
    host: 'YOUR_HOST',
  });
</script>
```

You can use a specific version of the SDK like this:

```html
<script src="https://unpkg.com/@gitlab/application-sdk-browser@0.2.5/dist/gl-sdk.min.js"></script>
```

## Browser SDK initialization options

Apart from `appId` and `host`, you can configure the Browser SDK with the following options:

```typescript
interface GitLabClientSDKOptions {
  appId: string;
  host: string;
  hasCookieConsent?: boolean;
  respectGlobalPrivacyControl?: boolean;
  trackerId?: string;
  pagePingTracking?:
    | boolean
    | {
        minimumVisitLength?: number;
        heartbeatDelay?: number;
      };
  plugins?: AllowedPlugins;
}
```

| Option                        | Description |
| :---------------------------- | :---------- |
| `appId`                       | The ID provided by the GitLab Project Analytics setup guide. This ID ensures your data is sent to your analytics instance. |
| `host`                        | The GitLab Project Analytics instance provided by the setup guide. |
| `hasCookieConsent`            | Whether to use cookies to identify unique users. Set to `false` by default. When `false`, users are considered anonymous users. No cookies or other storage mechanisms are used to identify users. |
| `respectGlobalPrivacyControl` | Whether to respect the user's [GPC](https://globalprivacycontrol.org/) configuration to permit or refuse tracking. Set to `true` by default. When `false`, events are emitted regardless of user configuration. |
| `trackerId`                   | Used to differentiate between multiple trackers running on the same page or application, because each tracker instance can be configured differently to capture different sets of data. This identifier helps ensure that the data sent to the collector is correctly associated with the correct tracker configuration. Default value is `gitlab`. |
| `pagePingTracking`            | Option to track user engagement on your website or application by sending periodic events while a user is actively browsing a page. Page pings provide valuable insight into how users interact with your content, such as how long they spend on a page, which sections they are viewing, and whether they are scrolling. `pagePingTracking` can be boolean or an object. As a boolean, set to `true` it enables page ping with default options, and set to `false` it disables page ping tracking. As an object, it has two options: `minimumVisitLength` (the minimum time that must have elapsed before the first heartbeat) and `heartbeatDelay` (the interval at which the callback is fired). |
| `plugins`                     | Specify which plugins to enable or disable. By default all plugins are enabled. |

### Plugins

- `Client Hints`: An alternative to tracking the User Agent, which is particularly useful in browsers that are freezing the User Agent string.
Enabling this plugin will automatically capture the following context:

  For example,
  [iglu:org.ietf/http_client_hints/jsonschema/1-0-0](https://github.com/snowplow/iglu-central/blob/master/schemas/org.ietf/http_client_hints/jsonschema/1-0-0)
  has the following configuration:

  ```json
  {
     "isMobile":false,
     "brands":[
        {
           "brand":"Google Chrome",
           "version":"89"
        },
        {
           "brand":"Chromium",
           "version":"89"
        }
     ]
  }
  ```

- `Link Click Tracking`: With this plugin, the tracker adds click event listeners to all link elements. Link clicks are tracked as self-describing events. Each link-click event captures the link's `href` attribute. The event also has fields for the link's ID, classes, and target (where the linked document is opened, such as a new tab or new window).

- `Performance Timing`: It collects performance-related data from a user's browser using the `Navigation Timing API`. This API provides detailed information about the various stages of loading a web page, such as domain lookup, connection time, content download, and rendering times. This plugin helps to gather insights into how well a website performs for users, identify potential performance bottlenecks, and improve the overall user experience.

- `Error Tracking`: It helps to capture and track errors that occur on a website or application. By monitoring these errors, you can gain insights into potential issues with code or third-party libraries, which can help to improve the overall user experience, and maintain the quality of the website or application.

By default all plugins are enabled. You can disable or enable these plugins through the `plugins` object:

```typescript
const tracker = glClientSDK({
  ...options,
  plugins: {
    clientHints: true,
    linkTracking: true,
    performanceTiming: true,
    errorTracking: true,
  },
});
```

## Methods

### `identify`

Used to associate a user and their attributes with the session and tracking events.

```javascript
glClient.identify(userId, userAttributes);
```

| Property         | Type                        | Description                                                                   |
| :--------------- | :-------------------------- | :---------------------------------------------------------------------------- |
| `userId`         | `String`                    | The user identifier your application uses to identify individual users. |
| `userAttributes` | `Object`/`Null`/`undefined` | The user attributes that need to be added to the session and tracking events. |

### `page`

Used to trigger a pageview event.

```javascript
glClient.page(eventAttributes);
```

| Property          | Type                        | Description                                                       |
| :---------------- | :-------------------------- | :---------------------------------------------------------------- |
| `eventAttributes` | `Object`/`Null`/`undefined` | The event attributes that need to be added to the pageview event. |

The `eventAttributes` object supports the following optional properties:

| Property         | Type                        | Description                                                                   |
| :--------------- | :-------------------------- | :---------------------------------------------------------------------------- |
| `title`          | `String`                    | Override the default page title. |
| `contextCallback` | `Function` | A callback that fires on the page view. |
| `context` | `Object` | Add context (additional information) on the page view. |
| `timestamp` | `timestamp` | Set the true timestamp or overwrite the device-sent timestamp on an event. |

### `track`

Used to trigger a custom event.

```javascript
glClient.track(eventName, eventAttributes);
```

| Property          | Type                        | Description                                                      |
| :---------------- | :-------------------------- | :--------------------------------------------------------------- |
| `eventName`       | `String`                    | The name of the custom event.                                    |
| `eventAttributes` | `Object`/`Null`/`undefined` | The event attributes that need to be added to the tracked event. |

### `refreshLinkClickTracking`

`enableLinkClickTracking` tracks only clicks on links that exist when the page has loaded. To track new links added to the page after it has been loaded, use `refreshLinkClickTracking`.

```javascript
glClient.refreshLinkClickTracking();
```

### `trackError`

NOTE:
`trackError` is supported on the Browser SDK, but the resulting events are not used or available.

Used to capture errors. This works only when the `errorTracking` plugin is enabled. The [plugin](#plugins) is enabled by default.

```javascript
glClient.trackError(eventAttributes);
```

For example, `trackError` can be used in `try...catch` like below:

```javascript
try {
  // Call the function that throws an error
  throwError();
} catch (error) {
  glClient.trackError({
    message: error.message, // "This is a custom error"
    filename: error.fileName || 'unknown', // The file in which the error occurred (e.g., "index.html")
    lineno: error.lineNumber || 0, // The line number where the error occurred (e.g., 2)
    colno: error.columnNumber || 0, // The column number where the error occurred (e.g., 6)
    error: error, // The Error object itself
  });
}
```

| Property          | Type     | Description                                                                                                          |
| :---------------- | :------- | :------------------------------------------------------------------------------------------------------------------- |
| `eventAttributes` | `Object` | The event attributes that need to be added to the tracked event. `message` is a mandatory key in `eventAttributes`. |

### `addCookieConsent`

`addCookieConsent` is used to allow tracking of user identifiers via cookies. By default `hasCookieConsent` is false, and no user identifiers are passed. To enable tracking of user identifiers, call the `addCookieConsent` method. This step is not needed if you intialized the Browser SDK with `hasCookieConsent` set to true.

```javascript
glClient.addCookieConsent();
```

### `setCustomUrl`

Used to set a custom URL for tracking.

```javascript
glClient.setCustomUrl(url);
```

| Property | Type     | Description                                       |
| :------- | :------- | :------------------------------------------------ |
| `url`    | `String` | The custom URL that you want to set for tracking. |

### `setReferrerUrl`

Used to set a referrer URL for tracking.

```javascript
glClient.setReferrerUrl(url);
```

| Property | Type     | Description                                         |
| :------- | :------- | :-------------------------------------------------- |
| `url`    | `String` | The referrer URL that you want to set for tracking. |

### `setDocumentTitle`

Used to override the document title.

```javascript
glClient.setDocumentTitle(title);
```

| Property | Type     | Description                        |
| :------- | :------- | :--------------------------------- |
| `title`  | `String` | The document title you want to set. |

## Contribute

If you would like to contribute to Browser SDK, follow the [contributing guide](https://gitlab.com/gitlab-org/analytics-section/product-analytics/gl-application-sdk-js/-/blob/main/docs/Contributing.md).

## Troubleshooting

If the Browser SDK is not sending events, or behaving in an unexpected way, take the following actions:

1. Verify that the `appId` and host values in the options object are correct.
1. Check if any browser privacy settings, extensions, or ad blockers are interfering with the Browser SDK.

For more information and assistance, see the [Snowplow documentation](https://docs.snowplow.io/docs/collecting-data/collecting-from-own-applications/javascript-trackers/browser-tracker/browser-tracker-v3-reference/)
or contact the [Analytics Instrumentation team](https://about.gitlab.com/handbook/engineering/development/analytics/analytics-instrumentation/#team-members).