-
Notifications
You must be signed in to change notification settings - Fork 287
/
segment.js
38 lines (32 loc) · 1.08 KB
/
segment.js
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
import getConfig from "next/config";
import * as snippet from "@segment/snippet";
/**
* Dispatch tracking data to segment
* @name dispatch
* @ignore
* @param {Object} data Arguments supplied by tracking library
* @param {String} data.action String value used as the first param to analytics.track()
* @returns {undefined} No Return
*/
export function dispatch(data) {
// Workaround for not being able to use object rest spread
const newData = Object.assign({}, data);
const { action } = newData;
delete newData.action;
window && window.analytics && window.analytics.track(action, newData);
}
/**
* Render string script
* @returns {String} String script to be included in the document head
*/
export function renderScript() {
const { publicRuntimeConfig: { segmentAnalytics } } = getConfig();
const opts = {
apiKey: segmentAnalytics.writeKey,
page: true // Set this to `false` if you want to manually fire `analytics.page()` from within your pages.
};
if (segmentAnalytics.skipMinimize === true) {
return snippet.max(opts);
}
return snippet.min(opts);
}