File size: 1,312 Bytes
6778ee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { config } from './config'

export function init(track) {
  var lastPage

  function page(isSPANavigation) {
    if (!(COMPILE_HASH && (!COMPILE_CONFIG || config.hashBasedRouting))) {
      if (isSPANavigation && lastPage === location.pathname) return
    }

    lastPage = location.pathname
    track('pageview')
  }

  var onSPANavigation = function () {
    page(true)
  }

  if (COMPILE_HASH && (!COMPILE_CONFIG || config.hashBasedRouting)) {
    window.addEventListener('hashchange', onSPANavigation)
  } else {
    var his = window.history
    if (his.pushState) {
      var originalPushState = his['pushState']
      his.pushState = function () {
        originalPushState.apply(this, arguments)
        onSPANavigation()
      }
      window.addEventListener('popstate', onSPANavigation)
    }
  }

  function handleVisibilityChange() {
    if (!lastPage && document.visibilityState === 'visible') {
      page()
    }
  }

  if (
    document.visibilityState === 'hidden' ||
    document.visibilityState === 'prerender'
  ) {
    document.addEventListener('visibilitychange', handleVisibilityChange)
  } else {
    page()
  }

  window.addEventListener('pageshow', function (event) {
    if (event.persisted) {
      // Page was restored from bfcache - track a pageview
      page()
    }
  })
}