I set up a small website and added Google Analytics. I poured through a lot documentation but can't find an answer to my question. I added the Global Tag and a Tag for each individual page. I am looking to see if I need to do both as I want to track site visits and what page they view. Do I actually need both (global tag and a tag for each page) and does the setup look correct? Any assistance would be greatly appreciated.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-171082518-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-171082518-1');
gtag('config', 'UA-171082518-1', {
'page_title' : 'Resume',
'page_path': '/#resume'
});
gtag('config', 'UA-171082518-1', {
'page_title' : 'Contact',
'page_path': '/#contact'
});
gtag('config', 'UA-171082518-1', {
'page_title' : 'About',
'page_path': '/#about'
});
The global tag must be loaded on the page the first time it opens, if the others are pages that you send on the same a page (i.e. after scroll or click) you don't need to enter it again, but if you reload the page you have to reload the initial code.
Since the site is one page and I could not use the standard analytics set up I added the following to allow me to track what section of the site a user visits.
/======== Gtag Per Section Setup ========/
$('.nav-menu a').on('click', function() {
var $this = $(this);
if($this.hasClass('activeGt')) {
return;
}
$('.nav-menu a').removeClass('activeGt');
$this.addClass('activeGt');
var page_href = $this.attr('href');
var page_title = page_href.replace('#', '');
page_title = page_title.charAt(0).toUpperCase() + page_title.slice(1) + '-Section';
var path = (window.location.href).replace(window.location.origin, '').toLowerCase();
path = path.substring(0, path.indexOf('#'));
path = path + page_href;
gtag('js', new Date());
gtag('config', 'UA-171082519-1', {
'anonymize_ip': true, // for GDPR
'page_title' : page_title,
'page_path': path
});
});
Related
I want to increase the Sample Rate for Page Timings from 1% to 10% in google analytics.
We are using Global site tag (gtag.js) for adding GA to our site.
Following is current config:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-123456-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-123456-1');
</script>
As per the doc, the following will capture the user load time, but it won't increase the sampling rate.
// Feature detects Navigation Timing API support.
if (window.performance) {
// Gets the number of milliseconds since page load
// (and rounds the result since the value must be an integer).
var timeSincePageLoad = Math.round(performance.now());
// Sends the timing event to Google Analytics.
gtag('event', 'timing_complete', {
'name': 'load',
'value': timeSincePageLoad,
'event_category': 'JS Dependencies'
});
}
This was solved by setting, gtag('config', 'UA-XXXX-X',{'site_speed_sample_rate': 100})
We have a single page application using Gtag (Global Site Tag), and we manage the tracking of pages manually to provide the correct page names that we want to track. So we call
gtag('config', gaPropertyId, gtagPageConfig)
every time there is a route change.
This means that we don't want gtag to track pages for us automatically, as we are currently tracking all initial pages twice. The first is when gtag is automatically firing the tracking of the page by sending the browser title and the URL, and the second time is when we trigger the page view from our code, to provide the title for that page that we want to have see in Google Analytics.
After a lot of research and debugging, I've narrowed it down to the settings screen, where "Page loads" is checked, and can't be disabled, see this image:
We load our script like this:
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxx-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-xxx', { 'send_page_view': false });
</script>
When I remove the line gtag('js', new Date());, it doesn't track the page anymore, but this will probably result in some unwanted behaviour.
Is there a way for us to prevent the initial page load tracking? I've searched everywhere, but can't find the solution.
Simply you should NOT include those fields on pages you don't want to send any data to Google:
gtag('js', new Date());
gtag('config', 'G-xxx');
Your snippet should look like this without date and config:
<script async src="https://www.googletagmanager.com/gtag/js?id=G-xxx"></script>
<script>window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}
Sending data
SPA (i.e. Vue, React, etc)
If you are using SPA (i.e. Vue, React), you can run gtag function after route updated:
gtag('js', new Date());
gtag('config', 'G-xxx', { page: path: route.path, page_title: route.meta.title })
PHP
You can show up date and config conditionally:
<?php if (/* suits my needs*/): ?>
gtag('js', new Date());
gtag('config', 'G-xxx');
<?php endif; ?>
Documentation says about measure pageviews:
To disable the default pageview hit, set the send_page_view parameter to false in the gtag.js snippet.
gtag('config', 'GA_MEASUREMENT_ID', {
send_page_view: false
});
https://developers.google.com/analytics/devguides/collection/gtagjs/pages
I'm using gtag.js to track Google Ads.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-abc123"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'AW-abc123');
</script>
This is the code to trigger my Google Ads Conversion Tracking pixel:
var callback = function () {
if (typeof(url) != 'undefined') {
window.location = url;
}
};
gtag('event', 'conversion', {
'send_to': 'AW-abc123/12345',
'event_callback': callback
});
When I load my page with a fresh cache, I can see my conversion pixel being fired:
However, after a page reload, the conversion no longer fires:
What is causing the conversion tag to stop firing?
Why do you think that tag doesn't fire? Blue color in Tag Assistant indicates non-standard implementation etc. Try to click on your tag in Tag Assistant and see details.
Source:
https://developers.google.com/analytics/devguides/collection/gtagjs/cross-domain#configuring_a_site_to_accept_linker_parameters
Reference:
If the destination domain has been configured to automatically link domains, it will accept linker parameters by default.
I assume there is an HTML based solution for me to implement configuring a destination domain to automatically link domains.
How can I do this?
We have been using this approach. I'm not sure if it is correct, but it is an answer.
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-66686611-1', {
'linker': {
'domains': ['every.com','domain.com','that.com','I.com','will.com','link.com','to.com','or.com','from.com'],
'accept_incoming': true,
'decorate_forms': true
},
});
</script>
I've migrated from Universal to Global Site Tag. I've got sample_rate:50. It's not working for Pageviews.
Now: pageviews = 100. Expected: pageviews = 50.
Can you please show me an example that works?
https://jsfiddle.net/5owj671h/
<script async src="https://www.googletagmanager.com/gtag/js?id=<?
=$gaTrackingCode?>"></script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
gtag('js', new Date());
gtag('config', '<?= $gaTrackingCode ?>', {
'custom_map': {'dimension3': hasAdBlock},
'sample_rate': 50
});
gtag('event', 'page_view', { 'send_to': '<?= $gaTrackingCode ?>',
'sample_rate': 50 });
</script>
The gtag scripts sends the pageviews automatically so when I stopped sending pageviews, it worked.
sampleRate works with users, not page views. And I guess sample_rate is a wrong name.