I want to add a function called "consent" that uses the functionality called Do Not Track (DNT) from browsers. The function role is to add Google analytics on rendered pages when DNT is not active or its state is '1'. Here is what I have come up with in my Nodejs express app:
var concent = function(req, res) {
if(req.header.dnt == '0' || req.header.dnt == null)
{return true;}
else{return false;}
}
The view looks like this in Jade:
if consent
script.
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-11111111-1', 'yourdomain.com');
ga('send', 'pageview');
Unfortunately when I test it it doesn't appear to work...
req.header is a function, not an object. At least for me the actual headers are stored under req.headers (plural s) - but that seems to be a node.js native behaviour: HTTP request.headers.
var consent = function(req, res) {
var DNT = req.header('dnt');
return typeof DNT == 'undefined' || DNT == '0';
}