1
+ import Cookies from 'universal-cookie' ;
2
+
1
3
/**
2
4
* This file contains functions for UI analytics.
3
5
* Contains code used in both client and server
12
14
* @return void
13
15
*/
14
16
export function addAnalyticsEvent ( event ) {
15
- let newEvent = event ;
16
- const config = window . state ?. context ?. plugins [ 'extra-context-plugin' ] . config ;
17
- if ( event . event === undefined ) {
18
- // this is the default event field if none is defined
19
- newEvent = { event : 'sendMatomoEvent' , ...event } ;
20
- }
21
-
22
- if (
23
- ( config ?. useCookiesPrompt &&
24
- window . CookieInformation ?. getConsentGivenFor ( 'cookie_cat_statistic' ) ) ||
25
- ! config ?. useCookiesPrompt
26
- ) {
17
+ if ( window . dataLayer ) {
18
+ let newEvent = event ;
19
+ if ( event . event === undefined ) {
20
+ // this is the default event field if none is defined
21
+ newEvent = { event : 'sendMatomoEvent' , ...event } ;
22
+ }
27
23
window . dataLayer . push ( newEvent ) ;
28
24
}
29
25
}
@@ -35,7 +31,15 @@ export function addAnalyticsEvent(event) {
35
31
*
36
32
* @return string
37
33
*/
38
- export function getAnalyticsInitCode ( config , hostname ) {
34
+ export function getAnalyticsInitCode ( config , req ) {
35
+ const { hostname, cookies } = req ;
36
+ const useAnalytics =
37
+ ! config . useCookiesPrompt || cookies . cookieConsent === 'true' ;
38
+
39
+ if ( ! useAnalytics ) {
40
+ return '' ;
41
+ }
42
+
39
43
if (
40
44
config . analyticsScript &&
41
45
hostname &&
@@ -47,30 +51,51 @@ export function getAnalyticsInitCode(config, hostname) {
47
51
) ;
48
52
}
49
53
50
- if ( config . GTMid ) {
51
- // Google Tag Manager script
52
- return `<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
54
+ let script = config . GTMid
55
+ ? // Google Tag Manager script
56
+ `<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
53
57
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
54
58
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
55
59
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
56
- })(window,document,'script','dataLayer','${ config . GTMid } ');</script>\n` ;
60
+ })(window,document,'script','dataLayer','${ config . GTMid } ');</script>\n`
61
+ : '' ;
62
+ if ( config . crazyEgg ) {
63
+ script = `${ script } <script type="text/javascript" src="//script.crazyegg.com/pages/scripts/0030/3436.js" async="async" ></script>` ;
57
64
}
58
- return '' ;
65
+ return script ;
59
66
}
67
+
60
68
const handleChange = ( ) => {
61
69
if ( ! window . CookieInformation ) {
62
70
return false ;
63
71
}
64
- return window . CookieInformation . getConsentGivenFor ( 'cookie_cat_statistics' ) ;
72
+ const allow = window . CookieInformation . getConsentGivenFor (
73
+ 'cookie_cat_statistic' ,
74
+ ) ;
75
+ const cookies = new Cookies ( ) ;
76
+ const oldState = cookies . get ( 'cookieConsent' ) === true ;
77
+ cookies . set ( 'cookieConsent' , allow ) ;
78
+ if ( oldState && ! allow ) {
79
+ // no consent any more, reload page
80
+ window . location . reload ( ) ;
81
+ }
82
+ return allow ;
65
83
} ;
84
+
66
85
/**
67
86
* Client side intialization for UI analytics
68
87
*
69
88
* @return void
70
89
*/
71
90
export function initAnalyticsClientSide ( config ) {
72
- window . dataLayer = window . dataLayer || [ ] ;
73
- if ( config ?. useCookiesPrompt ) {
91
+ const cookies = new Cookies ( ) ;
92
+ const useAnalytics =
93
+ ! config . useCookiesPrompt || cookies . get ( 'cookieConsent' ) === true ;
94
+
95
+ if ( useAnalytics ) {
96
+ window . dataLayer = window . dataLayer || [ ] ;
97
+ }
98
+ if ( config . useCookiesPrompt ) {
74
99
window . addEventListener (
75
100
'CookieInformationConsentGiven' ,
76
101
handleChange ,
0 commit comments