File size: 2,619 Bytes
1b72d7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict'

import { useEffect } from 'react'
import { loadExternalResource } from '@/lib/utils'
import { useRouter } from 'next/router'
import { siteConfig } from '@/lib/config'
const Ackee = () => {
  const router = useRouter()
  const server = siteConfig('ANALYTICS_ACKEE_DATA_SERVER')
  const domainId = siteConfig('ANALYTICS_ACKEE_DOMAIN_ID')

  // 或者使用其他依赖数组,根据需要执行 handleAckee
  useEffect(() => {
    handleAckeeCallback()
  }, [router])

  // handleAckee 函数
  const handleAckeeCallback = () => {
    handleAckee(
      router.asPath,
      {
        server: server,
        domainId: domainId
      },
      {
        /*
                 * Enable or disable tracking of personal data.
                 * We recommend to ask the user for permission before turning this option on.
                 */
        detailed: true,
        /*
                * Enable or disable tracking when on localhost.
                */
        ignoreLocalhost: false,
        /*
                * Enable or disable the tracking of your own visits.
                * This is enabled by default, but should be turned off when using a wildcard Access-Control-Allow-Origin header.
                * Some browsers strictly block third-party cookies. The option won't have an impact when this is the case.
                */
        ignoreOwnVisits: false
      }
    )
  }

  return null
}

export default Ackee

/**
 * Function to use Ackee.
 * Creates an instance once and a new record every time the pathname changes.
 * Safely no-ops during server-side rendering.
 * @param {?String} pathname - Current path.
 * @param {Object} environment - Object containing the URL of the Ackee server and the domain id.
 * @param {?Object} options - Ackee options.
 */
const handleAckee = async function (pathname, environment, options = {}) {
  await loadExternalResource(siteConfig('ANALYTICS_ACKEE_TRACKER'), 'js')
  const ackeeTracker = window.ackeeTracker

  const instance = ackeeTracker?.create(environment.server, options)

  if (instance == null) {
    console.warn('Skipped record creation because useAckee has been called in a non-browser environment')
    return
  }

  const hasPathname = (
    pathname != null && pathname !== ''
  )

  if (hasPathname === false) {
    console.warn('Skipped record creation because useAckee has been called without pathname')
    return
  }

  const attributes = ackeeTracker?.attributes(options.detailed)
  const url = new URL(pathname, location)

  return instance.record(environment.domainId, {
    ...attributes,
    siteLocation: url.href
  }).stop
}