/**
 * Configuration
 * @type {Object}
 */
let config = {
  debug: false,
  excludes: []
}

/**
 * Event tracking
 * @param  {String} category
 * @param  {String} action
 * @param  {String} [label='']
 * @param  {Number} [value=0]
 */
const event = function (category, action, label = '', value = 0) {
  if (typeof window.ga === 'undefined') {
    return
  }

  if (config.debug) {
    /* eslint-disable */
    console.groupCollapsed(`[VueAnalytics] Track event category "${category}"`)
    console.log(`category: ${category}`)
    console.log(`action: ${action}`)
    console.log(`label: ${label}`)
    console.log(`value: ${value}`)
    console.groupEnd()
    /* eslint-enable */
  }

  window.ga('send', 'event', category, action, label, value)
}

/**
 * Page tracking
 * @param  {String} page
 * @param  {String} title
 * @param  {String} location
 */
const page = function (page, title = '', location = '') {
  if (typeof window.ga === 'undefined') {
    return
  }

  if (config.debug) {
    /* eslint-disable */
    console.groupCollapsed(`[VueAnalytics] Track page "${page}"`)
    console.log(`page: ${page}`)
    console.log(`title: ${title}`)
    console.log(`location: ${location}`)
    console.groupEnd()
    /* eslint-enable */
  }

  window.ga('send', 'pageview', { page, title, location })
}

/**
 * Google Analytics script loader
 * it auto adds Google Analytics script without needs to modify the HTML page.
 * @param  {String} id Google Analytics ID
 * @return {Promise}
 */
export const loadScript = function (id) {
  return new Promise((resolve, reject) => {
    let script = document.createElement('script')
    const prior = document.getElementsByTagName('script')[0]

    script.async = 1

    prior.parentNode.insertBefore(script, prior)

    script.onload = script.onreadystatechange = function (_, isAbort) {
      if (isAbort || !script.readyState || /loaded|complete/.test(script.readyState)) {
        script.onload = script.onreadystatechange = null
        script = undefined

        if (isAbort) {
          reject({ error: true })
          return
        }

        window.ga('create', id, 'auto')

        resolve({ success: true, id })
      }
    }
    script.src = '//www.google-analytics.com/analytics.js'
  })
}

const isTrackable = (name) => {
  return !(config.excludes.length && config.excludes.indexOf(name) !== -1)
}

/**
 * Vue installer
 * @param  {Vue instance} Vue
 * @param  {Object} [options={}]
 */
const install = function (Vue, options = {}) {
  const { router, debug, excludeRoutes } = options

  config.excludes = excludeRoutes || config.excludes
  config.debug = !!debug

  /**
   * Naming conventions
   * Using "track" for semantic purposes
   * Using "ga", as an alias, for a more familiar feeling
   */
  Vue.track = Vue.ga = { event, page }
  Vue.prototype.$track = Vue.prototype.$ga = { event, page }

  // I don't like timeouts but apparently the currentRoute is not fully available yet
  // so need to wait the famous 0 second.
  // @todo: find a better way
  setTimeout(() => {
    const route = router.currentRoute

    if (!isTrackable(route.name)) {
      return
    }

    Vue.track.page(route.path, route.name, window.location.href)
  }, 0)

  if (router) {
    router.afterEach(({ path, name }) => {
      if (!isTrackable(name)) {
        return
      }

      Vue.track.page(path, name, window.location.href)
    })
  }
}

export default { install }

@@ -73,16 +73,31 @@
 false,%0D%0A
+  auto: true,%0D%0A
   exclud
@@ -2359,37 +2359,633 @@
 %0D%0A%0D%0A
-const isTrackable = (name) =%3E
+/**%0D%0A * Start tracking%0D%0A * @param  %7BVue%7D Vue%0D%0A * @param  %7BVueRouter%7D router%0D%0A */%0D%0Aconst start = function (Vue, router) %7B%0D%0A  return function () %7B%0D%0A    if (router) %7B%0D%0A      setTimeout(() =%3E %7B%0D%0A        const route = router.currentRoute%0D%0A%0D%0A        if (!isTrackable(route.name)) %7B%0D%0A          return%0D%0A        %7D%0D%0A%0D%0A        Vue.track.page(route.path, route.name, window.location.href)%0D%0A      %7D, 0)%0D%0A%0D%0A      router.afterEach((%7B path, name %7D) =%3E %7B%0D%0A        if (!isTrackable(name)) %7B%0D%0A          return%0D%0A        %7D%0D%0A%0D%0A        Vue.track.page(path, name, window.location.href)%0D%0A      %7D)%0D%0A    %7D%0D%0A  %7D%0D%0A%7D%0D%0A%0D%0Aconst isTrackable = function (name)
  %7B%0D%0A
@@ -3226,30 +3226,8 @@
 uter
-, debug, excludeRoutes
  %7D =
@@ -3258,16 +3258,24 @@
 ludes = 
+options.
 excludeR
@@ -3319,21 +3319,111 @@
 bug = !!
-debug
+options.debug%0D%0A  config.auto = typeof options.auto !== 'undefined' ? options.auto : config.auto
 %0D%0A%0D%0A  /*
@@ -3664,553 +3664,96 @@
  %7D%0D%0A
-%0D%0A  // I don't like timeouts but apparently the currentRoute is not fully available yet%0D%0A  // so need to wait the famous 0 second.%0D%0A  // @todo: find a better way%0D%0A  setTimeout(() =%3E %7B%0D%0A    const route = router.currentRoute
+  Vue.startTracking = start(Vue, router)
 %0D%0A%0D%0A  
- 
- 
 if (
-!isTrackable(route.name)) %7B%0D%0A      return%0D%0A    %7D%0D%0A%0D%0A    Vue.track.page(route.path, route.name, window.location.href)%0D%0A  %7D, 0)%0D%0A%0D%0A  if (router) %7B%0D%0A    router.afterEach((%7B path, name %7D) =%3E %7B%0D%0A      if (!isTrackable(name)) %7B%0D%0A        return%0D%0A      %7D%0D%0A%0D%0A      Vue.track.page(path, name, window.location.href)%0D%0A    %7D
+config.auto) %7B%0D%0A    Vue.startTracking(
 )%0D%0A 

add startTracking method