@@ -9,7 +9,7 @@ import { deleteCookie, getCookie, setCookie } from './utils'
99import { loadGoogleAnalytics , removeGoogleAnalytics } from './clientSideOnly'
1010import { COOKIE_EXPIRATION_DAYS } from './constants'
1111
12- export function initializeServices (
12+ export function initializeGoogleAnalytics (
1313 servicesInitialized : boolean ,
1414 configuredServices : Service [ ] ,
1515 setServicesInitialized : ( bool : boolean ) => void
@@ -21,71 +21,65 @@ export function initializeServices(
2121 const googleAnalytics4Config = configuredServices . find (
2222 ( { type } ) => type === SupportedService . GoogleAnalytics4
2323 )
24+
2425 if ( googleAnalyticsUniversalConfig ?. enabled ) {
25- loadGoogleAnalytics ( googleAnalyticsUniversalConfig . id , setServicesInitialized )
26+ loadGoogleAnalytics ( googleAnalyticsUniversalConfig . id )
27+ setServicesInitialized ( true )
2628 } else {
2729 if ( googleAnalytics4Config ?. enabled ) {
28- loadGoogleAnalytics ( googleAnalytics4Config . id , setServicesInitialized )
30+ loadGoogleAnalytics ( googleAnalytics4Config . id )
31+ setServicesInitialized ( true )
2932 }
3033 }
3134 }
3235}
3336
34- interface InitConfiguredServicesArgs {
35- services : SKCMConfiguration [ 'services' ]
36- onConfiguredServicesInitialized : (
37- configuredServices : Service [ ] ,
38- necessaryCookies : ServiceCookie [ ]
39- ) => void
40- }
41- export function initializeConfiguredServices ( {
42- services : {
37+ export function initializeConfiguredServices ( services : SKCMConfiguration [ 'services' ] = { } ) : {
38+ configuredServices : Service [ ]
39+ necessaryCookies : ServiceCookie [ ]
40+ } {
41+ const {
4342 googleAnalyticsUniversalId,
4443 googleAnalytics4Id,
4544 adCookiesEnabled,
4645 customNecessaryCookies
47- } = { } ,
48- onConfiguredServicesInitialized
49- } : InitConfiguredServicesArgs ) : void {
50- let _configuredServices : Service [ ] = [ ]
51- let _necessaryCookies : ServiceCookie [ ] = [ ]
46+ } = services
47+
48+ let configuredServices : Service [ ] = [ ]
49+ let necessaryCookies : ServiceCookie [ ] = [ ]
5250 if ( googleAnalyticsUniversalId ) {
53- _configuredServices . push ( {
51+ configuredServices . push ( {
5452 type : SupportedService . GoogleAnalyticsUniversal ,
5553 id : googleAnalyticsUniversalId ,
5654 enabled : getCookie ( SKCM_GA_GOOGLE_ANALYTICS_UNIVERSAL_COOKIE ?. name ) === 'true' ,
5755 cookies : GoogleOwnCookies . GoogleAnalyticsUniversal
5856 } )
59- _necessaryCookies . push ( SKCM_GA_GOOGLE_ANALYTICS_UNIVERSAL_COOKIE )
57+ necessaryCookies . push ( SKCM_GA_GOOGLE_ANALYTICS_UNIVERSAL_COOKIE )
6058 }
6159 if ( googleAnalytics4Id ) {
62- _configuredServices . push ( {
60+ configuredServices . push ( {
6361 type : SupportedService . GoogleAnalytics4 ,
6462 id : googleAnalytics4Id ,
6563 enabled : getCookie ( SKCM_GA_GOOGLE_ANALYTICS_4_COOKIE ?. name ) === 'true' ,
6664 cookies : GoogleOwnCookies . GoogleAnalytics4
6765 } )
68- _necessaryCookies . push ( SKCM_GA_GOOGLE_ANALYTICS_4_COOKIE )
66+ necessaryCookies . push ( SKCM_GA_GOOGLE_ANALYTICS_4_COOKIE )
6967 }
7068 if ( customNecessaryCookies ) {
71- _necessaryCookies = [ ..._necessaryCookies , ...customNecessaryCookies ]
69+ necessaryCookies = [ ...necessaryCookies , ...customNecessaryCookies ]
7270 }
7371 if ( ! adCookiesEnabled ) {
74- const filteredCookies = _configuredServices . map ( ( service ) => ( {
72+ const filteredCookies = configuredServices . map ( ( service ) => ( {
7573 ...service ,
7674 cookies : service ?. cookies ?. filter ( ( cookie ) => cookie ?. category !== CookieCategory . Advertising )
7775 } ) )
78- _configuredServices = filteredCookies
76+ configuredServices = filteredCookies
7977 }
8078
81- onConfiguredServicesInitialized ?. ( _configuredServices , _necessaryCookies )
79+ return { configuredServices , necessaryCookies }
8280}
8381
84- export function stopCoreServices (
85- configuredServices : Service [ ] ,
86- removeAdditionalCookiesCb : ( ) => void ,
87- setServicesInitialized : ( bool : boolean ) => void
88- ) : void {
82+ export function stopCoreServices ( configuredServices : Service [ ] ) : void {
8983 const googleAnalyticsUniversalConfig = configuredServices ?. find (
9084 ( { type } ) => type === SupportedService . GoogleAnalyticsUniversal
9185 )
@@ -94,20 +88,15 @@ export function stopCoreServices(
9488 )
9589 removeGoogleAnalytics ( googleAnalytics4Config ?. id )
9690 removeGoogleAnalytics ( googleAnalyticsUniversalConfig ?. id )
97- removeAdditionalCookiesCb ( )
98-
99- setServicesInitialized ?.( false )
10091}
10192
102- export function setNecessaryCookies (
93+ export function applyCookieManagerNecessaryCookies (
10394 value : 'true' | 'false' ,
104- configuredServices : Service [ ] ,
105- necessaryCookies : ServiceCookie [ ] ,
106- setConfiguredServices : ( services : Service [ ] ) => void
95+ necessaryCookies : ServiceCookie [ ]
10796) : void {
108- const SKCM_NECESSARY_COOKIES : string [ ] = [
109- SKCM_GA_GOOGLE_ANALYTICS_UNIVERSAL_COOKIE ? .name ,
110- SKCM_GA_GOOGLE_ANALYTICS_4_COOKIE ? .name
97+ const SKCM_NECESSARY_COOKIES = [
98+ SKCM_GA_GOOGLE_ANALYTICS_UNIVERSAL_COOKIE . name ,
99+ SKCM_GA_GOOGLE_ANALYTICS_4_COOKIE . name
111100 ]
112101 // set cookies
113102 const neededCookies =
@@ -117,12 +106,6 @@ export function setNecessaryCookies(
117106 for ( let i = 0 ; i < neededCookies ?. length ; i ++ ) {
118107 setCookie ( neededCookies [ i ] ?. name , value , COOKIE_EXPIRATION_DAYS )
119108 }
120- // enable services
121- const _configuredServices = configuredServices ?. map ( ( service ) => ( {
122- ...service ,
123- enabled : value === 'true'
124- } ) )
125- setConfiguredServices ( _configuredServices )
126109}
127110
128111export function clearAdditionalCookies ( necessaryCookies : ServiceCookie [ ] ) : void {
0 commit comments