<commit_before>import 'autotrack/lib/plugins/clean-url-tracker';
import 'autotrack/lib/plugins/event-tracker';
import 'autotrack/lib/plugins/media-query-tracker';
import 'autotrack/lib/plugins/outbound-link-tracker';
import 'autotrack/lib/plugins/page-visibility-tracker';
import 'autotrack/lib/plugins/social-widget-tracker';


import {
  getStoredData,
  getStoredTrackerData,
  setStoredData,
  setStoredTrackerData
} from './analytics-storage';


/**
 * A global list of tracker object, randomized to ensure no one tracker
 * data is always sent first.
 */
const ALL_TRACKERS = shuffleArray([
  {name: 't0', trackingId: 'UA-40829935-1'},
  {name: 'testing', trackingId: 'UA-40829935-4'}
]);
const PROD_TRACKERS = ALL_TRACKERS.filter(({name}) => !/test/.test(name));
const TEST_TRACKERS = ALL_TRACKERS.filter(({name}) => /test/.test(name));


const NULL_VALUE = '(not set)';


const metrics = {
  PAGE_VISIBLE: 'metric1',
  PAGE_HIDDEN: 'metric2'
};


const dimensions = {
  BREAKPOINT: 'dimension1',
  RESOLUTION: 'dimension2',
  ORIENTATION: 'dimension3',
  HIT_SOURCE: 'dimension4',
  URL_QUERY_PARAMS: 'dimension5',
  METRIC_VALUE: 'dimension6',
  CLIENT_ID: 'dimension7',
  PREVIOUS_HIT_INDEX: 'dimension8',
  PREVIOUS_HIT_PAYLOAD: 'dimension9',
  HIT_TYPE: 'dimension10',
  HIT_UUID: 'dimension11'
};


// The command queue proxies.
let gaAll = createGaProxy(ALL_TRACKERS);
let gaProd = createGaProxy(PROD_TRACKERS);
let gaTest = createGaProxy(TEST_TRACKERS);


// Delays running any analytics until after the load event
// to ensure beacons don't block resources.
window.onload = function() {
  createTrackers();
  trackErrors();
  setDefaultDimensionValues();
  requirePlugins();
  trackClientId();
  initSessionControl();
  sendInitialPageview();
};


function createTrackers() {
  let fields = {siteSpeedSampleRate: 10};
  let data = getStoredData();
  for (let tracker of PROD_TRACKERS) {
    window.ga('create', tracker.trackingId, 'auto', tracker.name, fields);
  }
  window.ga(function() {
    let tracker = window.ga.getAll()[0];
    data.clientId = tracker.get('clientId') || data.clientId;
    setStoredData(data);

    for (let tracker of TEST_TRACKERS) {
      if (data.clientId) fields.clientId = data.clientId;
      if (window.localStorage) fields.storage = 'none';
      window.ga('create', tracker.trackingId, 'auto', tracker.name, fields);
    }
  });
}


function trackErrors() {
  // Errors that have occurred prior to this script running are stored on
  // the `q` property of the window.onerror function.
  let errorQueue = window.onerror.q || [];

  // Override the temp `onerror()` handler to now send hits to GA.
  window.onerror = function(msg, file, line, col, error) {
    gaAll('send', 'event', {
      eventCategory: 'Script',
      eventAction: 'uncaught error',
      eventLabel: error ? error.stack : `${msg}\n${file}:${line}:${col}`
    });
  };

  // Replay any stored errors in the queue.
  for (let error of errorQueue) {
    window.onerror(...error);
  }
}


function setDefaultDimensionValues() {
  Object.keys(dimensions).forEach((key) => {
    gaAll('set', dimensions[key], NULL_VALUE);
  });
}


function requirePlugins() {
  gaProd('require', 'cleanUrlTracker', {
    stripQuery: true,
    queryDimensionIndex: getDefinitionIndex(dimensions.URL_QUERY_PARAMS),
    indexFilename: 'index.html',
    trailingSlash: 'add'
  });
  gaProd('require', 'eventTracker');
  gaProd('require', 'mediaQueryTracker', {
    definitions: [
      {
        name: 'Breakpoint',
        dimensionIndex: 1,
        items: [
          {name: 'xs', media: 'all'},
          {name: 'sm', media: '(min-width: 384px)'},
          {name: 'md', media: '(min-width: 576px)'},
          {name: 'lg', media: '(min-width: 768px)'}
        ]
      },
      {
        name: 'Resolution',
        dimensionIndex: 2,
        items: [
          {name: '1x',   media: 'all'},
          {name: '1.5x', media: '(-webkit-min-device-pixel-ratio: 1.5), ' +
                                '(min-resolution: 144dpi)'},
          {name: '2x',   media: '(-webkit-min-device-pixel-ratio: 2), ' +
                                '(min-resolution: 192dpi)'},
        ]
      },
      {
        name: 'Orientation',
        dimensionIndex: 3,
        items: [
          {name: 'landscape', media: '(orientation: landscape)'},
          {name: 'portrait',  media: '(orientation: portrait)'}
        ]
      }
    ]
  });
  gaProd('require', 'outboundLinkTracker');
  gaProd('require', 'pageVisibilityTracker', {
    visibleMetricIndex: getDefinitionIndex(metrics.PAGE_VISIBLE),
    hiddenMetricIndex: getDefinitionIndex(metrics.PAGE_HIDDEN),
    fieldsObj: {
      nonInteraction: null, // Ensure all events are interactive.
      [dimensions.HIT_SOURCE]: 'pageVisibilityTracker'
    },
    hitFilter: function(model) {
      model.set(dimensions.METRIC_VALUE, String(model.get('eventValue')), true);
    }
  });
  gaProd('require', 'socialWidgetTracker');
}<commit_msg>Enable select autotrack plugins on test tracker<commit_after>import 'autotrack/lib/plugins/clean-url-tracker';
import 'autotrack/lib/plugins/event-tracker';
import 'autotrack/lib/plugins/media-query-tracker';
import 'autotrack/lib/plugins/outbound-link-tracker';
import 'autotrack/lib/plugins/page-visibility-tracker';
import 'autotrack/lib/plugins/social-widget-tracker';


import {
  getStoredData,
  getStoredTrackerData,
  setStoredData,
  setStoredTrackerData
} from './analytics-storage';


/**
 * A global list of tracker object, randomized to ensure no one tracker
 * data is always sent first.
 */
const ALL_TRACKERS = shuffleArray([
  {name: 't0', trackingId: 'UA-40829935-1'},
  {name: 'testing', trackingId: 'UA-40829935-4'}
]);
const PROD_TRACKERS = ALL_TRACKERS.filter(({name}) => !/test/.test(name));
const TEST_TRACKERS = ALL_TRACKERS.filter(({name}) => /test/.test(name));


const NULL_VALUE = '(not set)';


const metrics = {
  PAGE_VISIBLE: 'metric1',
  PAGE_HIDDEN: 'metric2'
};


const dimensions = {
  BREAKPOINT: 'dimension1',
  RESOLUTION: 'dimension2',
  ORIENTATION: 'dimension3',
  HIT_SOURCE: 'dimension4',
  URL_QUERY_PARAMS: 'dimension5',
  METRIC_VALUE: 'dimension6',
  CLIENT_ID: 'dimension7',
  PREVIOUS_HIT_INDEX: 'dimension8',
  PREVIOUS_HIT_PAYLOAD: 'dimension9',
  HIT_TYPE: 'dimension10',
  HIT_UUID: 'dimension11'
};


// The command queue proxies.
let gaAll = createGaProxy(ALL_TRACKERS);
let gaProd = createGaProxy(PROD_TRACKERS);
let gaTest = createGaProxy(TEST_TRACKERS);


// Delays running any analytics until after the load event
// to ensure beacons don't block resources.
window.onload = function() {
  createTrackers();
  trackErrors();
  setDefaultDimensionValues();
  requirePlugins();
  trackClientId();
  initSessionControl();
  sendInitialPageview();
};


function createTrackers() {
  let fields = {siteSpeedSampleRate: 10};
  let data = getStoredData();
  for (let tracker of PROD_TRACKERS) {
    window.ga('create', tracker.trackingId, 'auto', tracker.name, fields);
  }
  window.ga(function() {
    let tracker = window.ga.getAll()[0];
    data.clientId = tracker.get('clientId') || data.clientId;
    setStoredData(data);

    for (let tracker of TEST_TRACKERS) {
      if (data.clientId) fields.clientId = data.clientId;
      if (window.localStorage) fields.storage = 'none';
      window.ga('create', tracker.trackingId, 'auto', tracker.name, fields);
    }
  });
}


function trackErrors() {
  // Errors that have occurred prior to this script running are stored on
  // the `q` property of the window.onerror function.
  let errorQueue = window.onerror.q || [];

  // Override the temp `onerror()` handler to now send hits to GA.
  window.onerror = function(msg, file, line, col, error) {
    gaAll('send', 'event', {
      eventCategory: 'Script',
      eventAction: 'uncaught error',
      eventLabel: error ? error.stack : `${msg}\n${file}:${line}:${col}`
    });
  };

  // Replay any stored errors in the queue.
  for (let error of errorQueue) {
    window.onerror(...error);
  }
}


function setDefaultDimensionValues() {
  Object.keys(dimensions).forEach((key) => {
    gaAll('set', dimensions[key], NULL_VALUE);
  });
}


function requirePlugins() {
  gaAll('require', 'cleanUrlTracker', {
    stripQuery: true,
    queryDimensionIndex: getDefinitionIndex(dimensions.URL_QUERY_PARAMS),
    indexFilename: 'index.html',
    trailingSlash: 'add'
  });
  gaProd('require', 'eventTracker');
  gaProd('require', 'mediaQueryTracker', {
    definitions: [
      {
        name: 'Breakpoint',
        dimensionIndex: 1,
        items: [
          {name: 'xs', media: 'all'},
          {name: 'sm', media: '(min-width: 384px)'},
          {name: 'md', media: '(min-width: 576px)'},
          {name: 'lg', media: '(min-width: 768px)'}
        ]
      },
      {
        name: 'Resolution',
        dimensionIndex: 2,
        items: [
          {name: '1x',   media: 'all'},
          {name: '1.5x', media: '(-webkit-min-device-pixel-ratio: 1.5), ' +
                                '(min-resolution: 144dpi)'},
          {name: '2x',   media: '(-webkit-min-device-pixel-ratio: 2), ' +
                                '(min-resolution: 192dpi)'},
        ]
      },
      {
        name: 'Orientation',
        dimensionIndex: 3,
        items: [
          {name: 'landscape', media: '(orientation: landscape)'},
          {name: 'portrait',  media: '(orientation: portrait)'}
        ]
      }
    ]
  });
  gaAll('require', 'outboundLinkTracker');
  gaProd('require', 'pageVisibilityTracker', {
    visibleMetricIndex: getDefinitionIndex(metrics.PAGE_VISIBLE),
    hiddenMetricIndex: getDefinitionIndex(metrics.PAGE_HIDDEN),
    fieldsObj: {
      nonInteraction: null, // Ensure all events are interactive.
      [dimensions.HIT_SOURCE]: 'pageVisibilityTracker'
    },
    hitFilter: function(model) {
      model.set(dimensions.METRIC_VALUE, String(model.get('eventValue')), true);
    }
  });
  gaProd('require', 'socialWidgetTracker');
}
{'fraction_non_alphanumeric': 0.15136327185244586, 'fraction_numerical': 0.012429831595829992, 'mean_word_length': 3.397091229616571, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '641968', 'n_tokens_mistral': 3447, 'n_tokens_neox': 3255, 'n_words': 698}