Adjust Google Analytics behavior based on consent - google-analytics

I'm trying to implement Google Analytics based on consent, so the user is not tracked and the GA cookies are not set until after they accept the cookies. I'm using the following code:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
'ad_storage': 'denied',
'analytics_storage': 'denied'
});
gtag('js', new Date());
gtag('config', 'UA-XXXXXX', { 'anonymize_ip': true, 'cookie_expires': 0 });
</script>
<!-- End Global site tag (gtag.js) - Google Analytics -->
<script type="text/javascript">
__tcfapi('addEventListener', 2, function(tcData, success) {
if (success && tcData.gdprApplies && (tcData.eventStatus === 'tcloaded' || tcData.eventStatus === 'useractioncomplete') ) {
if (tcData.purpose.consents[1]) {
gtag('consent', 'update', {
'ad_storage': 'granted',
'analytics_storage': 'granted'
})
}
}
})
</script>
This code works great in Chrome, but it's not tracking correctly in Safari. What am I missing?
Thanks!

Related

Google onclick event not verifying (using Unbounce)

We have lots of links on a landing page which are effectively to be tracked in Google ads as a conversion (outbound click). We're using the code below but clicks on the links aren't being registered in Google ads..
There is no further URL to call on hence "// window.location = url;"
function gtag_report_conversion(url) {
var callback = function () {
if (typeof(url) != 'undefined') {
// window.location = url;
}
};
gtag('event', 'conversion', {
'send_to': 'AW-xxxxxxxxx',
'event_callback': callback
});
return false;
}
$(document).ready(function () {
$("#lp-id-10").click(function () {
gtag_report_conversion();
});
$(document).ready(function () {
$("#lp-id-11").click(function () {
gtag_report_conversion();
});
We've tried a few ways of coding this but our conversion point in Google ads is still marked as Unverified

How to launch analytical data (gtag) after consent it

Im trying to manage consent cookies and analytics and ads tracking code by following this source:
https://developers.google.com/tag-platform/devguides/consent
My code looks like this:
gtag config
<script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo $localBusiness->getAnalyticsId(); ?>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
// Default ad_storage to 'denied'.
gtag('consent', 'default', {
'analytics_storage': 'denied',
'ad_storage': 'denied'
});
gtag('js', new Date());
gtag('set', 'url_passthrough', true);
/** Adds here gtag config command with conversion Id */
gtag('config', '<?php echo $localBusiness->getAdsId(); ?>');
gtag('config', '<?php echo $localBusiness->getAnalyticsId(); ?>');
</script>
update consent
consentAdsAnalytics: function() {
var hasAdsConsent = this.getCookie('lorraine-cookies-advertisement');
if (hasAdsConsent == 'yes') {
gtag('consent', 'update', {
'ad_storage': 'granted'
});
} else {
gtag('consent', 'update', {
'ad_storage': 'denied'
});
}
var hasAnalyticsConsent = this.getCookie('lorraine-cookies-analytics');
if (hasAnalyticsConsent == 'yes') {
gtag('consent', 'update', {
'analytics_storage': 'granted'
});
} else {
gtag('consent', 'update', {
'analytics_storage': 'denied'
});
}
},
The problem is that when I click consent button, the visit isnt countend inmediantly, I need to navigate trough the web to begin to count visits from user granted and thus, I lose referal data.
Do you know how is to launch a signal or something similar when I accept the consent.

Analytics - How to track clicks on a given button

I can't believe I have to ask for help for such a basic request, but it's driving me crazy :)
I need my Google Analytics reports to tell me how many times a given button has been clicked.
I'm using the following code:
index.html (<head> tag)
<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>
script.js
window.addEventListener('load', function() {
ga('create', 'UA-130702382-1', 'auto', 'myButtonTracker');
});
myElement.addEventListener('click', function() {
ga("myButtonTracker.send", "event", "button", "click", "myButton has been clicked.");
});
What on earth am I missing?
Try reforming with gtag events.
gtag('event', <action>, {
'event_category': <category>,
'event_label': <label>,
'value': <value>
});

Google Analtyics gTag

Is it correct way of implementing Google Analytics?
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-000000- 00"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-000000-00');
gtag('config', 'UA-000000-00', { 'anonymize_ip': true, 'forceSSL': true, 'anonymize_ip': true });
gtag('config', 'UA-000000-00', {'page_title' : 'home', 'page_path': '/'});
Reading the documents it seems you are missing the "event" and have placed the values in the config.
https://developers.google.com/analytics/devguides/collection/gtagjs/custom-dims-mets
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-000000- 00"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-000000-00');
gtag('config', 'UA-000000-00', {'metric1': 'anonymize_ip', 'metric2': 'forceSSL'});
gtag('config', 'UA-000000-00', {'metric3': 'page_title', 'metric4': 'page_path'});
gtag('event', 'send', {'anonymize_ip': true, 'forceSSL': true,'page_title': 'home', 'page_path': '/');
I deleted one of the duplicate anonymize_ip.

How to track js based links in Google Analytics using the new gtag code

I previously tracked clicks to open tabs / modals etc in my google analytics. I have since had to change to the new gTag code and js links are now not being tracked. How can I fix this?
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxx-5"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
// line below previously pushed js links events
gtag('event', 'pageview', { 'page': location.pathname + location.search + location.hash});
gtag('config', 'UA-xxxxxxx-5', {
'linker': {
'domains': ['example.org.au']
}
});
I fixed this.
To get the full path including the anchor on page load:
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-xxxxxxx-5', {
'page_path': location.pathname + location.search + location.hash,
'linker': {
'domains': ['example.org.au']
}
});
</script>
then to track an event triggered in JS (eg tab opening)
link = location.pathname.replace(/^[\/]/, '') + location.search + location.hash
gtag('event', 'page_view', {'page_path': link});

Resources