import appInfo from "./app-info.json"

const ua = require("universal-analytics")
const uuid = require("uuid/v4")
const Store = require("electron-store")
const log = require("electron-log")
const packageInfo = require("../package")

const gaID = appInfo["ga_id"]

const fetchGAUser = () => {
  const store = new Store()

  const userId = store.get("classroom-assistant-ga-user-id") || uuid()

  store.set("classroom-assistant-ga-user-id", userId)

  return ua(gaID, userId)
}

export const trackEvent = (category, action, label = null, value = null) => {
  const usr = fetchGAUser()
  usr.event({
    ec: category,
    ea: action,
    el: label,
    ev: value,
  }).send()
  log.info(`GA Track Event: ${category}:${action}:${label}:${value}`)
}

export const trackException = (description, fatal = false) => {
  const usr = fetchGAUser()
  usr.exception(description, fatal).send()
  log.info(`GA Exception: ${description}`)
}

export const trackScreen = (screenName) => {
  const usr = fetchGAUser()
  usr.screenview(screenName, packageInfo.name, packageInfo.version).send()
  log.info(`GA Track Screen: ${screenName}`)
}

// Share with Renderer Process
global.trackEvent = trackEvent
global.trackException = trackException
global.trackScreen = trackScreen

@@ -1,43 +1,4 @@
-import appInfo from %22./app-info.json%22%0A%0A
 cons
@@ -196,60 +196,232 @@
 nst 
-gaID = appInfo%5B%22ga_id%22%5D%0A%0Aconst fetchGAUser = () =%3E %7B
+fetchGAUser = () =%3E %7B%0A  let gaID%0A  try %7B%0A    const appInfo = require(%22./app-info.json%22)%0A    gaID = appInfo%5B%22ga_id%22%5D%0A  %7D catch (e) %7B%0A    log.warn(%22Google Analytics ID not found, not tracking any events.%22)%0A    return null%0A  %7D%0A
 %0A  c
@@ -445,17 +445,16 @@
 Store()%0A
-%0A
   const 
@@ -516,17 +516,16 @@
  uuid()%0A
-%0A
   store.
@@ -702,24 +702,54 @@
 tchGAUser()%0A
+  if (!usr) %7B%0A    return%0A  %7D%0A%0A
   usr.event(
@@ -987,24 +987,54 @@
 tchGAUser()%0A
+  if (!usr) %7B%0A    return%0A  %7D%0A%0A
   usr.except
@@ -1183,16 +1183,46 @@
 AUser()%0A
+  if (!usr) %7B%0A    return%0A  %7D%0A%0A
   usr.sc

allow nil ga id