<commit_before>'use strict';

import * as cookies from 'cookies-js';
import * as util from './lib/util.js';

let queue = window.fathom.q || [];
let trackerUrl = '';
const commands = {
  "trackPageview": trackPageview,
  "setTrackerUrl": setTrackerUrl,
};

function newVisitorData() {
  return {
    sid: util.generateKey(),
    isNewVisitor: true, 
    isNewSession: true,
    pagesViewed: [],
    lastSeen: +new Date(),
  }
}

function getData() {
  let thirtyMinsAgo = new Date();
  thirtyMinsAgo.setMinutes(thirtyMinsAgo.getMinutes() - 30);

  let data = cookies.get('_fathom');
  if(! data) {
    return newVisitorData();
  }

  try{
    data = JSON.parse(data);
  } catch(e) {
    console.error(e);
    return newVisitorData();
  }

  if(data.lastSeen < (+thirtyMinsAgo)) {
    data.isNewSession = true;
  }

  return data;  
}

function findTrackerUrl() {
  const el = document.getElementById('fathom-script')
  return el ? el.src.replace('tracker.js', 'collect') : '';
}

function setTrackerUrl(v) {
  trackerUrl = v;
}

function trackPageview() {
  if(trackerUrl === '') {
    trackerUrl = findTrackerUrl();
  }

  // Respect "Do Not Track" requests
  if('doNotTrack' in navigator && navigator.doNotTrack === "1") {
    return;
  }

  // get the path or canonical
  let path = location.pathname + location.search;

  // parse path from canonical, if page has one
  let canonical = document.querySelector('link[rel="canonical"][href]');
  if(canonical) {
    let a = document.createElement('a');
    a.href = canonical.href;
    path = a.pathname;
  }

  // only set referrer if not internal
  let referrer = '';
  if(document.referrer.indexOf(location.hostname) < 0) {
    referrer = document.referrer;
  }

  let data = getData();
  const d = {
    sid: data.sid,
    p: path,
    t: document.title,
    r: referrer,
    u: data.pagesViewed.indexOf(path) == -1 ? 1 : 0,
    b: 1,
    nv: data.isNewVisitor ? 1 : 0, 
    ns: data.isNewSession ? 1 : 0,
  };

  let i = document.createElement('img');
  i.src = trackerUrl + util.stringifyObject(d);
  i.addEventListener('load', function() {
    data.pagesViewed.push(path);
    data.isNewVisitor = false;
    data.isNewSession = false;
    data.lastSeen += new Date();
    cookies.set('_fathom', JSON.stringify(data), { expires: 60 * 60 * 24});
  });
  document.body.appendChild(i);
}

// override global fathom object
window.fathom = function() {
  var args = [].slice.call(arguments);
  var c = args.shift();
  commands[c].apply(this, args);
};

// process existing queue
queue.forEach((i) => fathom.apply(this, i));<commit_msg>don't send unneeded var in collect request<commit_after>'use strict';

import * as cookies from 'cookies-js';
import * as util from './lib/util.js';

let queue = window.fathom.q || [];
let trackerUrl = '';
const commands = {
  "trackPageview": trackPageview,
  "setTrackerUrl": setTrackerUrl,
};

function newVisitorData() {
  return {
    sid: util.generateKey(),
    isNewVisitor: true, 
    isNewSession: true,
    pagesViewed: [],
    lastSeen: +new Date(),
  }
}

function getData() {
  let thirtyMinsAgo = new Date();
  thirtyMinsAgo.setMinutes(thirtyMinsAgo.getMinutes() - 30);

  let data = cookies.get('_fathom');
  if(! data) {
    return newVisitorData();
  }

  try{
    data = JSON.parse(data);
  } catch(e) {
    console.error(e);
    return newVisitorData();
  }

  if(data.lastSeen < (+thirtyMinsAgo)) {
    data.isNewSession = true;
  }

  return data;  
}

function findTrackerUrl() {
  const el = document.getElementById('fathom-script')
  return el ? el.src.replace('tracker.js', 'collect') : '';
}

function setTrackerUrl(v) {
  trackerUrl = v;
}

function trackPageview() {
  if(trackerUrl === '') {
    trackerUrl = findTrackerUrl();
  }

  // Respect "Do Not Track" requests
  if('doNotTrack' in navigator && navigator.doNotTrack === "1") {
    return;
  }

  // get the path or canonical
  let path = location.pathname + location.search;

  // parse path from canonical, if page has one
  let canonical = document.querySelector('link[rel="canonical"][href]');
  if(canonical) {
    let a = document.createElement('a');
    a.href = canonical.href;
    path = a.pathname;
  }

  // only set referrer if not internal
  let referrer = '';
  if(document.referrer.indexOf(location.hostname) < 0) {
    referrer = document.referrer;
  }

  let data = getData();
  const d = {
    sid: data.sid,
    p: path,
    t: document.title,
    r: referrer,
    u: data.pagesViewed.indexOf(path) == -1 ? 1 : 0,
    nv: data.isNewVisitor ? 1 : 0, 
    ns: data.isNewSession ? 1 : 0,
  };

  let i = document.createElement('img');
  i.src = trackerUrl + util.stringifyObject(d);
  i.addEventListener('load', function() {
    data.pagesViewed.push(path);
    data.isNewVisitor = false;
    data.isNewSession = false;
    data.lastSeen += new Date();
    cookies.set('_fathom', JSON.stringify(data), { expires: 60 * 60 * 24});
  });
  document.body.appendChild(i);
}

// override global fathom object
window.fathom = function() {
  var args = [].slice.call(arguments);
  var c = args.shift();
  commands[c].apply(this, args);
};

// process existing queue
queue.forEach((i) => fathom.apply(this, i));
{'fraction_non_alphanumeric': 0.1558466576767482, 'fraction_numerical': 0.006742438836447698, 'mean_word_length': 3.3230641132389676, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 5, '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': '469794', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1770, 'n_words': 411}