Google Analytics Consent Tracking
February 5, 2025 · AI Conversations
ga('require', 'ec');
ga('set', 'currencyCode', ""); // Set tracker currency to Euros.
ga('ec:addProduct', { // Provide product details in an productFieldObject.
'id': "95", // Product ID (string).
'name': "", // Product name (string).
'category': "SUN SUBSCRIPTION", // Product category (string).
'price': "", // Product price (currency).
'quantity': 1 // Product quantity (number).
});
ga('ec:setAction', 'purchase', { // Transaction details are provided in an actionFieldObject.
'id': , // (Required) Transaction id (string).
'revenue': "", // Revenue (currency).
});
ga('send', 'pageview');
manually adjust above code to not fire before cookie hub consent approved
Tracking Google requests without Consent mode settings were detected.
Consent mode signals guide.
// Function to check if user consent is given
function hasUserConsented() {
// Replace this with the actual check for consent from your cookie consent manager
return /* true if consent is given, false otherwise */;
}
// Only execute the Google Analytics tracking if consent is given
if (hasUserConsented()) {
ga('create', 'UA-37352831-1');
ga('require', 'ec');
ga('set', 'currencyCode', "<?php echo $transactionData['currency']?>"); // Set tracker currency to Euros.
ga('ec:addProduct', { // Provide product details in a productFieldObject.
'id': "95", // Product ID (string).
'name': "<?php echo $pname ?>", // Product name (string).
'category': "SUN SUBSCRIPTION", // Product category (string).
'price': "<?php echo $transactionData['price']?>", // Product price (currency).
'quantity': 1 // Product quantity (number).
});
ga('ec:setAction', 'purchase', { // Transaction details are provided in an actionFieldObject.
'id': <?php echo $txnid ?>, // (Required) Transaction id (string).
'revenue': "<?php echo $transactionData['price']?>", // Revenue (currency).
});
ga('send', 'pageview');
} else {
// Optionally handle the case where consent has not been given
console.log("User has not provided consent for tracking.");
}
### Key Adjustments:
1. **Consent Check**: The `hasUserConsented` function should be defined based on your specific cookie consent management implementation. It should return `true` if the user has granted consent and `false` otherwise.
2. **Conditional Tracking**: The Google Analytics tracking code is wrapped in an `if` statement that checks for user consent. This prevents the tracking code from executing before consent is granted.
### Important Notes:
– Ensure that the function used to check for user consent correctly reflects your consent management strategy.
– You may need to adjust the logic inside `hasUserConsented` based on how you store and check for consent (e.g., using cookies, local storage, or a specific API provided by your consent management platform).
– Always test to ensure that tracking behaves as expected after implementing these changes.
<script>
</script>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
var cpm = {};
window.cookiehub.load(cpm);
});
</script>
1. **Load Cookie Hub**: Make sure to load the Cookie Hub script as you've already done.
2. **Check Consent**: Use Cookie Hub's API to determine if the user has given consent.
3. **Execute Google Analytics Tracking**: Only execute the Google Analytics tracking code if consent is granted.
Here's an updated version of your code:
<script src="https://cdn.cookiehub.eu/c2/256fa6e2.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
var cpm = {};
window.cookiehub.load(cpm);
// Function to check if user consent is given for Google Analytics
function hasUserConsented() {
return window.cookiehub.getConsent("google_analytics") === true; // Adjust "google_analytics" to your actual consent group name if necessary
}
// Google Analytics tracking code
if (hasUserConsented()) {
ga('create', 'UA-37352831-1');
ga('require', 'ec');
ga('set', 'currencyCode', "<?php echo $transactionData['currency']?>"); // Set tracker currency to Euros.
ga('ec:addProduct', { // Provide product details in a productFieldObject.
'id': "95", // Product ID (string).
'name': "<?php echo $pname ?>", // Product name (string).
'category': "SUN SUBSCRIPTION", // Product category (string).
'price': "<?php echo $transactionData['price']?>", // Product price (currency).
'quantity': 1 // Product quantity (number).
});
ga('ec:setAction', 'purchase', { // Transaction details are provided in an actionFieldObject.
'id': <?php echo $txnid ?>, // (Required) Transaction id (string).
'revenue': "<?php echo $transactionData['price']?>", // Revenue (currency).
});
ga('send', 'pageview');
} else {
// Optionally handle the case where consent has not been given
console.log("User has not provided consent for Google Analytics tracking.");
}
});
</script>
### Key Changes:
1. **Cookie Hub Loading**: The Cookie Hub script is included to manage consent.
2. **Consent Check**: The `hasUserConsented` function uses `window.cookiehub.getConsent("google_analytics")` to check if consent has been given for Google Analytics. Make sure to replace `"google_analytics"` with the actual name of your consent category for Google Analytics tracking if it differs.
3. **Conditional Execution**: The Google Analytics tracking code executes only if the user has consented.
### Important Notes:
– Ensure that your Cookie Hub implementation is correctly set up to manage consent categories, and adjust the category name in the `getConsent` function accordingly.
– Test the implementation thoroughly to verify that tracking only occurs after consent has been granted.
_ga Cookie Google Analytics
_gid Cookie Google Analytics
_gat Cookie Google Analytics
_ga_ Cookie Google Analytics
bcookie Cookie LinkedIn Insight
Here's how you can adjust your code using Cookie Hub to handle multiple tracking technologies based on user consent:
1. **Define Tracking Categories**: Make sure each tracking technology has a corresponding consent category in Cookie Hub (e.g., "google_analytics" for Google Analytics, "microsoft_ads" for Microsoft Ads, and "linkedin_insight" for LinkedIn).
2. **Modify the Tracking Code**: Update the tracking logic to check for user consent for each service before firing the tracking code.
### Example Implementation
Here’s how you can implement this:
<script src="https://cdn.cookiehub.eu/c2/256fa6e2.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
var cpm = {};
window.cookiehub.load(cpm);
// Function to check if user consent is given
function hasUserConsented(service) {
return window.cookiehub.getConsent(service) === true; // Check consent for the given service
}
// Google Analytics tracking
if (hasUserConsented("google_analytics")) {
ga('create', 'UA-37352831-1');
ga('require', 'ec');
ga('set', 'currencyCode', "<?php echo $transactionData['currency']?>"); // Set tracker currency to Euros.
ga('ec:addProduct', { // Provide product details in a productFieldObject.
'id': "95", // Product ID (string).
'name': "<?php echo $pname ?>", // Product name (string).
'category': "SUN SUBSCRIPTION", // Product category (string).
'price': "<?php echo $transactionData['price']?>", // Product price (currency).
'quantity': 1 // Product quantity (number).
});
ga('ec:setAction', 'purchase', { // Transaction details are provided in an actionFieldObject.
'id': <?php echo $txnid ?>, // (Required) Transaction id (string).
'revenue': "<?php echo $transactionData['price']?>", // Revenue (currency).
});
ga('send', 'pageview');
} else {
console.log("User has not provided consent for Google Analytics tracking.");
}
// Microsoft Ads tracking
if (hasUserConsented("microsoft_ads")) {
// Your Microsoft Ads tracking code here
// Example:
// window.uetq = window.uetq || [];
// window.uetq.push('event', 'Purchase', {
// 'ProductID': "95",
// 'Revenue': "<?php echo $transactionData['price']?>"
// });
} else {
console.log("User has not provided consent for Microsoft Ads tracking.");
}
// LinkedIn Insight tracking
if (hasUserConsented("linkedin_insight")) {
// Your LinkedIn Insight tracking code here
// Example:
// _linkedin_partner_id = "YOUR_LINKEDIN_PARTNER_ID";
// window._linkedin_data_partner_ids = window._linkedin_data_partner_ids || [];
// window._linkedin_data_partner_ids.push(_linkedin_partner_id);
} else {
console.log("User has not provided consent for LinkedIn Insight tracking.");
}
});
</script>
### Key Adjustments:
1. **Generalized Consent Check**: The `hasUserConsented` function checks for consent for the specified tracking service.
2. **Conditional Tracking for Each Service**: The Google Analytics, Microsoft Ads, and LinkedIn Insight tracking codes are only executed if the corresponding consent has been granted. You can add the actual tracking code for Microsoft Ads and LinkedIn in the respective sections.
3. **Logging for Consent Status**: Console logs are added to help you debug and ensure that the tracking codes are firing as expected based on user consent.
### Important Notes:
– Make sure that the consent categories ("google_analytics", "microsoft_ads", and "linkedin_insight") match what you have configured in Cookie Hub.
– Test the implementation thoroughly to confirm that tracking occurs only after consent is granted for each specific service.
### Google Analytics 4 (GA4)
If you're using Google Analytics 4 (GA4), you can include the following script in your HTML:
<!-- Google Analytics 4 -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX'); // Replace 'G-XXXXXXXXXX' with your GA4 measurement ID
</script>
### Universal Analytics (UA)
If you are still using Universal Analytics (UA), you can include it as follows:
<!-- Google Analytics (Universal Analytics) -->
<script async src="https://www.google-analytics.com/analytics.js"></script>
<script>
window.ga = window.ga || function(){(ga.q=ga.q||[]).push(arguments);};ga.l=+new Date;
ga('create', 'UA-XXXXXXXXX-X', 'auto'); // Replace 'UA-XXXXXXXXX-X' with your UA tracking ID
ga('send', 'pageview');
</script>
### Important Notes:
– **Measurement ID vs. Tracking ID**: In GA4, you use a Measurement ID that starts with "G-" (e.g., `G-XXXXXXXXXX`), while in Universal Analytics, you use a Tracking ID that starts with "UA-" (e.g., `UA-XXXXXXXXX-X`).
– **Async Loading**: Both scripts use the `async` attribute, which helps load the scripts asynchronously without blocking the rendering of the page.
– **Consent Management**: If you're implementing a consent management solution (like Cookie Hub), make sure to wrap the GA tracking code within a consent check, as discussed earlier.
Choose the appropriate script based on the version of Google Analytics you are using in your project.
<script>
</script>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
var cpm = {};
window.cookiehub.load(cpm);
});
</script>
### Recommended Placement
Here’s how you can structure your HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your Website Title</title>
<!-- Cookie Hub Script -->
<script src="https://cdn.cookiehub.eu/c2/27afa6e2.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
var cpm = {};
window.cookiehub.load(cpm);
});
</script>
<!-- Google Analytics Script (or other tracking scripts) -->
<!-- Make sure to add these only after checking consent -->
</head>
<body>
<!-- Your body content goes here -->
</body>
</html>
### Important Considerations:
1. **Consent Management**: Ensure that you implement checks for user consent for tracking scripts like Google Analytics before executing them. This way, tracking occurs only if the user has granted consent.
2. **Order of Scripts**: The Cookie Hub script should be placed before any other tracking scripts (like Google Analytics) that depend on user consent to ensure that consent is managed properly.
3. **Testing**: After implementing, test your setup to ensure that cookies are only set if the user consents and that tracking occurs as expected.
### Example with Consent Check
Here’s an example of how you might implement a consent check for Google Analytics after loading the Cookie Hub script:
<head>
<!-- Cookie Hub Script -->
<script src="https://cdn.cookiehub.eu/c2/27afa6e2.js"></script>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
var cpm = {};
window.cookiehub.load(cpm);
// Check consent for Google Analytics
function hasUserConsented() {
return window.cookiehub.getConsent("google_analytics") === true;
}
// Google Analytics tracking code
if (hasUserConsented()) {
// Google Analytics 4 or Universal Analytics code here
} else {
console.log("User has not provided consent for Google Analytics tracking.");
}
});
</script>
</head>
By following these guidelines, you can effectively manage cookie consent and tracking on your website.