Skip to content

Commit

Permalink
Merge pull request #2 from pgrippi/cookie_domain
Browse files Browse the repository at this point in the history
Add support for setting the cookie configuration for analytics.js
  • Loading branch information
Peter Grippi committed Nov 6, 2014
2 parents 2e227fa + 711ee62 commit 26a66c5
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,38 @@ var merge = require('lodash-node/compat/objects/merge');
var googleAnalyticsConfigDefaults = {
globalVariable: 'ga',
tracker: 'analytics.js',
webPropertyId: null
webPropertyId: null,
cookieDomain: null,
cookieName: null,
cookieExpires: null
};

function analyticsTrackingCode(config) {
var gaConfig = {};
if (config.cookieDomain != null) {
gaConfig.cookieDomain = config.cookieDomain;
}
if (config.cookieName != null) {
gaConfig.cookieName = config.cookieName;
}
if (config.cookieExpires != null) {
gaConfig.cookieExpires = config.cookieExpires;
}

if (Object.keys(gaConfig).length === 0) {
gaConfig = "'auto'";
} else {
gaConfig = JSON.stringify(gaConfig);
}

return [
"<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','" + config.globalVariable + "');",
"",
"" + config.globalVariable + "('create', '" + config.webPropertyId + "', 'auto');",
"" + config.globalVariable + "('create', '" + config.webPropertyId + "', " + gaConfig + ");",
"" + config.globalVariable + "('send', 'pageview');",
"</script>"
];
Expand Down

0 comments on commit 26a66c5

Please sign in to comment.