-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
188 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import Ember from 'ember'; | ||
import canUseDOM from '../utils/can-use-dom'; | ||
import { compact } from '../utils/object-transforms'; | ||
import BaseAdapter from './base'; | ||
|
||
const { | ||
$, | ||
assert, | ||
copy, | ||
get | ||
} = Ember; | ||
|
||
export default BaseAdapter.extend({ | ||
toStringExtension() { | ||
return 'Segment'; | ||
}, | ||
|
||
init() { | ||
const config = copy(get(this, 'config')); | ||
const segmentKey = config.key; | ||
|
||
assert(`[ember-metrics] You must pass a valid \`key\` to the ${this.toString()} adapter`, segmentKey); | ||
|
||
if (canUseDOM) { | ||
/* jshint ignore:start */ | ||
window.analytics=window.analytics||[],window.analytics.methods=["identify","group","track","page","pageview","alias","ready","on","once","off","trackLink","trackForm","trackClick","trackSubmit"],window.analytics.factory=function(t){return function(){var a=Array.prototype.slice.call(arguments);return a.unshift(t),window.analytics.push(a),window.analytics}};for(var i=0;i<window.analytics.methods.length;i++){var key=window.analytics.methods[i];window.analytics[key]=window.analytics.factory(key)}window.analytics.load=function(t){if(!document.getElementById("analytics-js")){var a=document.createElement("script");a.type="text/javascript",a.id="analytics-js",a.async=!0,a.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(a,n)}},window.analytics.SNIPPET_VERSION="2.0.9"; | ||
/* jshint ignore:end */ | ||
window.analytics.load(segmentKey); | ||
} | ||
}, | ||
|
||
alias(options = {}) { | ||
const compactedOptions = compact(options); | ||
const { alias, original } = compactedOptions; | ||
|
||
if (original) { | ||
window.analytics.alias(alias, original); | ||
} else { | ||
window.analytics.alias(alias); | ||
} | ||
}, | ||
|
||
identify(options = {}) { | ||
const compactedOptions = compact(options); | ||
const { distinctId } = compactedOptions; | ||
delete compactedOptions.distinctId; | ||
|
||
window.analytics.identify(distinctId, compactedOptions); | ||
}, | ||
|
||
trackEvent(options = {}) { | ||
const compactedOptions = compact(options); | ||
const { event } = compactedOptions; | ||
delete compactedOptions.event; | ||
|
||
window.analytics.track(event, compactedOptions); | ||
}, | ||
|
||
trackPage(options = {}) { | ||
const compactedOptions = compact(options); | ||
const { page } = compactedOptions; | ||
delete compactedOptions.page; | ||
|
||
window.analytics.page(page, compactedOptions); | ||
}, | ||
|
||
willDestroy() { | ||
$('script[src*="segment.com"]').remove(); | ||
delete window.analytics; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { moduleFor, test } from 'ember-qunit'; | ||
import sinon from 'sinon'; | ||
|
||
let sandbox, config; | ||
|
||
moduleFor('ember-metrics@metrics-adapter:segment', 'segment adapter', { | ||
beforeEach() { | ||
sandbox = sinon.sandbox.create(); | ||
config = { | ||
key: 'SEGMENT_KEY' | ||
}; | ||
}, | ||
afterEach() { | ||
sandbox.restore(); | ||
} | ||
}); | ||
|
||
test('#identify calls analytics with the right arguments', function(assert) { | ||
const adapter = this.subject({ config }); | ||
const stub = sandbox.stub(window.analytics, 'identify', () => { | ||
return true; | ||
}); | ||
adapter.identify({ | ||
distinctId: 123 | ||
}); | ||
assert.ok(stub.calledWith(123), 'it sends the correct arguments'); | ||
}); | ||
|
||
test('#trackEvent returns the correct response shape', function(assert) { | ||
const adapter = this.subject({ config }); | ||
const stub = sandbox.stub(window.analytics, 'track'); | ||
adapter.trackEvent({ | ||
event: 'Signed Up', | ||
category: 'button', | ||
action: 'click', | ||
label: 'nav buttons', | ||
value: 4 | ||
}); | ||
const expectedArguments = { | ||
category: 'button', | ||
action: 'click', | ||
label: 'nav buttons', | ||
value: 4 | ||
}; | ||
|
||
assert.ok(stub.calledWith('Signed Up', expectedArguments), 'track called with proper arguments'); | ||
}); | ||
|
||
test('#trackPage returns the correct response shape', function(assert) { | ||
const adapter = this.subject({ config }); | ||
const stub = sandbox.stub(window.analytics, 'page'); | ||
adapter.trackPage({ | ||
page: '/my-overridden-page?id=1', | ||
title: 'my overridden page' | ||
}); | ||
const expectedArguments = { | ||
title: 'my overridden page' | ||
}; | ||
|
||
assert.ok(stub.calledWith('/my-overridden-page?id=1', expectedArguments), 'page called with proper arguments'); | ||
}); | ||
|
||
test('#trackPage returns the correct response shape', function(assert) { | ||
const adapter = this.subject({ config }); | ||
const stub = sandbox.stub(window.analytics, 'page'); | ||
adapter.trackPage(); | ||
|
||
assert.ok(stub.calledWith(), 'page called with default arguments'); | ||
}); | ||
|
||
test('#alias returns the correct response shape', function(assert) { | ||
const adapter = this.subject({ config }); | ||
const stub = sandbox.stub(window.analytics, 'alias'); | ||
adapter.alias({ alias: 'foo', original: 'bar' }); | ||
|
||
assert.ok(stub.calledWith('foo', 'bar'), 'page called with default arguments'); | ||
}); |