Configuring Ember JS Analytics for GDPR


Warning - Old content ahead!

This page has been marked as a legacy post. This means it's quite old (from 2018) and probably out of date. Take it with a grain of salt!

One of the issues I ran into while developing a new Ember JS app was letting the user configure tracking libraries such as Google Analytics for GDPR compliance.

Fortunately it’s not that difficult! I’ve used two packages for this: ember-metrics and ember-local-storage. If you’re conservative with your external dependencies then you could get away without using ember-local-storage and just use native Javascript for this. I chose to use it for convenience.

Make sure you install the two above mentioned packages

ember install ember-local-storage
ember install ember-metrics

Add your configuration for ember-metrics in the config/environment.js ENV hash, here’s mine as an example:

	metricsAdapters: [
		name: 'GoogleAnalytics',
		environments: ['development', 'production'],
		config: {
		  id: 'UA-XXXXXXXX-1',
		  anonymizeIp: true,
		  // Use analytics_debug.js in development
		  debug: environment === 'development',
		  // Use verbose tracing of GA events
		  trace: environment === 'development',
		  // Ensure development env hits aren't sent to GA
		  sendHitTask: environment !== 'development',
		  // Specify Google Analytics plugins
		  require: []

Notice that we’re using anonymizeIP. This is a feature of Google Analytics which, you guessed it, anonymises IP addresses. This is optional and will depend on your project’s requirements.

Don’t forget to add the contentSecurityPolicy settings if you’re using ember-content-security-policy (More about that in the Ember Metrics README).

 contentSecurityPolicy: {
	  'default-src': "'none'",
	  'script-src': "'self'",
	  'font-src': "'self'",
	  'connect-src': "'self'",
	  'img-src': "'self'",
	  'style-src': "'self'",
	  'media-src': "'self'"

Update your app/router.js as instructed on Ember Metrics. We’ll add ember-local-storage to check for the user’s preferences before sending tracking requests as well. I’ve named the storage scope settings, however, you can call it what you prefer. See ember-local-storage README for details.

Within the _trackPage() method we’re checking if is set to true, if it is then we’ll send the request. You could also make other categories, e.g. marketing and send requests for those the same way.

Ember Metrics supports a bunch of the most popular tools out of the box, including Intercom. You can also add your own custom scripts as well.

import EmberRouter from '@ember/routing/router';
import config from './config/environment';
import { inject as service } from '@ember/service';
import { scheduleOnce } from '@ember/runloop';
import { get } from '@ember/object';
import { storageFor } from 'ember-local-storage';

const Router = EmberRouter.extend({
  // Import ember local storage
  settings: storageFor('settings'),
  location: config.locationType,
  rootURL: config.rootURL,
  metrics: service(),
  didTransition() {

  _trackPage() {
	scheduleOnce('afterRender', this, () => {
	  // If user wants analytics then send the request
	  if(this.get('')) {
		const page = this.get('url');
		const title = this.getWithDefault('currentRouteName', 'unknown');
		get(this, 'metrics').trackPage({ page, title });
}); {
  // Your routes here

export default Router;

Now we’ve got the logic in place but the user has no way of updating their preference unless we expect them to update localStorage via the console (If only!).

There are a bunch of ways you can do this, all we need to do is set the in localStorage. I’ve chosen to use a checkbox and I’ve put it on my main index.hbs template. We’ll set the variable initially through the routes/index.js and watch for updates in controller/index.js

<label>{{input type="checkbox" checked=wantsAnalytics }} Enable Analytics</label>

Note that by default we’re setting analytics to false. The user will have to opt-in manually to send their analytical data.

import Route from '@ember/routing/route';
import { storageFor } from 'ember-local-storage';

export default Route.extend({
  settings: storageFor('settings'),
  setupController(controller) {
    // Set default to false to be opt-in, or true to be opt out
	var wantsAnalytics = (this.get('') !== undefined) ? this.get('') : false;
	controller.set('wantsAnalytics', wantsAnalytics);

In the controller we observe the input checkbox value, once it changes we update the local storage to reflect the new value.

import Controller from '@ember/controller';
import { storageFor } from 'ember-local-storage';

export default Controller.extend({
  settings: storageFor('settings'),
  wantsAnalytics: true,
  updateAnalytics: function() {
	this.set('', this.get('wantsAnalytics'));

And that’s all there is to it! You can see this in action on your local environment by opening the developer console. Google Analytics is set to debug mode via Ember Metrics, so you can see when a page view is sent.

Spread the word

Share this article

Like this content?

Check out some of the apps that I've built!


Command-line snippet manager for power users



Third party app for iOS.



GUI for the rsync command