Skip to content

Commit

Permalink
Merge branch 'master' into update-mixpanel
Browse files Browse the repository at this point in the history
  • Loading branch information
nselikoff committed Jan 4, 2020
2 parents b531af9 + 785cb10 commit 48f35c9
Show file tree
Hide file tree
Showing 27 changed files with 7,983 additions and 14,101 deletions.
55 changes: 31 additions & 24 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
---
language: node_js
node_js:
# we recommend testing addons with the same minimum supported node version as Ember CLI
# so that your addon works for all apps
- 6
- '8'

sudo: false
dist: trusty
Expand All @@ -11,35 +10,43 @@ addons:
chrome: stable

cache:
yarn: true
directories:
- $HOME/.npm
- node_modules

env:
global:
# See https://git.io/vdao3 for details.
- JOBS=1
matrix:
- JOBS=3

jobs:
fail_fast: true
allow_failures:
- env: EMBER_TRY_SCENARIO=ember-canary
- env: EMBER_TRY_SCENARIO=ember-beta

include:
# runs linting and tests with current locked deps

- stage: 'Tests'
name: 'Tests'
script:
- yarn run lint:js
- yarn test

# we recommend new addons test the current and previous LTS
# as well as latest stable release (bonus points to beta/canary)
- EMBER_TRY_SCENARIO=ember-lts-2.12
- EMBER_TRY_SCENARIO=ember-lts-2.16
- EMBER_TRY_SCENARIO=ember-release
- EMBER_TRY_SCENARIO=ember-beta
- EMBER_TRY_SCENARIO=ember-canary
- EMBER_TRY_SCENARIO=ember-default

matrix:
fast_finish: true
allow_failures:
- stage: 'Additional Tests'
env: EMBER_TRY_SCENARIO=ember-lts-2.12
- env: EMBER_TRY_SCENARIO=ember-lts-2.16
- env: EMBER_TRY_SCENARIO=ember-release
- env: EMBER_TRY_SCENARIO=ember-beta
- env: EMBER_TRY_SCENARIO=ember-canary
- env: EMBER_TRY_SCENARIO=ember-default

before_install:
- npm config set spin false
- npm install -g npm@4
- npm --version
stages:
- name: 'Tests'
- name: 'Additional Tests'

script:
- npm run lint:js
# Usually, it's ok to finish the test scenario without reverting
# to the addon's original dependency state, skipping "cleanup".
- node_modules/.bin/ember try:one $EMBER_TRY_SCENARIO --skip-cleanup
- node_modules/.bin/ember try:one $EMBER_TRY_SCENARIO
76 changes: 47 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export default Route.extend({
});
```
[See this example with Native Classes](#native-class-usage)

If you wish to only call a single service, just specify it's name as the first argument:
Expand All @@ -236,50 +237,30 @@ set(this, 'metrics.context.userName', 'Jimbo');
this.metrics.trackPage({ page: 'page/1' }); // { userName: 'Jimbo', page: 'page/1' }
```
## Octane / Native Class usage
## Native Class usage
If you are living on the edge and using an app built with the [Ember Octane Blueprint](https://github.com/ember-cli/ember-octane-blueprint) or otherwise implementing Native Class syntax in your routes, the following example can be used to report route transitions to ember-metrics:
If you are using an app built with the [Ember Octane Blueprint](https://github.com/ember-cli/ember-octane-blueprint) or otherwise implementing Native Class syntax in your routes, the following example can be used to report route transitions to ember-metrics:
```js
// app/routes/application.js
import Route from '@ember/routing/route';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';
import { scheduleOnce } from '@ember/runloop';
export default class ApplicationRoute extends Route {
@service metrics
@service router
@action
didTransition() {
this._super(...arguments);
this._trackPage();
}
constructor() {
super(...arguments);
_trackPage() {
scheduleOnce('afterRender', () => {
const page = this.router.currentURL;
const title = this.router.currentRouteName;
let router = this.router;
router.on('routeDidChange', () => {
const page = router.currentURL;
const title = router.currentRouteName || 'unknown';
this.metrics.trackPage({ page, title });
})
}
}
```
And then in your other routes you can simply add a didTransition action that returns true and thus "bubbles up" to the application route and calls _trackPage:
```js
// app/routes/home.js
import Route from '@ember/routing/route';
import { action } from '@ember/object';
export default class HomeRoute extends Route {
@action
didTransition() {
this._super(...arguments);
return true;
});
}
}
```
Expand Down Expand Up @@ -424,6 +405,43 @@ moduleFor('route:foo', 'Unit | Route | foo', {
});
```

## Contributors

We're grateful to these wonderful contributors who've contributed to `ember-metrics`:

[//]: contributor-faces
<a href="https://github.com/poteto"><img src="https://avatars0.githubusercontent.com/u/1390709?v=4" title="poteto" width="80" height="80"></a>
<a href="https://github.com/kellyselden"><img src="https://avatars1.githubusercontent.com/u/602423?v=4" title="kellyselden" width="80" height="80"></a>
<a href="https://github.com/chrismllr"><img src="https://avatars3.githubusercontent.com/u/9942917?v=4" title="chrismllr" width="80" height="80"></a>
<a href="https://github.com/josemarluedke"><img src="https://avatars0.githubusercontent.com/u/230476?v=4" title="josemarluedke" width="80" height="80"></a>
<a href="https://github.com/mike-north"><img src="https://avatars1.githubusercontent.com/u/558005?v=4" title="mike-north" width="80" height="80"></a>
<a href="https://github.com/jelhan"><img src="https://avatars3.githubusercontent.com/u/4965703?v=4" title="jelhan" width="80" height="80"></a>
<a href="https://github.com/denneralex"><img src="https://avatars1.githubusercontent.com/u/5065602?v=4" title="denneralex" width="80" height="80"></a>
<a href="https://github.com/CvX"><img src="https://avatars1.githubusercontent.com/u/66961?v=4" title="CvX" width="80" height="80"></a>
<a href="https://github.com/sly7-7"><img src="https://avatars1.githubusercontent.com/u/1826661?v=4" title="sly7-7" width="80" height="80"></a>
<a href="https://github.com/tyleryasaka"><img src="https://avatars1.githubusercontent.com/u/6504519?v=4" title="tyleryasaka" width="80" height="80"></a>
<a href="https://github.com/opsb"><img src="https://avatars2.githubusercontent.com/u/46232?v=4" title="opsb" width="80" height="80"></a>
<a href="https://github.com/Artmann"><img src="https://avatars3.githubusercontent.com/u/91954?v=4" title="Artmann" width="80" height="80"></a>
<a href="https://github.com/colinhoernig"><img src="https://avatars1.githubusercontent.com/u/195992?v=4" title="colinhoernig" width="80" height="80"></a>
<a href="https://github.com/gmurphey"><img src="https://avatars3.githubusercontent.com/u/373721?v=4" title="gmurphey" width="80" height="80"></a>
<a href="https://github.com/cibernox"><img src="https://avatars2.githubusercontent.com/u/265339?v=4" title="cibernox" width="80" height="80"></a>
<a href="https://github.com/reidab"><img src="https://avatars2.githubusercontent.com/u/13192?v=4" title="reidab" width="80" height="80"></a>
<a href="https://github.com/locks"><img src="https://avatars1.githubusercontent.com/u/32344?v=4" title="locks" width="80" height="80"></a>
<a href="https://github.com/achambers"><img src="https://avatars0.githubusercontent.com/u/416724?v=4" title="achambers" width="80" height="80"></a>
<a href="https://github.com/XrXr"><img src="https://avatars2.githubusercontent.com/u/6457510?v=4" title="XrXr" width="80" height="80"></a>
<a href="https://github.com/alexlafroscia"><img src="https://avatars2.githubusercontent.com/u/1645881?v=4" title="alexlafroscia" width="80" height="80"></a>
<a href="https://github.com/balinterdi"><img src="https://avatars2.githubusercontent.com/u/5022?v=4" title="balinterdi" width="80" height="80"></a>
<a href="https://github.com/blimmer"><img src="https://avatars1.githubusercontent.com/u/630449?v=4" title="blimmer" width="80" height="80"></a>
<a href="https://github.com/ballPointPenguin"><img src="https://avatars0.githubusercontent.com/u/35609?v=4" title="ballPointPenguin" width="80" height="80"></a>
<a href="https://github.com/bdelaforest"><img src="https://avatars2.githubusercontent.com/u/7151559?v=4" title="bdelaforest" width="80" height="80"></a>
<a href="https://github.com/billpull"><img src="https://avatars1.githubusercontent.com/u/854970?v=4" title="billpull" width="80" height="80"></a>
<a href="https://github.com/noslouch"><img src="https://avatars1.githubusercontent.com/u/2090382?v=4" title="noslouch" width="80" height="80"></a>
<a href="https://github.com/wecc"><img src="https://avatars2.githubusercontent.com/u/708205?v=4" title="wecc" width="80" height="80"></a>
<a href="https://github.com/cigoe"><img src="https://avatars3.githubusercontent.com/u/518239?v=4" title="cigoe" width="80" height="80"></a>
<a href="https://github.com/dcyriller"><img src="https://avatars2.githubusercontent.com/u/6677373?v=4" title="dcyriller" width="80" height="80"></a>

[//]: contributor-faces

## Installation

* `git clone` this repository
Expand Down
2 changes: 2 additions & 0 deletions addon/metrics-adapters/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ export default emberObject.extend({
trackEvent() {},
trackPage() {},
alias() {}
}).reopenClass({
supportsFastBoot: false
});
34 changes: 15 additions & 19 deletions addon/metrics-adapters/facebook-pixel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import canUseDOM from '../utils/can-use-dom';
import objectTransforms from '../utils/object-transforms';
import removeFromDOM from '../utils/remove-from-dom';
import BaseAdapter from './base';
Expand All @@ -18,26 +17,26 @@ export default BaseAdapter.extend({

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

if (canUseDOM) {
/* eslint-disable */
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
/* eslint-enable */
if (window.fbq) {
return;
}

window.fbq('init', id);
/* eslint-disable */
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
/* eslint-enable */

// Leave this call due to Facebook API docs
// https://developers.facebook.com/docs/facebook-pixel/api-reference#setup
this.trackEvent({ event: 'PageView' });
}
window.fbq('init', id);

// Leave this call due to Facebook API docs
// https://developers.facebook.com/docs/facebook-pixel/api-reference#setup
this.trackEvent({ event: 'PageView' });
},

trackEvent(options = {}) {
if (!canUseDOM) { return; }

const compactedOptions = compact(options);
const { event } = compactedOptions;

Expand All @@ -50,13 +49,10 @@ export default BaseAdapter.extend({
},

trackPage(options = {}) {
if (!canUseDOM) { return; }

window.fbq('track', 'PageView', options);
},

willDestroy() {
if (!canUseDOM) { return; }
removeFromDOM('script[src*="fbevents.js"]');

delete window.fbq;
Expand Down
73 changes: 28 additions & 45 deletions addon/metrics-adapters/google-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { isPresent } from '@ember/utils';
import { assert } from '@ember/debug';
import { get } from '@ember/object';
import { capitalize } from '@ember/string';
import canUseDOM from '../utils/can-use-dom';
import objectTransforms from '../utils/object-transforms';
import removeFromDOM from '../utils/remove-from-dom';
import BaseAdapter from './base';
Expand All @@ -17,59 +16,47 @@ export default BaseAdapter.extend({

init() {
const config = assign({}, get(this, 'config'));
const { id, sendHitTask, trace, require } = config;
let { debug } = config;
const { id, sendHitTask, trace, require, debug } = config;

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

delete config.id;
delete config.require;

if (debug) { delete config.debug; }
if (sendHitTask) { delete config.sendHitTask; }
if (trace) { delete config.trace; }
delete config.debug;
delete config.sendHitTask;
delete config.trace;

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

if (canUseDOM) {

/* eslint-disable */
(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',`https://www.google-analytics.com/analytics${debug ? '_debug' : ''}.js`,'ga');
/* eslint-enable */

if (trace === true) {
window.ga_debug = { trace: true };
}
/* eslint-disable */
(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',`https://www.google-analytics.com/analytics${debug ? '_debug' : ''}.js`,'ga');
/* eslint-enable */

if (hasOptions) {
window.ga('create', id, config);
} else {
window.ga('create', id, 'auto');
}
if (trace === true) {
window.ga_debug = { trace: true };
}

if (require) {
require.forEach((plugin) => {
window.ga('require', plugin);
});
}
window.ga('create', id, hasOptions ? config : 'auto');

if (sendHitTask === false) {
window.ga('set', 'sendHitTask', null);
}
if (require) {
require.forEach((plugin) => {
window.ga('require', plugin);
});
}

if (sendHitTask === false) {
window.ga('set', 'sendHitTask', null);
}
},

identify(options = {}) {
const compactedOptions = compact(options);
const { distinctId } = compactedOptions;

if (canUseDOM) {
window.ga('set', 'userId', distinctId);
}
window.ga('set', 'userId', distinctId);
},

trackEvent(options = {}) {
Expand All @@ -93,9 +80,7 @@ export default BaseAdapter.extend({
}

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

return event;
},
Expand All @@ -105,20 +90,18 @@ export default BaseAdapter.extend({
const sendEvent = { hitType: 'pageview' };
const event = assign(sendEvent, compactedOptions);

if (canUseDOM) {
for (let key in compactedOptions) {
if (compactedOptions.hasOwnProperty(key)) {
window.ga('set', key, compactedOptions[key]);
}
for (let key in compactedOptions) {
if (compactedOptions.hasOwnProperty(key)) {
window.ga('set', key, compactedOptions[key]);
}
window.ga('send', sendEvent);
}

window.ga('send', sendEvent);

return event;
},

willDestroy() {
if (!canUseDOM) { return; }
removeFromDOM('script[src*="google-analytics"]');

delete window.ga;
Expand Down
Loading

0 comments on commit 48f35c9

Please sign in to comment.