<commit_before>import BaseTracker from './BaseTracker'
import { getPageUnloadRetriesForNamespace, retryOnPageUnload } from './pageUnload'

function loadDrift () {
  /* eslint-disable */

  !function() {
    var t = window.driftt = window.drift = window.driftt || [];
    if (!t.init) {
      if (t.invoked) return void (window.console && console.error && console.error("Drift snippet included twice."));
      t.invoked = !0, t.methods = [ "identify", "config", "track", "reset", "debug", "show", "ping", "page", "hide", "off", "on" ],
        t.factory = function(e) {
          return function() {
            var n = Array.prototype.slice.call(arguments);
            return n.unshift(e), t.push(n), t;
          };
        }, t.methods.forEach(function(e) {
        t[e] = t.factory(e);
      }), t.load = function(t) {
        var e = 3e5, n = Math.ceil(new Date() / e) * e, o = document.createElement("script");
        o.type = "text/javascript", o.async = !0, o.crossorigin = "anonymous", o.src = "https://js.driftt.com/include/" + n + "/" + t + ".js";
        var i = document.getElementsByTagName("script")[0];
        i.parentNode.insertBefore(o, i);
      };
    }
  }();

  drift.SNIPPET_VERSION = '0.3.1';
  drift.load('9h3pui39u2s3');
}

const DEFAULT_DRIFT_IDENTIFY_USER_PROPERTIES = [
  'email', 'anonymous', 'dateCreated', 'hourOfCode', 'name', 'referrer', 'testGroupNumber', 'testGroupNumberUS',
  'gender', 'lastLevel', 'siteref', 'ageRange', 'schoolName', 'coursePrepaidID', 'role', 'firstName', 'lastName'
]

export default class DriftTracker extends BaseTracker {
  constructor (store) {
    super()

    this.store = store
  }

  get drift () {
    return this.driftApi
  }

  async _initializeTracker () {
    if (this.isChatEnabled) {
      await this.initializeDrift()
    } else {
      this.onInitializeSuccess()
    }

    this.store.watch(
      (state) => state.route,
      () => this.routeUpdated()
    )

    this.store.watch(
      (state) => state.me.role,
      () => this.userRoleUpdated()
    )

    this.watchForDisableAllTrackingChanges(this.store)
  }

  async initializeDrift () {
    loadDrift()

    window.drift.on('ready', async (api) => {
      this.driftApi = api

      this.initDriftOnLoad()
      if (!this.initializationComplete)
        this.onInitializeSuccess()

      this.updateDriftConfiguration()

      const retries = await getPageUnloadRetriesForNamespace('drift')
      for (const retry of retries) {
        this[retry.identifier](...retry.args)
      }
    })
  }

  initDriftOnLoad () {
    // Hide by default
    this.driftApi.widget.hide()

    // Show when a message is received
    window.drift.on('message', (e) => {
      if (!e?.data?.sidebarOpen) {
        if (this.isChatEnabled) {
          this.driftApi.widget.show()
        }
      }
    })
  }

  routeUpdated () {
    this.updateDriftConfiguration()
  }

  userRoleUpdated () {
    this.updateDriftConfiguration()
  }

  get onPlayPage () {
    const { route } = this.store.state
    return (route.path || '').indexOf('/play/') === 0
  }

  get isChatEnabled () {
    return !this.onPlayPage && !this.store.getters['me/isStudent'] && !this.store.getters['me/isHomePlayer']  // && !this.disableAllTracking
  }

  updateDriftConfiguration () {
    const chatEnabled = this.isChatEnabled
    if (this.isInitialized !== false) {
      // Drift failed to load, let's not try again.
      return
    } else if (chatEnabled && !this.driftApi) {
      return this.initializeDrift()
    } else if (!chatEnabled && !this.driftApi) {
      return
    }

    window.drift.config({
      enableWelcomeMessage: chatEnabled,
      enableCampaigns: chatEnabled,
      enableChatTargeting: chatEnabled,
    })
  }

  async identify (traits = {}) {
    if (this.disableAllTracking) {
      return
    }

    await this.initializationComplete

    const { me } = this.store.state<commit_msg>Handle unloaded Drift when identifying (#6182)<commit_after>import BaseTracker from './BaseTracker'
import { getPageUnloadRetriesForNamespace, retryOnPageUnload } from './pageUnload'

function loadDrift () {
  /* eslint-disable */

  !function() {
    var t = window.driftt = window.drift = window.driftt || [];
    if (!t.init) {
      if (t.invoked) return void (window.console && console.error && console.error("Drift snippet included twice."));
      t.invoked = !0, t.methods = [ "identify", "config", "track", "reset", "debug", "show", "ping", "page", "hide", "off", "on" ],
        t.factory = function(e) {
          return function() {
            var n = Array.prototype.slice.call(arguments);
            return n.unshift(e), t.push(n), t;
          };
        }, t.methods.forEach(function(e) {
        t[e] = t.factory(e);
      }), t.load = function(t) {
        var e = 3e5, n = Math.ceil(new Date() / e) * e, o = document.createElement("script");
        o.type = "text/javascript", o.async = !0, o.crossorigin = "anonymous", o.src = "https://js.driftt.com/include/" + n + "/" + t + ".js";
        var i = document.getElementsByTagName("script")[0];
        i.parentNode.insertBefore(o, i);
      };
    }
  }();

  drift.SNIPPET_VERSION = '0.3.1';
  drift.load('9h3pui39u2s3');
}

const DEFAULT_DRIFT_IDENTIFY_USER_PROPERTIES = [
  'email', 'anonymous', 'dateCreated', 'hourOfCode', 'name', 'referrer', 'testGroupNumber', 'testGroupNumberUS',
  'gender', 'lastLevel', 'siteref', 'ageRange', 'schoolName', 'coursePrepaidID', 'role', 'firstName', 'lastName'
]

export default class DriftTracker extends BaseTracker {
  constructor (store) {
    super()

    this.store = store
  }

  get drift () {
    return this.driftApi
  }

  async _initializeTracker () {
    if (this.isChatEnabled) {
      await this.initializeDrift()
    } else {
      this.onInitializeSuccess()
    }

    this.store.watch(
      (state) => state.route,
      () => this.routeUpdated()
    )

    this.store.watch(
      (state) => state.me.role,
      () => this.userRoleUpdated()
    )

    this.watchForDisableAllTrackingChanges(this.store)
  }

  async initializeDrift () {
    loadDrift()

    window.drift.on('ready', async (api) => {
      this.driftApi = api

      this.initDriftOnLoad()
      this.onInitializeSuccess()

      this.updateDriftConfiguration()

      const retries = await getPageUnloadRetriesForNamespace('drift')
      for (const retry of retries) {
        this[retry.identifier](...retry.args)
      }
    })
  }

  initDriftOnLoad () {
    // Hide by default
    this.driftApi.widget.hide()

    // Show when a message is received
    window.drift.on('message', (e) => {
      if (!e?.data?.sidebarOpen) {
        if (this.isChatEnabled) {
          this.driftApi.widget.show()
        }
      }
    })
  }

  routeUpdated () {
    this.updateDriftConfiguration()
  }

  userRoleUpdated () {
    this.updateDriftConfiguration()
  }

  get onPlayPage () {
    const { route } = this.store.state
    return (route.path || '').indexOf('/play/') === 0
  }

  get isChatEnabled () {
    return !this.onPlayPage && !this.store.getters['me/isStudent'] && !this.store.getters['me/isHomePlayer']  // && !this.disableAllTracking
  }

  updateDriftConfiguration () {
    const chatEnabled = this.isChatEnabled
    if (this.isInitialized !== false) {
      // Drift failed to load, let's not try again.
      return
    } else if (chatEnabled && !this.driftApi) {
      return this.initializeDrift()
    } else if (!chatEnabled && !this.driftApi) {
      return
    }

    window.drift.config({
      enableWelcomeMessage: chatEnabled,
      enableCampaigns: chatEnabled,
      enableChatTargeting: chatEnabled,
    })
  }

  async identify (traits = {}) {
    if (this.disableAllTracking) {
      return
    }

    await this.initializationComplete
    if (!window.drift) {
      return;
    }

    const { me } = this.store.state
{'fraction_non_alphanumeric': 0.15396380321182768, 'fraction_numerical': 0.004333418302319653, 'mean_word_length': 2.7031618688060406, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '4996336', 'n_tokens_mistral': 2599, 'n_tokens_neox': 2548, 'n_words': 497}