mercredi 2 novembre 2016

Integrate Natero with ember-cli

we are using Ember 1.10.1 with Ember CLI 0.2.1, I'm currently trying to integrate Natero into our app, looking at the quickstart guide I thought about using an initializer to do it. It appears to work with Chrome but with other browser I'm getting the error: _na is undefined. The general idea was to dynamically inject the script and wait for the promise to be resolved to set the _naobject on the window.

What would be a better approach to handle this ?

import Ember from 'ember';
/* jshint ignore:start */
import ENV from 'webapp/config/environment';
/* jshint ignore:end */

export function initialize(/* container, application */) {
  /* jshint ignore:start */
    let src = 'http://ift.tt/2fbG10S';

    let injectScript = function (src) {
      return new Ember.RSVP.Promise(function (resolve) {
        var script    = document.createElement('script');
        script.type   = 'text/javascript';
        script.async  = true;
        script.src    = src;
        script.onload = function () {
          resolve();
        };
        document.getElementsByTagName('head')[0].appendChild(script);
      });
    };

    injectScript(src).then(function() {
      /*
       * On each page that loads, initialize the natero analytics js library.
       * The userId and accountId can be set here if known at initialization time.
       * Once the userId/accountId are set they are stored in a cookie for later use,
       * so that they only need to be set once per session.
       */
      // http://ift.tt/2f1m8qA
      // http://ift.tt/2fbFooc
      let authKey  = ENV.natero.authKey,
          apiKey   = ENV.natero.apiKey,
          settings = {
            trackUnload:      true,
            debugUrl:         "http://ift.tt/2f1qrC3" + authKey + "/" + apiKey,
            disableEventSend: false, // disable the sending of events
            debug:            false  // console debug prints
          };

      if (['production', 'prd'].indexOf(ENV.environment) > -1) {
        delete settings['debugUrl'];
      }

      window._na = new na(
        apiKey,
        authKey,
        settings
      );
    });
  /* jshint ignore:end */

  Ember.Router.reopen({
    notifyNatero: function () {
      // http://ift.tt/2fbBxHL
      let currentRoute = Webapp.__container__.lookup('controller:application').get('currentRouteName');
      _na.setModuleId(currentRoute);
    }.on('didTransition')
  });
}

export default {
  name:       'natero',
  initialize: initialize
};




Aucun commentaire:

Enregistrer un commentaire