-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeasurement-protocol.js
152 lines (137 loc) · 2.74 KB
/
measurement-protocol.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
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
import { stringify } from 'querystring';
import http from '@haensl/http';
const config = {
fetch: undefined,
measurementId: undefined,
measurementSecret: undefined,
measurementUrl: 'https://www.google-analytics.com/mp/collect'
};
const getUrl = () => new URL(`${config.measurementUrl}?${stringify({
measurement_id: config.measurementId,
api_secret: config.measurementSecret
})}`);
/**
* Tries to parse Google Analytics client id from
* the cookies object.
* Generates one from timestamps if no cookie.
*/
export const clientId = (cookies = {}) => {
const gaCookie = cookies._ga;
if (gaCookie) {
const clientIdTest = /\d+\.\d+/.exec(gaCookie);
if (Array.isArray(clientIdTest) && clientIdTest.length > 0) {
return clientIdTest[0];
}
}
const pre = Math.round(new Date().getTime() / 1000);
const post = Math.round(new Date().getTime() / 1000);
return `${pre}.${post}`;
};
/**
* Initialize the service with measurement ID and secret.
*/
export const init = ({
fetch,
measurementId,
measurementSecret,
measurementUrl
}) => {
config.measurementId = measurementId;
config.measurementSecret = measurementSecret;
config.measurementUrl = measurementUrl;
config.fetch = fetch;
};
/**
* Tracks an analytics event via measurement protocol,
* i.e. via API.
*
* @param name the event name.
*
* @param params the event parameters.
*
* @param clientId the clientId for this event. Generated if not supplied.
*/
export const event = ({
name,
params,
clientId = clientId()
}) => {
const url = getUrl();
return config.fetch(url, {
method: http.methods.post,
body: JSON.stringify({
client_id: clientId,
events: [
{
name,
params
}
]
})
});
};
export const setUserProperty = ({
name,
value,
clientId = clientId()
}) => {
const url = getUrl();
return config.fetch(url, {
method: http.methods.post,
body: JSON.stringify({
client_id: clientId,
user_properties: {
[name]: value
}
})
});
};
export const setUserId = ({
id,
clientId = clientId()
}) => {
const url = getUrl();
return config.fetch(url, {
method: http.methods.post,
body: JSON.stringify({
user_id: id
})
});
};
export const exception = ({
description,
fatal,
clientId = clientId()
}) => {
return event({
name: 'exception',
params: {
description,
fatal
},
clientId
});
};
export const pageView = ({
location,
title,
clientId = clientId()
}) => {
return event({
name: 'page_view',
params: {
page_title: title,
page_location: location
},
clientId
});
};
export default {
init,
clientId,
event,
exception,
pageView,
setUserId,
setUserProperty
};