{"size": 10437, "ext": "js", "max_stars_repo_path": "test/integrations/google-analytics.js", "max_stars_repo_name": "Awesomatic/analytics.js", "max_stars_repo_stars_event_min_datetime": "2020-04-22T15:05:58.000Z", "max_stars_repo_stars_event_max_datetime": "2020-04-22T15:05:58.000Z", "max_issues_repo_path": "test/integrations/google-analytics.js", "max_issues_repo_name": "Awesomatic/analytics.js", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/integrations/google-analytics.js", "max_forks_repo_name": "Awesomatic/analytics.js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.4657894737, "max_line_length": 129, "alphanum_fraction": 0.6327488742}

describe('Google Analytics', function () {

var analytics = window.analytics || require('analytics')
  , assert = require('assert')
  , sinon = require('sinon')
  , when = require('when');

describe('Universal', function () {

var settings = {
  anonymizeIp: true,
  domain: 'none',
  siteSpeedSampleRate: 42,
  trackingId: 'UA-27033709-12'
};

before(function (done) {
  // setup a stub to listen on
  window.ga = function () {
    window.ga.q || (window.ga.q = []);
    window.ga.q.push(arguments);
  };
  this.gaSpy = sinon.spy(window, 'ga');

  this.timeout(10000);
  this.spy = sinon.spy();
  analytics.ready(this.spy);
  analytics.initialize({ 'Google Analytics': settings });
  this.integration = analytics._integrations['Google Analytics'];
  this.options = this.integration.options;
  var stub = window.ga;
  when(function () { return window.ga != stub; }, done);
});

describe('#name', function () {
  it('Google Analytics', function () {
    assert(this.integration.name == 'Google Analytics');
  });
});

describe('#key', function () {
  it('trackingId', function () {
    assert(this.integration.key == 'trackingId');
  });
});

describe('#defaults', function () {
  it('anonymizeIp', function () {
    assert(this.integration.defaults.anonymizeIp === false);
  });

  it('classic', function () {
    assert(this.integration.defaults.classic === false);
  });

  it('domain', function () {
    assert(this.integration.defaults.domain === 'none');
  });

  it('doubleClick', function () {
    assert(this.integration.defaults.doubleClick === false);
  });

  it('enhancedLinkAttribution', function () {
    assert(this.integration.defaults.enhancedLinkAttribution === false);
  });

  it('ignoreReferrer', function () {
    assert(this.integration.defaults.ignoreReferrer === null);
  });

  it('initialPageview', function () {
    assert(this.integration.defaults.initialPageview === true);
  });

  it('siteSpeedSampleRate', function () {
    assert(this.integration.defaults.siteSpeedSampleRate === null);
  });

  it('trackingId', function () {
    assert(this.integration.defaults.trackingId === '');
  });
});

describe('#initialize', function () {
  after(function () {
    this.gaSpy.restore();
  });

  it('should call ready', function () {
    assert(this.spy.called);
  });

  it('should store options', function () {
    assert(this.options.trackingId == settings.trackingId);
  });

  it('should define a global tracker', function () {
    assert('function' == typeof window.ga);
    assert('ga' == window.GoogleAnalyticsObject);
  });

  it('should pass domain option to Google Analytics', function () {
    assert(this.gaSpy.args[1][2].cookieDomain == settings.domain);
  });

  it('should pass sample rate option to Google Analytics', function () {
    assert(this.gaSpy.args[1][2].siteSpeedSampleRate == settings.siteSpeedSampleRate);
  });

  it('should pass anonymize ip option to Google Analytics', function () {
    assert(this.gaSpy.calledWith('set', 'anonymizeIp', true));
  });

  it('should track a pageview with the canonical url');
});

describe('#track', function () {
  beforeEach(function () {
    this.spy = sinon.spy(window, 'ga');
  });

  afterEach(function () {
    this.spy.restore();
  });

  it('should send an event', function () {
    analytics.track('event');
    assert(this.spy.calledWith('send', 'event', {
      hitType: 'event',
      eventCategory: 'All',
      eventAction: 'event',
      eventLabel: undefined,
      eventValue: 0,
      nonInteraction: undefined
    }));
  });

  it('should send a category property', function () {
    analytics.track('event', { category: 'Category' });
    assert(this.spy.calledWith('send', 'event', {
      hitType: 'event',
      eventCategory: 'Category',
      eventAction: 'event',
      eventLabel: undefined,
      eventValue: 0,
      nonInteraction: undefined
    }));
  });

  it('should send a label property', function () {
    analytics.track('event', { label: 'label' });
    assert(this.spy.calledWith('send', 'event', {
      hitType: 'event',
      eventCategory: 'All',
      eventAction: 'event',
      eventLabel: 'label',
      eventValue: 0,
      nonInteraction: undefined
    }));
  });

  it('should send a rounded value property', function () {
    analytics.track('event', { value: 1.1 });
    assert(this.spy.calledWith('send', 'event', {
      hitType: 'event',
      eventCategory: 'All',
      eventAction: 'event',
      eventLabel: undefined,
      eventValue: 1,
      nonInteraction: undefined
    }));
  });

  it('should prefer a rounded revenue property', function () {
    analytics.track('event', { revenue: 9.99 });
    assert(this.spy.calledWith('send', 'event', {
      hitType: 'event',
      eventCategory: 'All',
      eventAction: 'event',
      eventLabel: undefined,
      eventValue: 10,
      nonInteraction: undefined
    }));
  });

  it('should send a non-interaction property', function () {
    analytics.track('event', { noninteraction: true });
    assert(this.spy.calledWith('send', 'event', {
      hitType: 'event',
      eventCategory: 'All',
      eventAction: 'event',
      eventLabel: undefined,
      eventValue: 0,
      nonInteraction: true
    }));
  });

  it('should send a non-interaction option', function () {
    analytics.track('event', {}, { noninteraction: true });
    assert(this.spy.calledWith('send', 'event', {
      hitType: 'event',
      eventCategory: 'All',
      eventAction: 'event',
      eventLabel: undefined,
      eventValue: 0,
      nonInteraction: true
    }));
  });
});

describe('#pageview', function () {
  beforeEach(function () {
    this.spy = sinon.spy(window, 'ga');
  });

  afterEach(function () {
    this.spy.restore();
  });

  it('should send a pageview', function () {
    analytics.pageview();
    assert(this.spy.calledWith('send', 'pageview', {
      hitType: 'pageview',
      page: undefined
    }));
  });

  it('should send a url', function () {
    analytics.pageview('/path');
    assert(this.spy.calledWith('send', 'pageview', {
      hitType: 'pageview',
      page: '/path'
    }));
  });
});

});

describe('Classic', function () {

var settings = {
  anonymizeIp: true,
  classic: true,
  domain: 'none',
  enhancedLinkAttribution: true,
  ignoreReferrer: ['domain.com', 'www.domain.com'],
  siteSpeedSampleRate: 42,
  trackingId: 'UA-27033709-5'
};

before(function (done) {
  // setup a stub to listen on
  window._gaq = [];
  this.gaStub = sinon.stub(window._gaq, 'push');

  this.timeout(10000);
  this.spy = sinon.spy();
  analytics.ready(this.spy);
  analytics.initialize({ 'Google Analytics': settings });
  this.integration = analytics._integrations['Google Analytics'];
  this.options = this.integration.options;
  var stub = window._gaq.push;
  when(function () { return window._gaq.push != stub; }, done);
});

describe('#initialize', function () {
  after(function () {
    this.gaStub.restore();
  });

  it('should call ready', function () {
    assert(this.spy.called);
  });

  it('should store options', function () {
    assert(this.options.trackingId == settings.trackingId);
  });

  it('should define a queue', function () {
    assert(window._gaq);
  });

  it('should pass the tracking id to Google Analytics', function () {
    assert(this.gaStub.calledWith(['_setAccount', settings.trackingId]));
  });

  it('should pass domain option to Google Analytics', function () {
    assert(this.gaStub.calledWith(['_setDomainName', settings.domain]));
  });

  it('should pass enhanced link attribution option to Google Analytics', function () {
    assert(this.gaStub.calledWith(['_require', 'inpage_linkid', 'http://www.google-analytics.com/plugins/ga/inpage_linkid.js']));
  });

  it('should pass sample rate option to Google Analytics', function () {
    assert(this.gaStub.calledWith(['_setSiteSpeedSampleRate', settings.siteSpeedSampleRate]));
  });

  it('should pass anonymize ip option to Google Analytics', function () {
    assert(this.gaStub.calledWith(['_gat._anonymizeIp']));
  });

  it('should pass ignored referrers option to Google Analytics', function () {
    assert(this.gaStub.calledWith(['_addIgnoredRef', settings.ignoreReferrer[0]]));
    assert(this.gaStub.calledWith(['_addIgnoredRef', settings.ignoreReferrer[1]]));
  });

  it('should track a pageview with the canonical url');
});

describe('#track', function () {
  beforeEach(function () {
    this.stub = sinon.stub(window._gaq, 'push');
  });

  afterEach(function () {
    this.stub.restore();
  });

  it('should send an event', function () {
    analytics.track('event');
    assert(this.stub.calledWith(['_trackEvent', 'All', 'event', undefined, 0, undefined]));
  });

  it('should send a category property', function () {
    analytics.track('event', { category: 'Category' });
    assert(this.stub.calledWith(['_trackEvent', 'Category', 'event', undefined, 0, undefined]));
  });

  it('should send a label property', function () {
    analytics.track('event', { label: 'label' });
    assert(this.stub.calledWith(['_trackEvent', 'All', 'event', 'label', 0, undefined]));
  });

  it('should send a rounded value property', function () {
    analytics.track('event', { value: 1.1 });
    assert(this.stub.calledWith(['_trackEvent', 'All', 'event', undefined, 1, undefined]));
  });

  it('should prefer a rounded revenue property', function () {
    analytics.track('event', { revenue: 9.99 });
    assert(this.stub.calledWith(['_trackEvent', 'All', 'event', undefined, 10, undefined]));
  });

  it('should send a non-interaction property', function () {
    analytics.track('event', { noninteraction: true });
    assert(this.stub.calledWith(['_trackEvent', 'All', 'event', undefined, 0, true]));
  });

  it('should send a non-interaction option', function () {
    analytics.track('event', {}, { noninteraction: true });
    assert(this.stub.calledWith(['_trackEvent', 'All', 'event', undefined, 0, true]));
  });
});

describe('#pageview', function () {
  beforeEach(function () {
    this.stub = sinon.stub(window._gaq, 'push');
  });

  afterEach(function () {
    this.stub.restore();
  });

  it('should send a pageview', function () {
    analytics.pageview();
    assert(this.stub.calledWith(['_trackPageview', undefined]));
  });

  it('should send a url', function () {
    analytics.pageview('/path');
    assert(this.stub.calledWith(['_trackPageview', '/path']));
  });
});

});

});