The initial gtag code for setting up google analytics looks something like the following:
window.dataLayer = window.dataLayer || [];
function gtag(){window.dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA_TRACKING_ID');
This sets up gtag and google analytics 4, it also creates a page hit right away. If you want to avoid the initial tracking on page view you can add additional parameters to your config
call. Below we set send_page_view
to false, which stops gtag from sending the initial page view event:
window.dataLayer = window.dataLayer || [];
window.gtag = function () { dataLayer.push(arguments); };
window.gtag('config', 'GA_TRACKING_ID', { 'send_page_view': false });
window.gtag('js', new Date());
Later if we want to send a page view, we can create a pageview
event:
gtag('event', 'page_view', {
page_title: window.document.title,
page_location: window.location.href,
page_path: window.location.pathname,
send_to: 'GA_TRACKING_ID'
});
I hope you found this helpful, please leave a comment down below if you did!