discourse/discourse — pull request #1863 by ecuk (2014-01-26 03:10:39+00:00)
Title: Google Universal Analytics

This patch adds Google Universal Analytics (currently beta) functionality so that Discourse sites can take full advantage of the benefits of using the new analytics.js instead of—or in addition to—the older ga.js. (Google's documentation at https://developers.google.com/analytics/devguides/collection/analyticsjs/ says that 'it is perfectly safe to include both ga.js and analytics.js snippets on the same page'.) Google will eventually deprecate the use of ga.js, and they currently recommend upgrading to analytics.js (for the roadmap see https://developers.google.com/analytics/devguides/collection/upgrade/index#phases).

The patch adds two new site settings, `ga_universal_tracking_code` and `ga_universal_domain_name`. If the former is not set, there is no change in behaviour relative to not having this patch. If it *is* set, on the other hand, a script tag with the new Google Analytics code snippet that loads analytics.js is added to the page header. (Because it loads asynchronously, Google specifically says that the script should be within the page `

` element.)

The default value for `ga_universal_domain_name` is 'auto', which, according to the Google Analytics documentation at https://developers.google.com/analytics/devguides/collection/analyticsjs/domains#auto will generally do exactly what you want (even on multi-domain sites).

The page tracking code in page_tracking.js has also been updated to use the Universal Analytics API if `ga_universal_tracking_code` is present.

Because this patch could not be tested in a development environment—because the analytics code is only rendered in a production environment—I have tested this patch on my production server. This means that only the most common paths through the code have been tested. Edge cases appear to be ok, however.