0.7.1 • Published 5 years ago

nypr-metrics v0.7.1

Weekly downloads
52
License
MIT
Repository
github
Last release
5 years ago

nypr-metrics

Required Config Values

NameTypeDescription
platformEventsAPIStringUsed by the data-pipeline service to fire platform events.
siteNameStringUsed by the npr-analytics metrics adapter as a data point.
metricsAdaptersArrayHolds adapter specific options

Optional Config

The eager-load-services initializer depends on audio-related addons to be installed. If you are not using it, the initializer maybe opted out by adding disableEagerListenAnalytics: true to your app's config/environment.js file like so:

// config/environment.js
module.exports = function(environment) {
  let ENV = {
    ...
    'nypr-metrics': {
      disableEagerListenAnalytics: true,
    }
    ...
  }
  ...
};

metrics-adapters/npr-analytics

The npr-analytics metrics adapter is designed to work with the ember-metrics addon, which is installed as part of the nypr-metrics addon as well. A config object for this metrics adapter must be added to the metricsAdapters array, like so:

// config/environment.js
module.exports = function(environment) {
  let ENV = {
    ...
    metricsAdapters: [{
      name: 'NprAnalytics',
      config: {
        id: 'UA-18188937-11',
        cookieDomain: 'auto',
        name: 'npr'
      }
    }]
    ...
  };
  ...
};

This will allow the web client to comply with NPR's reporting standards.

Methods

trackPage

Send a page view to the NPR account for the given page path and title. Parameters

NameTypeDescription
optionsObjectAn options object with two keys: page and title
options.pageStringThe current page pathname and any query strings. Must start with a forward slash (/)
options.titleStringThe current page's title

trackEvent

Fires an event to the NPR google analytics back end Parameters

NameTypeDescription
optionsObjectAn options object with three keys: category, action, label
options.categoryStringThe category value for the event
options.actionStringThe action value for the event
options.labelStringThe label value for the event

setDimensions

Sets the required dimensions on the NPR analytics tracker. Only needs to be called on detail routes. The Show, Channel, and Story models found in nypr-publisher-lib have an nprAnalyticsDimensions attribute which can be passed directly to this method.

Parameters

NameTypeDescription
optionsObjectAn options object with one key: nprVals
options.nprValsArrayAn array of values to be set on the NPR tracker. Can accept the nprAnalyticsDimensions attribute directly.

service/data-layer

The data-layer service provides a higher-level API to the dataLayer provided by Google Tag Manager. The API surface is tailored specifically for NYPR use-cases and can be easily extended as new requirements arise.

NOTE This service is injected under the nypr-metrics namespace in order to maintain a clear distinction between the global window.dataLayer and this particular service. So, you'd inject it like this:

dataLayer: service('nypr-metrics/data-layer')

NOTE This service only provides an API to Google Tag Manager's dataLayer. It is up to the end-user to actually implement GTM in their application, perhaps through an instance initializer and environment variable such as this:

// app/instance-initializers/google-tag-manager.js
import config from '../config/environment';

export function initialize(/* appInstance */) {
  if (typeof window !== 'undefined' && config.environment !== 'test') {
    (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer',config.googleTagManager);
  }
}

export default {
  initialize
};

And don't forget to add the preview window iframe to your index.html

<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=<YOUR GTM CONTAINER ID>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

Methods

setForType(type, instance)

Set particular dataLayer variables based on the given type.

Parameters

NameTypeDescription
typeStringThe type of instance from which to pull values for the dataLayer. Determines specific logic and key names get pushed into the dataLayer.
instanceObject/ModelThe thing going into the dataLayer.

clearForType(type)

Clears the dataLayer values for the given type.

Parameters

NameTypeDescription
typeStringEach type sets different values; this ensures only a given types variables are cleared.

setLoggedIn(state)

Set the dataLayer variable to indicate the user's logged-in status.

Parameters

NameTypeDescription
stateBooleantrue for logged in, false if not

setMemberStatus(state)

Set the dataLayer variable to indicate the user's membership status.

Parameters

NameTypeDescription
stateStringNonmember, One-Time Donor, Sustainer

setPageTitle(title)

Set the dataLayer variable for the current page title.

Parameters

NameTypeDescription
titleStringThe page title.

audioTracking(eventName, soundObject)

Fires an audio event via the dataLayer.

Parameters

NameTypeDescription
eventNameStringplay, pause, end, resume
soundObjectSoundA sound object as generated by ember-hifi

push(key, value)

If there isn't a specific method for your use-case, you can use this to set arbitrary dataLayer variables.

Parameters

NameTypeDescription
keyStringKey under which this value will be stored in the dataLayer.
valueAnyThe value to save. Can be any valid JavaScript value, but keep in mind that uses complicated values directly in GA Tags is not supported.

clear(key[, key, ...])

Sets the given key to null in the dataLayer, effectively clearing the value until it is set again. Accepts multiple keys.

Parameters

NameTypeDescription
keyStringThe dataLayer variable name to clear

service/listen-analytics

The listen-analytics service coordinates analytics tracking related to listening activities. It listens to events fired by ember-hifi and makes calls to methods on the data-pipeline service and available ember-metrics adapters. It reads state directly off of ember-hifi.

event queues

Due to a timing issue with firing the audio-ended event, the service queues up events and uses a debounced function to fire off queued events. We do this because when a piece of audio finishes, the <audio/> element will fire first a pause event and then an ended event successively. When the queue flushes, it checks to see if the pending events are in a pause -> ended pattern, and if so it only fires the ended event. There's a queue for both listen actions (sent to data-pipeline) and google analytics.

metadata

A recent update to ember-hifi allows consumers to attach arbitrary data to a metadata property on generated sound objects. listen-analytics expects a few properties on that metadata object for tracking purposes.

  • analytics: an object with arbitrary metadata to be passed to the data-pipeline
  • contentModel: a stream or story ember-data record
  • contentModelType: string representing the ember-data model type
  • playContext: a string representing where in the DOM the click to play this sound originally happened, e.g. 'StoryHeader', 'HomePage', if applicable
  • autoplayChoice Google Analytics events still include the analyticsCode, but this will soon be replaced by a simple string formatted as <show title> | <story title>.`

service/data-pipeline

This service packages up data and fires it off to the platform events microservice. The service is configured to look for a browserId property on itself and include it in outgoing requests. It will also call an authorize method which the client can use to update the passed in fetchOptions.

Client Integration

To integrate with the data-pipeline, there is a simple strategy an upstream app can do. Import the service from the addon and re-export it within the app's namespace, adding a browserId computed and an authorize method.

import DataPipeline from 'nypr-metrics/services/data-pipeline';
import Ember from 'ember';
import computed from 'ember-computed';

export default DataPipeline.extend({
  session: Ember.inject.service(),
  browserId: computed.readOnly('session.data.browserId'),
  authorize(fetchOptions) {
    this.get('session').authorize('authorizer:nypr', (header, value) => {
      fetchOptions.headers[header] = value;
    });

    return fetchOptions;
  }

})

instance-initialzers/eager-load-services

This instance initializer is an affordance for upstream apps so the the listen-analytics service can attach event listeners as soon as possible.

Publishing

This addon is published to npm using semver. See the wiki for more instructions.

0.7.1

5 years ago

0.7.0

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.4

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.4.0-beta.9

6 years ago

0.4.0-beta.8

6 years ago

0.4.0-beta.7

6 years ago

0.4.0-beta.6

6 years ago

0.4.0-beta.5

6 years ago

0.4.0-beta.4

6 years ago

0.4.0-beta.3

6 years ago

0.3.2

6 years ago

0.4.0-beta.2

6 years ago

0.4.0-beta.1

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.2.0-0

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago