2022-10-17 23:37:01 +08:00
|
|
|
import Script from 'next/script';
|
2022-07-17 21:40:41 +08:00
|
|
|
|
2022-10-17 23:37:01 +08:00
|
|
|
import siteMetadata from '@/data/siteMetadata';
|
2022-07-17 21:40:41 +08:00
|
|
|
|
|
|
|
const GAScript = () => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Script
|
|
|
|
strategy="lazyOnload"
|
|
|
|
src={`https://www.googletagmanager.com/gtag/js?id=${siteMetadata.analytics.googleAnalyticsId}`}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Script strategy="lazyOnload" id="ga-script">
|
|
|
|
{`
|
|
|
|
window.dataLayer = window.dataLayer || [];
|
|
|
|
function gtag(){dataLayer.push(arguments);}
|
|
|
|
gtag('js', new Date());
|
|
|
|
gtag('config', '${siteMetadata.analytics.googleAnalyticsId}', {
|
|
|
|
page_path: window.location.pathname,
|
|
|
|
});
|
|
|
|
`}
|
|
|
|
</Script>
|
|
|
|
</>
|
2022-10-17 23:37:01 +08:00
|
|
|
);
|
|
|
|
};
|
2022-07-17 21:40:41 +08:00
|
|
|
|
2022-10-17 23:37:01 +08:00
|
|
|
export default GAScript;
|
2022-07-17 21:40:41 +08:00
|
|
|
|
|
|
|
// https://developers.google.com/analytics/devguides/collection/gtagjs/events
|
|
|
|
export const logEvent = (action, category, label, value) => {
|
|
|
|
window.gtag?.('event', action, {
|
|
|
|
event_category: category,
|
|
|
|
event_label: label,
|
|
|
|
value: value,
|
2022-10-17 23:37:01 +08:00
|
|
|
});
|
|
|
|
};
|