1- import { Angulartics2GoogleTagManager } from 'angulartics2' ;
1+ import { Angulartics2GoogleAnalytics , Angulartics2GoogleTagManager } from 'angulartics2' ;
22import { of } from 'rxjs' ;
33
44import { GoogleAnalyticsService } from './google-analytics.service' ;
@@ -10,11 +10,13 @@ import { GOOGLE_ANALYTICS_KLARO_KEY } from '../shared/cookies/klaro-configuratio
1010
1111describe ( 'GoogleAnalyticsService' , ( ) => {
1212 const trackingIdProp = 'google.analytics.key' ;
13- const trackingIdTestValue = 'mock-tracking-id' ;
13+ const trackingIdV4TestValue = 'G-mock-tracking-id' ;
14+ const trackingIdV3TestValue = 'UA-mock-tracking-id' ;
1415 const innerHTMLTestValue = 'mock-script-inner-html' ;
1516 const srcTestValue = 'mock-script-src' ;
1617 let service : GoogleAnalyticsService ;
17- let angularticsSpy : Angulartics2GoogleTagManager ;
18+ let googleAnalyticsSpy : Angulartics2GoogleAnalytics ;
19+ let googleTagManagerSpy : Angulartics2GoogleTagManager ;
1820 let configSpy : ConfigurationDataService ;
1921 let klaroServiceSpy : jasmine . SpyObj < KlaroService > ;
2022 let scriptElementMock : any ;
@@ -32,15 +34,18 @@ describe('GoogleAnalyticsService', () => {
3234 } ) ;
3335
3436 beforeEach ( ( ) => {
35- angularticsSpy = jasmine . createSpyObj ( 'Angulartics2GoogleTagManager' , [
37+ googleAnalyticsSpy = jasmine . createSpyObj ( 'Angulartics2GoogleAnalytics' , [
38+ 'startTracking' ,
39+ ] ) ;
40+ googleTagManagerSpy = jasmine . createSpyObj ( 'Angulartics2GoogleTagManager' , [
3641 'startTracking' ,
3742 ] ) ;
3843
3944 klaroServiceSpy = jasmine . createSpyObj ( 'KlaroService' , {
4045 'getSavedPreferences' : jasmine . createSpy ( 'getSavedPreferences' )
4146 } ) ;
4247
43- configSpy = createConfigSuccessSpy ( trackingIdTestValue ) ;
48+ configSpy = createConfigSuccessSpy ( trackingIdV4TestValue ) ;
4449
4550 scriptElementMock = {
4651 set src ( newVal ) { /* noop */ } ,
@@ -66,7 +71,7 @@ describe('GoogleAnalyticsService', () => {
6671 GOOGLE_ANALYTICS_KLARO_KEY : true
6772 } ) ) ;
6873
69- service = new GoogleAnalyticsService ( angularticsSpy , klaroServiceSpy , configSpy , documentSpy ) ;
74+ service = new GoogleAnalyticsService ( googleAnalyticsSpy , googleTagManagerSpy , klaroServiceSpy , configSpy , documentSpy ) ;
7075 } ) ;
7176
7277 it ( 'should be created' , ( ) => {
@@ -90,7 +95,7 @@ describe('GoogleAnalyticsService', () => {
9095 GOOGLE_ANALYTICS_KLARO_KEY : true
9196 } ) ) ;
9297
93- service = new GoogleAnalyticsService ( angularticsSpy , klaroServiceSpy , configSpy , documentSpy ) ;
98+ service = new GoogleAnalyticsService ( googleAnalyticsSpy , googleTagManagerSpy , klaroServiceSpy , configSpy , documentSpy ) ;
9499 } ) ;
95100
96101 it ( 'should NOT add a script to the body' , ( ) => {
@@ -100,7 +105,8 @@ describe('GoogleAnalyticsService', () => {
100105
101106 it ( 'should NOT start tracking' , ( ) => {
102107 service . addTrackingIdToPage ( ) ;
103- expect ( angularticsSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
108+ expect ( googleAnalyticsSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
109+ expect ( googleTagManagerSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
104110 } ) ;
105111 } ) ;
106112
@@ -111,7 +117,7 @@ describe('GoogleAnalyticsService', () => {
111117 klaroServiceSpy . getSavedPreferences . and . returnValue ( of ( {
112118 [ GOOGLE_ANALYTICS_KLARO_KEY ] : true
113119 } ) ) ;
114- service = new GoogleAnalyticsService ( angularticsSpy , klaroServiceSpy , configSpy , documentSpy ) ;
120+ service = new GoogleAnalyticsService ( googleAnalyticsSpy , googleTagManagerSpy , klaroServiceSpy , configSpy , documentSpy ) ;
115121 } ) ;
116122
117123 it ( 'should NOT add a script to the body' , ( ) => {
@@ -121,15 +127,16 @@ describe('GoogleAnalyticsService', () => {
121127
122128 it ( 'should NOT start tracking' , ( ) => {
123129 service . addTrackingIdToPage ( ) ;
124- expect ( angularticsSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
130+ expect ( googleAnalyticsSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
131+ expect ( googleTagManagerSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
125132 } ) ;
126133 } ) ;
127134
128135 describe ( 'when google-analytics cookie preferences are not existing' , ( ) => {
129136 beforeEach ( ( ) => {
130- configSpy = createConfigSuccessSpy ( trackingIdTestValue ) ;
137+ configSpy = createConfigSuccessSpy ( trackingIdV4TestValue ) ;
131138 klaroServiceSpy . getSavedPreferences . and . returnValue ( of ( { } ) ) ;
132- service = new GoogleAnalyticsService ( angularticsSpy , klaroServiceSpy , configSpy , documentSpy ) ;
139+ service = new GoogleAnalyticsService ( googleAnalyticsSpy , googleTagManagerSpy , klaroServiceSpy , configSpy , documentSpy ) ;
133140 } ) ;
134141
135142 it ( 'should NOT add a script to the body' , ( ) => {
@@ -139,18 +146,19 @@ describe('GoogleAnalyticsService', () => {
139146
140147 it ( 'should NOT start tracking' , ( ) => {
141148 service . addTrackingIdToPage ( ) ;
142- expect ( angularticsSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
149+ expect ( googleAnalyticsSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
150+ expect ( googleTagManagerSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
143151 } ) ;
144152 } ) ;
145153
146154
147155 describe ( 'when google-analytics cookie preferences are set to false' , ( ) => {
148156 beforeEach ( ( ) => {
149- configSpy = createConfigSuccessSpy ( trackingIdTestValue ) ;
157+ configSpy = createConfigSuccessSpy ( trackingIdV4TestValue ) ;
150158 klaroServiceSpy . getSavedPreferences . and . returnValue ( of ( {
151159 [ GOOGLE_ANALYTICS_KLARO_KEY ] : false
152160 } ) ) ;
153- service = new GoogleAnalyticsService ( angularticsSpy , klaroServiceSpy , configSpy , documentSpy ) ;
161+ service = new GoogleAnalyticsService ( googleAnalyticsSpy , googleTagManagerSpy , klaroServiceSpy , configSpy , documentSpy ) ;
154162 } ) ;
155163
156164 it ( 'should NOT add a script to the body' , ( ) => {
@@ -160,18 +168,19 @@ describe('GoogleAnalyticsService', () => {
160168
161169 it ( 'should NOT start tracking' , ( ) => {
162170 service . addTrackingIdToPage ( ) ;
163- expect ( angularticsSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
171+ expect ( googleAnalyticsSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
172+ expect ( googleTagManagerSpy . startTracking ) . toHaveBeenCalledTimes ( 0 ) ;
164173 } ) ;
165174 } ) ;
166175
167- describe ( 'when both google-analytics cookie and the tracking id are non-empty' , ( ) => {
176+ describe ( 'when both google-analytics cookie and the tracking v4 id are non-empty' , ( ) => {
168177
169178 beforeEach ( ( ) => {
170- configSpy = createConfigSuccessSpy ( trackingIdTestValue ) ;
179+ configSpy = createConfigSuccessSpy ( trackingIdV4TestValue ) ;
171180 klaroServiceSpy . getSavedPreferences . and . returnValue ( of ( {
172181 [ GOOGLE_ANALYTICS_KLARO_KEY ] : true
173182 } ) ) ;
174- service = new GoogleAnalyticsService ( angularticsSpy , klaroServiceSpy , configSpy , documentSpy ) ;
183+ service = new GoogleAnalyticsService ( googleAnalyticsSpy , googleTagManagerSpy , klaroServiceSpy , configSpy , documentSpy ) ;
175184 } ) ;
176185
177186 it ( 'should create a script tag whose innerHTML contains the tracking id' , ( ) => {
@@ -183,10 +192,10 @@ describe('GoogleAnalyticsService', () => {
183192 expect ( documentSpy . createElement ( 'script' ) ) . toBe ( scriptElementMock ) ;
184193
185194 expect ( srcSpy ) . toHaveBeenCalledTimes ( 1 ) ;
186- expect ( srcSpy . calls . argsFor ( 0 ) [ 0 ] ) . toContain ( trackingIdTestValue ) ;
195+ expect ( srcSpy . calls . argsFor ( 0 ) [ 0 ] ) . toContain ( trackingIdV4TestValue ) ;
187196
188197 expect ( innerHTMLSpy ) . toHaveBeenCalledTimes ( 1 ) ;
189- expect ( innerHTMLSpy . calls . argsFor ( 0 ) [ 0 ] ) . toContain ( trackingIdTestValue ) ;
198+ expect ( innerHTMLSpy . calls . argsFor ( 0 ) [ 0 ] ) . toContain ( trackingIdV4TestValue ) ;
190199 } ) ;
191200
192201 it ( 'should add a script to the body' , ( ) => {
@@ -196,9 +205,46 @@ describe('GoogleAnalyticsService', () => {
196205
197206 it ( 'should start tracking' , ( ) => {
198207 service . addTrackingIdToPage ( ) ;
199- expect ( angularticsSpy . startTracking ) . toHaveBeenCalledTimes ( 1 ) ;
208+ expect ( googleAnalyticsSpy . startTracking ) . not . toHaveBeenCalled ( ) ;
209+ expect ( googleTagManagerSpy . startTracking ) . toHaveBeenCalledTimes ( 1 ) ;
210+ } ) ;
211+ } ) ;
212+
213+ describe ( 'when both google-analytics cookie and the tracking id v3 are non-empty' , ( ) => {
214+
215+ beforeEach ( ( ) => {
216+ configSpy = createConfigSuccessSpy ( trackingIdV3TestValue ) ;
217+ klaroServiceSpy . getSavedPreferences . and . returnValue ( of ( {
218+ [ GOOGLE_ANALYTICS_KLARO_KEY ] : true
219+ } ) ) ;
220+ service = new GoogleAnalyticsService ( googleAnalyticsSpy , googleTagManagerSpy , klaroServiceSpy , configSpy , documentSpy ) ;
221+ } ) ;
222+
223+ it ( 'should create a script tag whose innerHTML contains the tracking id' , ( ) => {
224+ service . addTrackingIdToPage ( ) ;
225+ expect ( documentSpy . createElement ) . toHaveBeenCalledTimes ( 1 ) ;
226+ expect ( documentSpy . createElement ) . toHaveBeenCalledWith ( 'script' ) ;
227+
228+ // sanity check
229+ expect ( documentSpy . createElement ( 'script' ) ) . toBe ( scriptElementMock ) ;
230+
231+ expect ( innerHTMLSpy ) . toHaveBeenCalledTimes ( 1 ) ;
232+ expect ( innerHTMLSpy . calls . argsFor ( 0 ) [ 0 ] ) . toContain ( trackingIdV3TestValue ) ;
233+ } ) ;
234+
235+ it ( 'should add a script to the body' , ( ) => {
236+ service . addTrackingIdToPage ( ) ;
237+ expect ( bodyElementSpy . appendChild ) . toHaveBeenCalledTimes ( 1 ) ;
238+ } ) ;
239+
240+ it ( 'should start tracking' , ( ) => {
241+ service . addTrackingIdToPage ( ) ;
242+ expect ( googleAnalyticsSpy . startTracking ) . toHaveBeenCalledTimes ( 1 ) ;
243+ expect ( googleTagManagerSpy . startTracking ) . not . toHaveBeenCalled ( ) ;
200244 } ) ;
201245 } ) ;
246+
202247 } ) ;
248+
203249 } ) ;
204250} ) ;
0 commit comments