Craft 2 plugin for a drop in cookie acceptor to load GoogleAnalytics or other external resources only after the user accepts the Privacy Policy.
You don't use Craft ? This gist is a good starting point. In the end it's just plain HTML/CSS/JS! 👏
- Load GoogleAnalytics code only after accepting the notice.
- Provide link to opt-out from analytics services.
- Allows to load custom scripts like fonts, iframes, youtube videos, facebook, twitter plugin etc...
- "Refuse cookies"-option for user
- Set link to data policy page in settings.
- Easy code to change style or behaviour.
- No dependencies, just a few lines of HTML, JS and CSS
- Copy the
acceptcookies
directory into yourcraft/plugins
directory - Browse to Settings > Plugins in the Craft CP
- Click on the Install button next to Accept Cookies
Setup for plugin http://yourdomain.com/admin/settings/plugins/acceptcookies
- Change Google Analytics ID
- Add custom scripts
- Link to data policy page
- Let user reject the acceptor (-> no analytics.js or custom scripts for user)
- Add
{{craft.acceptCookies.trackingCode|raw}}
in your base layout
Disable acceptor for logged in users ?
{% if not currentUser %}
{{ craft.acceptCookies.trackingCode|raw }}
{% endif %}
You can provide your visitors a link to opt-out of google analytics. This is the link to deactivate the tracking:
<a href="javascript:gaOptout();">Deactivate Google Analytics</a>
Load font from external services only after user accepts cookies. Use this example code to put into the custom script section of plugin settings.
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css'; // if you include TypeKit then remove this line
link.id = 'myfont'
// change the link to an css here
link.href = 'https://fonts.googleapis.com/css?family=Roboto+Slab:400,300,700,100&subset=latin,latin-ext';
link.media = 'all';
head.appendChild(link);
Load maps from external services only after user accepts cookies. Use this example code to put into the custom script section of plugin settings.
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('script');
link.id = 'maps';
// change the API key in the maps link
link.src = 'https://maps.googleapis.com/maps/api/js?key=MAPS_API_KEY&callback=initMap';
link.async = true;
link.defer = true;
head.appendChild(link);
Load Google Tag Manager script only after user accepts cookies. Use this example code to put into the custom script section of plugin settings.
var head = document.getElementsByTagName('head')[0];
var gtagscript = document.createElement('script');
gtagscript.src= "https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X";
gtagscript.async = true;
head.appendChild(gtagscript);
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-X');
Load facebook plugin from external services only after user accepts cookies. Use this example code to put into the custom script section of plugin settings.
// Please use the snippets which facebooks give you. The language and API version from this snippet could be outdated.
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/de_DE/sdk.js#xfbml=1&version=v2.5&appId=YOUR_APP_ID";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
Write the following iframe in this way:
<div iframe-data="https://www.mylink.to/an/external/resource.mp4">
You have to accept cookies to view this video.
</div>
The script will parse all [iframe-data] - elements and converts them into iframes
After the script is loaded it will call the function initMap()
. Put your initialization logic for your maps in there.
-
2.0
-
GDPR ready!
-
Optimized sentence in cookie acceptor.
-
Experimental: Load iframe, only after user accepts cookie.
-
1.1
-
Allow custom scripts
-
Added translations
-
Added sample to load fonts
-
1.0
- Basic cookie acceptor. If accepted -> triggers google analytics
- Simple config/settings
- Initial release!
- custom JS which loads after accepting cookies
- custom css field and basic styling options
Feel free to join !