- This topic has 9 replies, 2 voices, and was last updated 4 years, 9 months ago by
rappid. This post has been viewed 1380 times
-
AuthorPosts
-
September 21, 2021 at 8:11 pm #10039359
rappid
ParticipantI do not understand how google analytics can be made part of the cookie consent bar properly. I mean having another option there is simple. You would just need to add:
<div class="gdpr-cookie-item">
<div class="gdpr-cookie-item-title">
<strong class="text-color-dark">Analytics
</div>
<div class="gdpr-cookie-item-description">
<p class="mb-0">We are using Google Analytics because we need to.</p>
</div>
<div class="gdpr-cookie-item-action">
<input type="checkbox" name="cookies_consent[]" class="gdpr-input custom-checkbox-switch" value="analytics" />
</div>
</div>But how can I actually achieve that my data is only called when the above is set to true?
I see how it is done with vimeo for example:<div data-plugin-gdpr-wrapper data-plugin-options="{'checkCookie': 'vimeo', 'ajaxURL': 'ajax/gdpr-vimeo.html'}">Is there a similar way to do it with the analytics code?
September 21, 2021 at 10:30 pm #10039363Support
KeymasterHello, if you want to check if the preferences were saved you can add an IF condition like this:
if( $.cookie( 'porto-privacy-bar' ) ) { code here... }Add that after all JS codes in the page.
Regards.
September 22, 2021 at 12:56 am #10039367rappid
ParticipantSorry, I might not have made my question clear enough and I do not understand your answer.
I was trying to find a way to have something else than the predefined gdpr-cookie-items (youtube, vimeo, etc.) denied/allowed by the cookie bar.
For example google analytics.
How would I achieve that in a nice way?Like written above I managed to have a gdpr-cookie-item with value “analytics”. Which appears as a value of the cookie named porto-gdpr-preferences. So far so good. Will I now have to manually write some javascript code that checks for the existence of “analytics” in the value of this cookie and if so, set the gtag “add_storage” to “granted” or is there a nicer way of doing this? I feel like I am missing something. I could do it like that but I thought there must be an easier way.
September 22, 2021 at 1:26 am #10039368rappid
ParticipantActually I was more thinking about a solution where I can just put
<div data-plugin-gdpr-wrapper data-plugin-options="{'checkCookie': 'analytics', 'ajaxURL': '/ajax_analytics.php'}"> </div>And then have my analytics code embedded pretty automatically.
But:
1. I am not sure if this is a good idea although it seems to work
2. I cannot use a div in the <head> section where the analytics code is supposed to be placed.
September 23, 2021 at 9:40 pm #10039375Support
KeymasterHello, my suggestion was actually to add the Google Analytics code inside the IF condition:
if( $.cookie( 'porto-privacy-bar' ) ) { // Google Analytics Code Here... window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-123123123'); }But please note that this code must be added after theme.js and theme.init.js in order to get it working.
Thanks.
September 24, 2021 at 3:40 pm #10039386rappid
ParticipantThanks a lot for clarification.
Isn´t that just checking if the cookie porto-privacy-bar is present?
Wouldn´t I need to check if there is actually “analytics” part of the value of the cookie “porto-gdpr-preferences” to set this up correctly?
Because this is when Google Analytics should be firing and not just when porto-privacy-bar is true. Right?
September 24, 2021 at 10:52 pm #10039402Support
KeymasterHello,
The IF condition I sent will verify if the “required” preferences were approved by the user. If the user don’t do anything or after reset the preferences it will return as undefined.
You may also add that in the Popup description if you want:

Kind Regards.
September 25, 2021 at 2:42 am #10039408rappid
ParticipantOK great. Thanks. And what if I would like to add Google Analytics as a non-required preference?
IMHO that makes more sense because basically it is not required to activate Google Analytics to have the website running properly.
This is why it should be optional.
September 25, 2021 at 3:27 am #10039411Support
KeymasterHello, unfortunately it’s not possible to add a new option at the moment without creating a new cookie and it will require a customization in the main code. We added that in our TODO list and we will consider that for upcoming versions.
Thanks.
September 25, 2021 at 4:38 pm #10039416rappid
ParticipantThank you for adding that to your todo list.
I think it is essential to have analytics optional inside the cookie consent.I managed to do almost that in two different ways already. But both of them are not ideal
I managed to add an option by just adding another gdpr-cookie-item like this
<div class="gdpr-cookie-item"> <div class="gdpr-cookie-item-title"> <strong class="text-color-dark">Analytics</strong> </div> <div class="gdpr-cookie-item-description"> <p class="mb-0"><txp:variable name="gdpr_description_analytics"/></p> </div> <div class="gdpr-cookie-item-action"> <input type="checkbox" name="cookies_consent[]" class="gdpr-input custom-checkbox-switch" value="analytics" /> </div> </div>Then on the bottom of my page I added this:
<div data-plugin-gdpr-wrapper data-plugin-options="{'checkCookie': 'analytics', 'ajaxURL': '/ajax_gtag.php'}"> </div>ajax_gtag.php contains the gtag script and preferences.
That works fine but having the javascript “ajaxed” inside a div on the bottom of the page does not seem right somehow.
Unfortunately I cannot think of any other way of doing it at the moment.The other way was to just add that in the custom.js
function loadGoogleAnalytics(){ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = 'https://www.googletagmanager.com/gtag/js?id=XXXX'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'XXXX',{ 'anonymize_ip': true }); }And then just add this on the document(ready)
if($.cookie('porto-gdpr-preferences').indexOf('analytics')!=-1){ loadGoogleAnalytics(); }But unfortunately this will only trigger Google Analytics on the next page reload. Which is not desireable. It should load it directly.
-
AuthorPosts
This topic is marked as "RESOLVED" and can not rceive new replies.