stampede76
stampede76

Reputation: 1631

node.js request multi line

Google analytics measurement protocol says to use multiple lines for their /batch endpoint:

https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#batch

POST /batch HTTP/1.1
Host: www.google-analytics.com

v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fhome
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fabout
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fcontact

How would I do something like that with node.js and request? Here's my current code for /collect

request.post(
      'http://www.google-analytics.com/batch',
      { form: { v:1,tid:'UA-xxxxx-1',cid:event.queryStringParameters.cid,t:'event',ec:'xxx',ea:"xxx", el:"xxx", ev:"xxx", dr:'xxx'} },
      function (error, response, body) {
        done(null,'Check for GA event');
      }
  );

Upvotes: 1

Views: 300

Answers (1)

hangrynorwegian
hangrynorwegian

Reputation: 102

Combine each line in a single string, separated by "\n".

const request = require("request");

request({
    url: "http://www.google-analytics.com/batch",
    method: "post",
    body: "v=1&t=pageview&tid=UA-XXXXXXXX-X&cid=555&dl=https%3A%2F%2Fmydomain.com%2Ftest&dt=Test\nv=1&t=pageview&tid=UA-XXXXXXXX-X&cid=554&dl=https%3A%2F%2Fmydomain.com%2Ftest2&dt=Test2"
}, function(error, response, body) {
    if (error) { console.log(error); }
});

Your real-time report will show 2 active users on 2 different pages.

Upvotes: 2

Related Questions