Skip to content
This repository has been archived by the owner on Apr 30, 2020. It is now read-only.

feat(Feature/GoogleAnalytics): Implemented Optout link feature for GA… #231

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Features/GoogleAnalytics/fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,21 @@
"maxlength": 500,
"placeholder" : "Separate IP addresses with commas"
}
],
"translatableOptions": [
{
"name": "optOutConfirm",
"label": "Opt out Confirm Text",
"type": "text",
"required": 1,
"default_value": "Are you sure you want to opt out of Google Analytics?"
},
{
"name": "optOutSuccess",
"label": "Opt out Success Text",
"type": "text",
"required": 1,
"default_value": "You successfully opt out from Google Analytics for this website."
}
]
}
28 changes: 28 additions & 0 deletions Features/GoogleAnalytics/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,42 @@

use Flynt\Features\GoogleAnalytics\GoogleAnalytics;
use Flynt\Utils\Feature;
use Flynt\Utils\Asset;
use Flynt\Features\Acf\OptionPages;

add_action('init', 'Flynt\Features\GoogleAnalytics\init');

function init()
{
$googleAnalyticsOptions = OptionPages::get('globalOptions', 'feature', 'GoogleAnalytics');
$googleAnalyticsOptionsTranslatable = OptionPages::get('translatableOptions', 'feature', 'GoogleAnalytics');

if ($googleAnalyticsOptions) {
new GoogleAnalytics($googleAnalyticsOptions);
}
}

add_action('wp_enqueue_scripts', function () {
$gaOptions = OptionPages::get('globalOptions', 'feature', 'GoogleAnalytics');
$gaOptionsTranslatable = OptionPages::get('translatableOptions', 'feature', 'GoogleAnalytics');

Asset::enqueue([
'type' => 'script',
'name' => 'js-cookie',
'path' => 'vendor/js-cookie.js'
]);

Asset::enqueue([
'type' => 'script',
'name' => 'Flynt/Features/GoogleAnalytics',
'path' => 'Features/GoogleAnalytics/script.js',
'dependencies' => ['jquery', 'js-cookie']
]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put these Asset::enqueue statements into a wp_enqueue_scripts hook.


$data = [
'gaId' => $gaOptions['gaId'],
'confirm' => $gaOptionsTranslatable['optOutConfirm'],
'success' => $gaOptionsTranslatable['optOutSuccess']
];
wp_localize_script('Flynt/Features/GoogleAnalytics', 'dataFlyntFeatureGoogleAnalytics', $data);
}, 100);
35 changes: 35 additions & 0 deletions Features/GoogleAnalytics/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* global dataFlyntFeatureGoogleAnalytics, Cookies */

import 'file-loader?name=vendor/js-cookie.js!js-cookie/src/js.cookie'

const data = dataFlyntFeatureGoogleAnalytics

export function optOut () {
let confirmOptout = false

if (data.confirm) {
confirmOptout = window.confirm(data.confirm)
} else {
confirmOptout = true
}

if (confirmOptout) {
window['ga-disable-' + data.gaId] = true
if (data.success) {
window.alert(data.success)
}
setOptoutCookie()
}
}

export function isOptout () {
return getOptoutCookie()
}

function setOptoutCookie () {
Cookies.set('disableGa', true)
}

function getOptoutCookie () {
return Cookies.get('disableGa')
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"gulp-watch": "^4.3.10",
"hasbin": "^1.2.3",
"jeet": "^7.0.0",
"normalize.css": "^7.0.0",
"js-cookie": "^2.1.4",
"normalize.css": "^6.0.0",
"picturefill": "^3.0.2",
"replace-in-file": "^2.5.0",
"require-dir": "^0.3.1",
Expand Down Expand Up @@ -78,7 +79,6 @@
"postbump": "gulp replaceVersion && git add ."
}
},
"dependencies": {},
"browserslist": [
"> 1%",
"last 2 versions",
Expand Down