JO
2024-08-13 bf781fe79d3f2babeeaa9dd55e5f4a5da04e9977
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
/* global CONFIG, dataLayer, gtag */
 
if (!CONFIG.google_analytics.only_pageview) {
  if (CONFIG.hostname === location.hostname) {
    window.dataLayer = window.dataLayer || [];
    window.gtag = function() {
      dataLayer.push(arguments);
    };
    gtag('js', new Date());
    gtag('config', CONFIG.google_analytics.tracking_id);
 
    document.addEventListener('pjax:success', () => {
      gtag('event', 'page_view', {
        page_location: location.href,
        page_path    : location.pathname,
        page_title   : document.title
      });
    });
  }
} else {
  const sendPageView = () => {
    if (CONFIG.hostname !== location.hostname) return;
    const uid = localStorage.getItem('uid') || (Math.random() + '.' + Math.random());
    localStorage.setItem('uid', uid);
    navigator.sendBeacon('https://www.google-analytics.com/collect', new URLSearchParams({
      v  : 1,
      tid: CONFIG.google_analytics.tracking_id,
      cid: uid,
      t  : 'pageview',
      dp : encodeURIComponent(location.pathname)
    }));
  };
  document.addEventListener('pjax:complete', sendPageView);
  sendPageView();
}