by Robert Matthees > E-commerce > easy-ga.js (2018-01-06)
Inspired by the General Data Protection Regulation (GDPR) & the European ePrivacy Regulation (ePR), I was looking into a Google Analytics Set-up that respects the Data Privacy Choices of Website Visitors / Online Shop Users, but still tracks their Orders & Transactions properly. The outcome is a little script:
Get easy-ga.js on GitHub (version: 0.1)
Example-Code:
var myTracker = new Tracking({
'property': 'UA-8238XXXX-1' //***Your Google Analytics ID
});
Example-Code:
myTracker.init(); //***Send Pageview
Example-Link: Google Analytics Tracking: opt-out / in
Example-Code:
<a href="javascript:myTracker.opt()">Google Analytics Tracking: opt-<span>out</span><span>/</span><span>in</span></a>
Info: The first Span-Element contains the message shown when the User is able to perform an Opt-out (creating the Opt-out Cookie), the last Span-Element contains the message for an Opt-in (deleting an existing Opt-out Cookie). The one in the middle can be used as separator for an initial, pre-rendered view.
Another Example without a Span-Element in the middle: opt-outopt-in
Example-Code:
<a href="javascript:myTracker.opt()"><span>opt-out</span><span>opt-in</span></a>
Same as:
<a href="javascript:myTracker.opt()">opt-<span>out</span><span>in</span></a>
Example-Data:
//***Test Data for Purchase Transaction Tracking
//*1. Array with 1 to n Product Objects:
var products = [{
'id': '12333',
'name': 'mini skirt ',
'category': 'woman clothing', //***optional
'brand': 'best skirts', //***optional
'variant': 'black', //***optional
'price': '30',
'quantity': 5
},{
'id': '12334',
'name': 'striped socks',
'category': 'man clothing',
'brand': 'hardly new',
'variant': 'red dot pink black',
'price': '1',
'coupon': 'OLDMANSALE', //***optional | Coupon Code (Product Level)
'quantity': 1
}];
//*2. Transaction Object:
var transaction = {
'id': 'abc1',
'affiliation': 'CookieDropShop', //***optional | name of affiliate partner responsible for transaction
'revenue': '155.99', //***optional | either total or without tax/shipping (up to you)
'tax': '24.91', //***optional
'shipping': '4.99', //***optinal
'coupon': 'SUMMERSALE1' //***optional | Coupon Code (Order Level)
};
Example-Code:
myTracker.init({ 'track': {
'action': 'purchase',
'products': products, //***Your Array with Product Obejects
'transaction': transaction //***Your Transaction Object
}});
Separated by comma:
'property': 'UA-8238XXXX-1', //***Enter Your Google Analytics ID | string | no default
'php': 'easy-ga.php', //***Where is the PHP Script located? | string | default: easy-ga.php
'selector': 'a[href$=".opt()"]', //***CSS Selector of Opt-out Link | string | default: a[href$=".opt()"]
'default_optout': 1, //***Tracking Switched Off by Default (just track after opt-in) | bool | default: 0
'opt_link': 1, //***Change Text of Opt-Out/In Links | bool | default: 1
'msg_confirm': 1, //***Show Opt-out/in Confirmation on Link Interaction? | bool | default: 1
//***Confirmation Messages | string | defaults below
'msg_txt_opt_in': 'opt-in successfull (tracking active)',
'msg_txt_opt_out': 'opt-out successfull (tracking stopped)',
'msg_txt_conflict': 'cookie opt-in, but -do not track- browser settings (still no tracking)',
'msg_txt_no_cookie': 'you need to enable cookies in your browser settings to use this feature',
'msg_txt_no_cookie_optout': 'you need to enable cookies in your browser settings to use this feature (tracking disabled by default)',
//***Confirmation Message Colors | string | defaults below
'msg_cl_opt_in': '#009400',
'msg_cl_opt_out': '#009400',
'msg_cl_conflict': '#ff0000',
'msg_cl_no_cookie': '#ff0000',
'msg_cl_no_cookie_optout': '#ff0000',
'msg_time': 2750, //***How long should the confimation message be displayed? (doubles when conflict / no cookie message) | integer in ms | default: 2750
'msg_container': '.msg-ga', //***Confirmation Msg in a specific Container instead of Link replacement? | string | empty on default
'warning': 1, //***Show Warning Message | bool | default: 0
//***Warning Messages | string | defaults below
'warning_optout': 'Important: There is an Opt-out Cookie saved in your Browser that prevents us from optimizing your experience on our site.',
'warning_do_not_track': 'Important: There are Do-not-track Settings activated in your Browser which prevent us from optimizing your experience on our site.',
'warning_both': 'Important: There is an Opt-out Cookie saved in your Browser & Do-not-track Settings which prevent us from optimizing your experience on our site.',
'warning_adblock': 'Important: Using AdBlockers is like Stealing Pocket Money from Children in Primary School.', //***Not 100% accurate: The Warning Message just comes up when an AdBlock User is not using Do-not-track Settings or an Opt-out Cookie at the same time as otherwise no Goggle Analytics will be started in the Frontend at all (and its running Functionality is used to detect AdBlockers)
'warning_cookies_disabled': 'Please enable Cookies in your Browser Settings to enjoy all features on our site.',
'warning_container': '.warn-ga', //***Where to display the warning message? CSS Selector | string | empty on default
'debug': 1 //***Debug Mode for Console Log Output | bool | default: 0