Reputation: 23
I tried to implementing google analytics API in a node-webkit application with Analytics Measurement Protocol, I managed to do that with ga-dev-tools.appspot.com but I can't use it with a simple javascript request.
I suppose the problem was the header parameter "Origin" (in a node-webkit app is "file://"), therefore I tried to use a http request with node but I reached the same result: google respond with "hitParsingResult": [ {"valid": true,...
however in Google Analytics Dashboard ? can't see the event or, in this case, a pageview.
The strange thing is that if I copy the request with chrome inspector on the ga-dev-tools.appspot.com (copy as cURL) and I exec it in the linux terminal it works but not with nodejs http request.
The details: cURL call
curl 'https://www.google-analytics.com/collect' -H 'Accept: */*' -H 'Referer: https://ga-dev-tools.appspot.com/hit-builder/' -H 'Origin: https://ga-dev-tools.appspot.com' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' --data 'v=1&t=screenview&tid=UA-XXXXX-X&cid=develop&an=mainapplication&av=2.0.1&cd=login'
nodejs code
request.post("https://www.google-analytics.com/debug/collect",
var options = {
url: 'https://api.github.com/repos/request/request',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36',
'Origin': 'https://ga-dev-tools.appspot.com',
'Referer': 'https://ga-dev-tools.appspot.com/hit-builder/',
'Accept': '*/*'
},
formData: 'v=1&tid=UA-XXXXX-X&cid=develop&an=mainapplication&av=2.0.1&t=screenview&cd=login'
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
else
{
console.error(error)
}
})
I tried to find a nodejs module to do that but I can't find one with Application integration (screenview, an, av..)
Someone can help me?
Upvotes: 1
Views: 647
Reputation: 23
I resolved all my issues and posted the code on github, if someone needed it: nwjs-analytics
Upvotes: 1
Reputation: 30441
Your curl request is going to https://www.google-analytics.com/collect
and your Node.js request is going to https://www.google-analytics.com/debug/collect
(notice the debug
in the URL).
The debug endpoint is for debugging errors only. It does not send actual hits to Google Analytics. I suspect that's your problem.
Upvotes: 2