<commit_before>import assert from 'assert';
import sinon from 'sinon';
import reset from './../reset.js';
import after from './../../src/functions/after.js';
import argumentsToArray from './../functions/argumentsToArray.js';
import GoogleAnalytics from './../../src/integrations/GoogleAnalytics.js';
import ddManager from './../../src/ddManager.js';

describe('Integrations: GoogleAnalytics', () => {

  describe('Universal', () => {
    let ga;
    let options = {
      trackingId: 'UA-51485228-7',
      anonymizeIp: true,
      domain: 'auto',
      siteSpeedSampleRate: 42,
      namespace: false
    };

    beforeEach(() => {
      window.digitalData = {
        events: []
      };
      ga = new GoogleAnalytics(window.digitalData, options);
      ddManager.addIntegration('Google Analytics', ga);
    });

    afterEach(() => {
      ga.reset();
      ddManager.reset();
      reset();
    });

    describe('before loading', function () {
      beforeEach(function () {
        sinon.stub(ga, 'load');
      });

      afterEach(function () {
        ga.load.restore();
      });

      describe('#initialize', function () {
        it('should require \'displayfeatures\' if .doubleClick option is `true`', function () {
          ga.setOption('doubleClick', true);
          ddManager.initialize({
            sendViewedPageEvent: false,
          });
          assert.deepEqual(argumentsToArray(window.ga.q[1]), ['require', 'displayfeatures']);
        });

        it('should require "linkid.js" if enhanced link attribution is `true`', function () {
          ga.setOption('enhancedLinkAttribution', true);
          ddManager.initialize({
            sendViewedPageEvent: false,
          });
          assert.deepEqual(argumentsToArray(window.ga.q[1]), ['require', 'linkid', 'linkid.js']);
        });

        it('should create window.GoogleAnalyticsObject', function () {
          assert.ok(!window.GoogleAnalyticsObject);
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          assert.equal(window.GoogleAnalyticsObject, 'ga');
        });

        it('should create window.ga', function () {
          assert.ok(!window.ga);
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          assert.equal(typeof window.ga, 'function');
        });

        it('should create window.ga.l', function () {
          assert.ok(!window.ga);
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          assert.equal(typeof window.ga.l, 'number');
        });

        it('should call window.ga.create with options', function () {
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          assert.deepEqual(argumentsToArray(window.ga.q[0]), ['create', options.trackingId, {
            cookieDomain: 'none',
            siteSpeedSampleRate: options.siteSpeedSampleRate,
            allowLinker: true,
          }]);
        });

        it('should anonymize the ip', function () {
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          assert.deepEqual(argumentsToArray(window.ga.q[1]), ['set', 'anonymizeIp', true]);
        });

        it('should call #load', function () {
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          assert.ok(ga.load.calledOnce);
        });

        it('should not send universal user id by default', function (done) {
          window.digitalData.user = {
            userId: 'baz'
          };
          window.digitalData.page = {};
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          digitalData.events.push({
            name: 'Viewed Page',
            callback: () => {
              assert.notDeepEqual(argumentsToArray(window.ga.q[2]), ['set', 'userId', 'baz']);
              done();
            }
          })
        });

        it('should send universal user id if sendUserId option is true and user.id is truthy', function (done) {
          window.digitalData.user = {
            userId: 'baz'
          };
          window.digitalData.page = {};
          ga.setOption('sendUserId', true);
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          digitalData.events.push({
            name: 'Viewed Page',
            callback: () => {
              assert.deepEqual(argumentsToArray(window.ga.q[3]), ['set', 'userId', 'baz']);
              done();
            }
          });
        });

      });

    });

    // describe('loading', function () {
    //   it('should load', function (done) {
    //     assert.ok(!ga.isLoaded());
    //     ddManager.once('load', () => {
    //       assert.ok(ga.isLoaded());
    //       done();
    //     });
    //     ddManager.initialize({
    //       sendViewedPageEvent: false
    //     });
    //   });
    // });

    describe('after loading', () => {
      beforeEach((done) => {
        ddManager.once('ready', done);
        ddManager.initialize({
          sendViewedPageEvent: false
        });
      });

      // describe('#enrichDigitalData', () => {
      //   it('should add clientId', (done) => {
      //     ga.on('ready', () => {
      //       assert.ok(window.digitalData.user.googleClientId);
      //     });
      //     ga.on('enrich', () => {
      //       assert.ok(window.digitalData.integrations.googleAnalytics);
      //       assert.ok(window.digitalData.integrations.googleAnalytics.clientId);
      //       done();
      //     });
      //   });
      // });
    });<commit_msg>ga tests<commit_after>import assert from 'assert';
import sinon from 'sinon';
import reset from './../reset.js';
import after from './../../src/functions/after.js';
import argumentsToArray from './../functions/argumentsToArray.js';
import GoogleAnalytics from './../../src/integrations/GoogleAnalytics.js';
import ddManager from './../../src/ddManager.js';

describe('Integrations: GoogleAnalytics', () => {

  describe('Universal', () => {
    let ga;
    let options = {
      trackingId: 'UA-51485228-7',
      anonymizeIp: true,
      domain: 'auto',
      siteSpeedSampleRate: 42,
      namespace: false
    };

    beforeEach(() => {
      window.digitalData = {
        events: []
      };
      ga = new GoogleAnalytics(window.digitalData, options);
      ddManager.addIntegration('Google Analytics', ga);
    });

    afterEach(() => {
      ga.reset();
      ddManager.reset();
      reset();
    });

    describe('before loading', function () {
      beforeEach(function () {
        sinon.stub(ga, 'load');
      });

      afterEach(function () {
        ga.load.restore();
      });

      describe('#initialize', function () {
        it('should require \'displayfeatures\' if .doubleClick option is `true`', function () {
          ga.setOption('doubleClick', true);
          ddManager.initialize({
            sendViewedPageEvent: false,
          });
          assert.deepEqual(argumentsToArray(window.ga.q[1]), ['require', 'displayfeatures']);
        });

        it('should require "linkid.js" if enhanced link attribution is `true`', function () {
          ga.setOption('enhancedLinkAttribution', true);
          ddManager.initialize({
            sendViewedPageEvent: false,
          });
          assert.deepEqual(argumentsToArray(window.ga.q[1]), ['require', 'linkid', 'linkid.js']);
        });

        it('should create window.GoogleAnalyticsObject', function () {
          assert.ok(!window.GoogleAnalyticsObject);
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          assert.equal(window.GoogleAnalyticsObject, 'ga');
        });

        it('should create window.ga', function () {
          assert.ok(!window.ga);
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          assert.equal(typeof window.ga, 'function');
        });

        it('should create window.ga.l', function () {
          assert.ok(!window.ga);
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          assert.equal(typeof window.ga.l, 'number');
        });

        it('should call window.ga.create with options', function () {
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          assert.deepEqual(argumentsToArray(window.ga.q[0]), ['create', options.trackingId, {
            cookieDomain: 'none',
            siteSpeedSampleRate: options.siteSpeedSampleRate,
            allowLinker: true,
          }]);
        });

        it('should anonymize the ip', function () {
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          assert.deepEqual(argumentsToArray(window.ga.q[1]), ['set', 'anonymizeIp', true]);
        });

        it('should call #load', function () {
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          assert.ok(ga.load.calledOnce);
        });

        it('should not send universal user id by default', function (done) {
          window.digitalData.user = {
            userId: 'baz'
          };
          window.digitalData.page = {};
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          digitalData.events.push({
            name: 'Viewed Page',
            callback: () => {
              assert.notDeepEqual(argumentsToArray(window.ga.q[2]), ['set', 'userId', 'baz']);
              done();
            }
          })
        });

        it('should send universal user id if sendUserId option is true and user.id is truthy', function (done) {
          window.digitalData.user = {
            userId: 'baz'
          };
          window.digitalData.page = {};
          ga.setOption('sendUserId', true);
          ddManager.initialize({
            sendViewedPageEvent: false
          });
          digitalData.events.push({
            name: 'Viewed Page',
            callback: () => {
              assert.deepEqual(argumentsToArray(window.ga.q[3]), ['set', 'userId', 'baz']);
              done();
            }
          });
        });

      });

    });

    // describe('loading', function () {
    //   it('should load', function (done) {
    //     assert.ok(!ga.isLoaded());
    //     ddManager.once('load', () => {
    //       assert.ok(ga.isLoaded());
    //       done();
    //     });
    //     ddManager.initialize({
    //       sendViewedPageEvent: false
    //     });
    //   });
    // });

    describe('after loading', () => {
      beforeEach((done) => {
        ddManager.once('ready', done);
        ddManager.initialize({
          sendViewedPageEvent: false
        });
      });

      describe('#enrichDigitalData', () => {
        it('should add clientId', (done) => {
          ga.on('ready', () => {
            assert.ok(window.digitalData.user.googleClientId);
          });
          ga.on('enrich', () => {
            assert.ok(window.digitalData.integrations.googleAnalytics);
            assert.ok(window.digitalData.integrations.googleAnalytics.clientId);
            done();
          });
        });
      });
    });
{'fraction_non_alphanumeric': 0.15549024846379908, 'fraction_numerical': 0.003027874254163327, 'mean_word_length': 1.8531504065040652, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4926344', 'n_tokens_mistral': 3118, 'n_tokens_neox': 3001, 'n_words': 539}