mirror of
https://github.com/next-theme/hexo-theme-next.git
synced 2026-01-20 19:02:33 +00:00
Update Google Analytics only_pageview mode to GA4 (#649)
This commit is contained in:
parent
dedc8275e7
commit
9bd24f7ba0
@ -770,6 +770,8 @@ google_analytics:
|
|||||||
# By default, NexT will load an external gtag.js script on your site.
|
# By default, NexT will load an external gtag.js script on your site.
|
||||||
# If you only need the pageview feature, set the following option to true to get a better performance.
|
# If you only need the pageview feature, set the following option to true to get a better performance.
|
||||||
only_pageview: false
|
only_pageview: false
|
||||||
|
# only needed if you are using `only_pageview` mode, https://developers.google.com/analytics/devguides/collection/protocol/ga4
|
||||||
|
measure_protocol_api_secret:
|
||||||
|
|
||||||
# Baidu Analytics
|
# Baidu Analytics
|
||||||
# See: https://tongji.baidu.com
|
# See: https://tongji.baidu.com
|
||||||
|
|||||||
@ -22,13 +22,31 @@ if (!CONFIG.google_analytics.only_pageview) {
|
|||||||
if (CONFIG.hostname !== location.hostname) return;
|
if (CONFIG.hostname !== location.hostname) return;
|
||||||
const uid = localStorage.getItem('uid') || (Math.random() + '.' + Math.random());
|
const uid = localStorage.getItem('uid') || (Math.random() + '.' + Math.random());
|
||||||
localStorage.setItem('uid', uid);
|
localStorage.setItem('uid', uid);
|
||||||
navigator.sendBeacon('https://www.google-analytics.com/collect', new URLSearchParams({
|
fetch(
|
||||||
v : 1,
|
'https://www.google-analytics.com/mp/collect?' + new URLSearchParams({
|
||||||
tid: CONFIG.google_analytics.tracking_id,
|
api_secret : CONFIG.google_analytics.measure_protocol_api_secret,
|
||||||
cid: uid,
|
measurement_id: CONFIG.google_analytics.tracking_id
|
||||||
t : 'pageview',
|
}),
|
||||||
dp : encodeURIComponent(location.pathname)
|
{
|
||||||
}));
|
method : 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
client_id: uid,
|
||||||
|
events : [
|
||||||
|
{
|
||||||
|
name : 'page_view',
|
||||||
|
params: {
|
||||||
|
page_location: location.href,
|
||||||
|
page_title : document.title
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}),
|
||||||
|
mode: 'no-cors'
|
||||||
|
}
|
||||||
|
);
|
||||||
};
|
};
|
||||||
document.addEventListener('pjax:complete', sendPageView);
|
document.addEventListener('pjax:complete', sendPageView);
|
||||||
sendPageView();
|
sendPageView();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user