Skip to content

Commit

Permalink
Scope GA tracking for the given name
Browse files Browse the repository at this point in the history
  • Loading branch information
PoslinskiNet committed May 19, 2017
1 parent 1c111e3 commit 773a742
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 106 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ module.exports = function(environment) {
environments: ['development', 'production'],
config: {
id: 'UA-XXXX-Y',
// optionally you can provide the name if you like to use multiple GA adapters
name: 'MyApplication'
// 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',
// optional, true is the default
enableOnStart: false,
sendHitTask: environment !== 'development'
}
},
{
Expand Down
24 changes: 11 additions & 13 deletions addon/metrics-adapters/google-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ export default BaseAdapter.extend({
return 'GoogleAnalytics';
},

init() {
name() {
const config = copy(get(this, 'config'));
const { id, sendHitTask, trace, enableOnStart } = config;
let { debug } = config;
return typeof config.name === 'undefined' ? this.toStringExtension() : config.name;
},

const enabled = (typeof enableOnStart != 'undefined' ? enableOnStart : true);
init() {
const config = copy(get(this, 'config'));
const { id, sendHitTask, trace, debug } = config;

assert(`[ember-metrics] You must pass a valid \`id\` to the ${this.toString()} adapter`, id);

Expand All @@ -39,14 +41,10 @@ export default BaseAdapter.extend({
if (trace) {
delete config.trace;
}
if (enableOnStart) {
delete config.enableOnStart;
}

const hasOptions = isPresent(Object.keys(config));

if (canUseDOM && enabled) {

if (canUseDOM) {
/* jshint ignore:start */
(function(i, s, o, g, r, a, m) {
i.GoogleAnalyticsObject = r; i[r] = i[r] || function() {
Expand All @@ -67,7 +65,7 @@ export default BaseAdapter.extend({
}

if (sendHitTask === false) {
window.ga('set', 'sendHitTask', null);
window.ga(`${this.name()}.set`, 'sendHitTask', null);
}
}
},
Expand All @@ -77,7 +75,7 @@ export default BaseAdapter.extend({
const { distinctId } = compactedOptions;

if (canUseDOM) {
window.ga('set', 'userId', distinctId);
window.ga(`${this.name()}.set`, 'userId', distinctId);
}
},

Expand All @@ -103,7 +101,7 @@ export default BaseAdapter.extend({

const event = assign(sendEvent, gaEvent);
if (canUseDOM) {
window.ga('send', event);
window.ga(`${this.name()}.send`, event);
}

return event;
Expand All @@ -115,7 +113,7 @@ export default BaseAdapter.extend({

const event = assign(sendEvent, compactedOptions);
if (canUseDOM) {
window.ga('send', event);
window.ga(`${this.name()}.send`, event);
}

return event;
Expand Down
69 changes: 2 additions & 67 deletions addon/services/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,14 @@ export default Service.extend({
* @return {Object} instantiated adapters
*/
activateAdapters(adapterOptions = []) {
this.willDestroy();

const appEnvironment = get(this, 'appEnvironment');
const cachedAdapters = get(this, '_adapters');
const activatedAdapters = {};


adapterOptions
.filter((adapterOption) => this._filterEnvironments(adapterOption, appEnvironment))
.forEach((adapterOption) => {
const { name, config } = adapterOption;
const { name } = adapterOption;
const adapter = cachedAdapters[name] ? cachedAdapters[name] : this._activateAdapter(adapterOption);

set(activatedAdapters, name, adapter);
Expand All @@ -111,32 +108,6 @@ export default Service.extend({
return set(this, '_adapters', activatedAdapters);
},

/**
* Activates the given adapter that supports enableOnStart flag
*
* @method activateAdapter
* @param {String} adapterName
* @return {Void}
*/
activateAdapter(adapterName) {
if ( this._adapterEnableStateWillChange(adapterName, true) ) {
this.activateAdapters(this._changeAdapterEnableState(adapterName, true));
}
},

/**
* Deactives the given adapter that supports enableOnStart flag
*
* @method deactivateAdapter
* @param {String} adapterName
* @return {Void}
*/
deactivateAdapter(adapterName) {
if ( this._adapterEnableStateWillChange(adapterName, false) ) {
this.activateAdapters(this._changeAdapterEnableState(adapterName, false));
}
},

/**
* Invokes a method on the passed adapter, or across all activated adapters if not passed.
*
Expand Down Expand Up @@ -191,42 +162,6 @@ export default Service.extend({
return Adapter.create({ this, config });
},

/**
* Change the enableOnStart state of the given adapter to the given state
*
* @method _changeAdapterEnableState
* @param {String} adapterName
* @param {Boolean} state
* @return {Object} instantiated adapters
*/
_changeAdapterEnableState(adapterName, state) {
let adapters = this._adapterOptions();

adapters.forEach((adapter) => {
if ( adapter.name == adapterName ) {
set(adapter.config, 'enableOnStart', state);
}
});

return adapters;
},

/**
* Compare the enableOnStart state of the given adapter
*
* @method _adapterEnableStateWillChange
* @param {String} adapterName
* @param {Boolean} state
* @return {Boolean} true if adapter enableOnStart state will change
*/
_adapterEnableStateWillChange(adapterName, state) {
let adapterOption = this._adapterOptions().filter((adapterOption) => {
return adapterOption.name === adapterName;
})[0];

return adapterOption.config.enableOnStart !== state;
},

/**
* The current state of metricsAdapter options
*
Expand Down Expand Up @@ -274,5 +209,5 @@ export default Service.extend({
const wrappedEnvironments = emberArray(environments);

return wrappedEnvironments.indexOf('all') > -1 || wrappedEnvironments.indexOf(appEnvironment) > -1;
}
},
});
14 changes: 13 additions & 1 deletion tests/unit/metrics-adapters/google-analytics-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ test('#identify calls ga with the right arguments', function(assert) {
adapter.identify({
distinctId: 123
});
assert.ok(stub.calledWith('set', 'userId', 123), 'it sends the correct arguments');
assert.ok(stub.calledWith('GoogleAnalytics.set', 'userId', 123), 'it sends the correct arguments');
});

test('#identify calls ga with the right arguments if custom name is provided', function(assert) {
config = Object.assign(config, { name: 'MyCustomAnalytics' });
const adapter = this.subject({ config });
const stub = sandbox.stub(window, 'ga', () => {
return true;
});
adapter.identify({
distinctId: 123
});
assert.ok(stub.calledWith('MyCustomAnalytics.set', 'userId', 123), 'it sends the correct arguments');
});

test('#trackEvent returns the correct response shape', function(assert) {
Expand Down
47 changes: 25 additions & 22 deletions tests/unit/services/metrics-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,32 @@ test('#activateAdapters activates an array of adapters', function(assert) {

test('#activateAdapters is idempotent', function(assert) {
const service = this.subject({ options });
service.activateAdapters([
{
name: 'GoogleAnalytics',
environments: ['all'],
config: {
id: 'I like pie'
}
},
{
name: 'Mixpanel',
environments: ['all'],
config: {
id: 'I like pie'
}
},
{
name: 'LocalDummyAdapter',
environments: ['all'],
config: {
id: 'I like pie'
Ember.run(function() {
service.activateAdapters([
{
name: 'GoogleAnalytics',
environments: ['all'],
config: {
id: 'I like pie'
}
},
{
name: 'Mixpanel',
environments: ['all'],
config: {
id: 'I like pie'
}
},
{
name: 'LocalDummyAdapter',
environments: ['all'],
config: {
id: 'I like pie'
}
}
}
]);
]);
});

assert.equal(get(service, '_adapters.GoogleAnalytics.config.id'), 'UA-XXXX-Y', 'it does not override the GoogleAnalytics adapter');
assert.equal(get(service, '_adapters.Mixpanel.config.token'), '0f76c037-4d76-4fce-8a0f-a9a8f89d1453', 'it does not override the Mixpanel adapter');
assert.equal(get(service, '_adapters.LocalDummyAdapter.config.foo'), 'bar', 'it does not override the LocalDummyAdapter');
Expand Down

0 comments on commit 773a742

Please sign in to comment.