Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
ember-metrics
Advanced tools
Send data to multiple analytics integrations without re-implementing new API
Send data to multiple analytics services without re-implementing new API
This addon adds a simple metrics
service to your app that makes it simple to send data to multiple analytics services without having to implement a new API each time.
Using this addon, you can easily use bundled adapters for various analytics services, and one API to track events, page views, and more. When you decide to add another analytics service to your stack, all you need to do is add it to your configuration, and that's it!
Writing your own adapters for currently unsupported analytics services is easy too. If you'd like to then share it with the world, submit a pull request and we'll add it to the bundled adapters.
GoogleAnalytics
id
: Property ID, e.g. UA-XXXX-Y
Mixpanel
token
: Mixpanel tokenGoogleTagManager
id
: Container ID, e.g. GTM-XXXX
dataLayer
: An array containing a single POJO of information, e.g.:
dataLayer = [{
'pageCategory': 'signup',
'visitorType': 'high-value'
}];
Segment
key
: Segment keyPiwik
piwikUrl
: Tracker URLsiteId
: Site IdKISSMetrics
(WIP)CrazyEgg
(WIP)For Ember CLI >= 0.2.3
:
ember install ember-metrics
For Ember CLI < 0.2.3
:
ember install:addon ember-metrics
This addon is tested against the release
, beta
, and canary
channels, as well as ~1.11.0
, and 1.12.1
.
To setup, you should first configure the service through config/environment
:
module.exports = function(environment) {
var ENV = {
metricsAdapters: [
{
name: 'GoogleAnalytics',
environments: ['development', 'production'],
config: {
id: 'UA-XXXX-Y'
}
},
{
name: 'Mixpanel',
environments: ['production'],
config: {
token: '0f76c037-4d76-4fce-8a0f-a9a8f89d1453'
}
},
{
name: 'Segment',
environments: ['production'],
config: {
key: '4fce-8a0f-a9a8f89d1453'
}
},
{
name: 'Piwik',
environments: ['production'],
config: {
piwikUrl: 'http://piwik.my.com',
siteId: 42
}
},
{
name: 'LocalAdapter',
environments: ['all'], // default
config: {
foo: 'bar'
}
}
]
}
}
Adapter names are PascalCased. Refer to the list of supported adapters above for more information.
The metricsAdapters
option in ENV
accepts an array of objects containing settings for each analytics service you want to use in your app in the following format:
/**
* @param {String} name Adapter name
* @param {Array} environments Environments that the adapter should be activated in
* @param {Object} config Configuration options for the service
*/
{
name: 'Analytics',
environments: ['all'],
config: {}
}
Values in the config
portion of the object are dependent on the adapter. If you're writing your own adapter, you will be able to retrieve the options passed into it:
// Example adapter
export default BaseAdapter.extend({
init() {
const { apiKey, options } = Ember.get(this, 'config');
this.setupService(apiKey);
this.setOptions(options);
}
});
To only activate adapters in specific environments, you can add an array of environment names to the config, as the environments
key. Valid environments are:
development
test,
production
all
(default, will be activated in all environments)If you're using ember-cli-content-security-policy, you'll need to modify the content security policy to allow loading of any remote scripts. In config/environment.js
, add this to the ENV
hash (modify as necessary):
// example for loading Google Analytics
contentSecurityPolicy: {
'default-src': "'none'",
'script-src': "'self' www.google-analytics.com",
'font-src': "'self'",
'connect-src': "'self' www.google-analytics.com",
'img-src': "'self'",
'style-src': "'self'",
'media-src': "'self'"
}
In order to use the addon, you must first configure it, then inject it into any Object registered in the container that you wish to track. For example, you can call a trackPage
event across all your analytics services whenever you transition into a route, like so:
// app/router.js
import Ember from 'ember';
import config from './config/environment';
const Router = Ember.Router.extend({
location: config.locationType,
metrics: Ember.inject.service(),
didTransition() {
this._super(...arguments);
this._trackPage();
},
_trackPage() {
Ember.run.scheduleOnce('afterRender', this, () => {
const page = document.location.pathname;
const title = this.getWithDefault('currentRouteName', 'unknown');
Ember.get(this, 'metrics').trackPage({ page, title });
});
}
});
export default Router.map(/* ... */);
If you wish to only call a single service, just specify it's name as the first argument:
// only invokes the `trackPage` method on the `GoogleAnalyticsAdapter`
metrics.trackPage('GoogleAnalytics', {
title: 'My Awesome App'
});
Often, you may want to include information like the current user's name with every event or page view that's tracked. Any properties that are set on metrics.context
will be merged into options for every Service call.
Ember.set(this, 'metrics.context.userName', 'Jimbo');
Ember.get(this, 'metrics').trackPage({ page: 'page/1' }); // { userName: 'Jimbo', page: 'page/1' }
There are 4 main methods implemented by the service, with the same argument signature:
trackPage([analyticsName], options)
This is commonly used by analytics services to track page views. Due to the way Single Page Applications implement routing, you will need to call this on the activate
hook of each route to track all page views.
trackEvent([analyticsName], options)
This is a general purpose method for tracking a named event in your application.
identify([analyticsName], options)
For analytics services that have identification functionality.
alias([analyticsName], options)
For services that implement it, this method notifies the analytics service that an anonymous user now has a unique identifier.
If an adapter implements specific methods you wish to call, then you can use invoke
invoke(method, [analyticsName], options)
metrics.invoke('trackLink', 'Piwik', { url: 'my_favorite_link' , linkType: 'download' });
If your app implements dynamic API keys for various analytics integration, you can defer the initialization of the adapters. Instead of configuring ember-metrics
through config/environment
, you can call the following from any Object registered in the container:
import Ember from 'ember';
export default Ember.Route.extend({
metrics: Ember.inject.service(),
afterModel(model) {
const metrics = Ember.get(this, 'metrics');
const id = Ember.get(model, 'googleAnalyticsKey');
metrics.activateAdapters([
{
name: 'GoogleAnalytics',
environments: ['all'],
config: {
id
}
}
]);
}
});
Because activateAdapters
is idempotent, you can call it as many times as you'd like. However, it will not reinstantiate existing adapters.
Since ember-metrics now automatically removes all unused adapters, it's also important to force the inclusion of the adapter via config/environment
. NOTE: If the adapter is already defined in the metricsAdapters
array of config/environment
then this step is not necessary.
// config/environment
module.exports = function(environment) {
var ENV = {
'ember-metrics': {
includeAdapters: ['google-analytics']
}
};
return ENV;
First, generate a new Metrics Adapter:
$ ember generate metrics-adapter foo-bar
This creates app/metrics-adapters/foo-bar.js
and a unit test at tests/unit/metrics-adapters/foo-bar-test.js
, which you should now customize.
The standard contracts are optionally defined, but init
and willDestroy
must be implemented by your adapter.
This method is called when an adapter is activated by the service. It is responsible for adding the required script tag used by the integration, and for initializing it.
When the adapter is destroyed, it should remove its script tag and property. This is usually defined on the window
.
Once you have implemented your adapter, you can add it to your app's config, like so:
module.exports = function(environment) {
var ENV = {
metricsAdapters: [
{
name: 'MyAdapter',
environments: ['all'],
config: {
secret: '29fJs90qnfEa',
options: {
foo: 'bar'
}
}
}
]
}
}
For unit tests, you will need to specify the adapters in use under needs
, like so:
moduleFor('route:foo', 'Unit | Route | foo', {
needs: [
'service:metrics',
'ember-metrics@metrics-adapter:google-analytics', // bundled adapter
'ember-metrics@metrics-adapter:mixpanel', // bundled adapter
'metrics-adapter:local-dummy-adapter' // local adapter
]
});
git clone
this repositorynpm install
bower install
ember server
ember test
ember test --server
ember build
For more information on using ember-cli, visit http://www.ember-cli.com/.
FAQs
Send data to multiple analytics integrations without re-implementing new API
The npm package ember-metrics receives a total of 4,597 weekly downloads. As such, ember-metrics popularity was classified as popular.
We found that ember-metrics demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.