UNPKG

3.1 kBJavaScriptView Raw
1
2
3
4var https = require("https")
5, opts = {
6 hostname: "www.google-analytics.com",
7 port: 443,
8 path: "/batch",
9 method: "POST",
10 headers: {
11 "Content-Length": 0,
12 "User-Agent": ""
13 }
14}
15
16function send(arr) {
17 var req = https.request(opts, (res) => {
18 console.log(`STATUS: ${res.statusCode}`);
19 console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
20 res.setEncoding('utf8');
21 res.on('data', (chunk) => {
22 console.log(`BODY: ${chunk}`);
23 });
24 res.on('end', () => {
25 console.log('No more data in response.');
26 });
27});
28
29req.on('error', (e) => {
30 console.error(`problem with request: ${e.message}`);
31});
32
33// Write data to request body
34req.write(postData);
35req.end()
36}
37
38/*
39POST /batch HTTP/1.1
40Host: www.google-analytics.com
41
42v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fhome
43v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fabout
44v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fcontact
45
46Batch limitations
47In addition to the standard limitations of Measurement Protocol hits, batch requests have the following additional limitations:
48
49 - A maximum of 20 hits can be specified per request.
50 - The total size of all hit payloads cannot be greater than 16K bytes.
51 - No single hit payload can be greater than 8K bytes.
52
53
54Page Tracking
55v=1 // Version.
56&tid=UA-XXXXX-Y // Tracking ID / Property ID.
57&cid=555 // Anonymous Client ID.
58&t=pageview // Pageview hit type.
59&dh=mydemo.com // Document hostname.
60&dp=/home // Page.
61&dt=homepage // Title.
62
63Event Tracking
64v=1 // Version.
65&tid=UA-XXXXX-Y // Tracking ID / Property ID.
66&cid=555 // Anonymous Client ID.
67&t=event // Event hit type
68&ec=video // Event Category. Required.
69&ea=play // Event Action. Required.
70&el=holiday // Event label.
71&ev=300 // Event value.
72
73Exception Tracking
74v=1 // Version.
75&tid=UA-XXXXX-Y // Tracking ID / Property ID.
76&cid=555 // Anonymous Client ID.
77&t=exception // Exception hit type.
78&exd=IOException // Exception description.
79&exf=1 // Exception is fatal?
80
81
82User Timing Tracking
83v=1 // Version.
84&tid=UA-XXXXX-Y // Tracking ID / Property ID.
85&cid=555 // Anonymous Client ID.
86
87&t=timing // Timing hit type.
88&utc=jsonLoader // Timing category.
89&utv=load // Timing variable.
90&utt=5000 // Timing time.
91&utl=jQuery // Timing label.
92 // These values are part of browser load times
93&dns=100 // DNS load time.
94&pdt=20 // Page download time.
95&rrt=32 // Redirect time.
96&tcp=56 // TCP connect time.
97&srt=12 // Server response time.
98
99
100App / Screen Tracking
101v=1 // Version.
102&tid=UA-XXXXX-Y // Tracking ID / Property ID.
103&cid=555 // Anonymous Client ID.
104&t=screenview // Screenview hit type.
105&an=funTimes // App name.
106&av=1.5.0 // App version.
107&aid=com.foo.App // App Id.
108&aiid=com.android.vending // App Installer Id.
109&cd=Home // Screen name / content description.
110
111*/
112