1717
1818goog . module ( 'googlecodelabs.CodelabAnalytics' ) ;
1919
20- const Const = goog . require ( 'goog.string.Const' ) ;
2120const EventHandler = goog . require ( 'goog.events.EventHandler' ) ;
22- const TrustedResourceUrl = goog . require ( 'goog.html.TrustedResourceUrl' ) ;
23- const { safeScriptEl} = goog . require ( 'safevalues.dom' ) ;
2421
2522/**
2623 * The general codelab action event fired for trackable interactions.
@@ -41,24 +38,6 @@ const PAGEVIEW_EVENT = 'google-codelab-pageview';
4138 */
4239const GAID_ATTR = 'gaid' ;
4340
44- /**
45- * The Google Analytics GA4 ID.
46- * @const {string}
47- */
48- const GA4ID_ATTR = 'ga4id' ;
49-
50- /** @const {string} */
51- const GTAG = 'gtag' ;
52-
53- /**
54- * Namespaced data layer for use with GA4 properties. Allows for independent
55- * data layers so that other data layers, like that for GTM, don't receive data
56- * they don't need.
57- *
58- * @const {string}
59- */
60- const CODELAB_DATA_LAYER = 'codelabDataLayer' ;
61-
6241/** @const {string} */
6342const CODELAB_ID_ATTR = 'codelab-id' ;
6443
@@ -68,12 +47,6 @@ const CODELAB_ID_ATTR = 'codelab-id';
6847 */
6948const CODELAB_GAID_ATTR = 'codelab-gaid' ;
7049
71- /**
72- * The GA4ID defined by the current codelab.
73- * @const {string}
74- */
75- const CODELAB_GA4ID_ATTR = 'codelab-ga4id' ;
76-
7750/** @const {string} */
7851const CODELAB_ENV_ATTR = 'environment' ;
7952
@@ -130,9 +103,6 @@ class CodelabAnalytics extends HTMLElement {
130103 /** @private {?string} */
131104 this . gaid_ ;
132105
133- /** @private {?string} */
134- this . ga4Id_ ;
135-
136106 /** @private {?string} */
137107 this . codelabId_ ;
138108
@@ -155,9 +125,8 @@ class CodelabAnalytics extends HTMLElement {
155125 */
156126 connectedCallback ( ) {
157127 this . gaid_ = this . getAttribute ( GAID_ATTR ) || '' ;
158- this . ga4Id_ = this . getAttribute ( GA4ID_ATTR ) || '' ;
159128
160- if ( this . hasSetup_ || ( ! this . gaid_ && ! this . ga4Id_ ) ) {
129+ if ( this . hasSetup_ || ! this . gaid_ ) {
161130 return ;
162131 }
163132
@@ -170,14 +139,6 @@ class CodelabAnalytics extends HTMLElement {
170139 } else {
171140 this . init_ ( ) ;
172141 }
173-
174- if ( this . ga4Id_ ) {
175- this . initializeGa4_ ( ) ;
176- }
177-
178- if ( this . ga4Id_ && ! this . gaid_ ) {
179- this . addEventListeners_ ( ) ;
180- }
181142 }
182143
183144 /** @private */
@@ -192,7 +153,7 @@ class CodelabAnalytics extends HTMLElement {
192153 addEventListeners_ ( ) {
193154 this . eventHandler_ . listen ( document . body , ACTION_EVENT ,
194155 ( e ) => {
195- const detail = /** @type {! AnalyticsTrackingEvent } */ (
156+ const detail = /** @type {AnalyticsTrackingEvent } */ (
196157 e . getBrowserEvent ( ) . detail ) ;
197158 // Add tracking...
198159 this . trackEvent_ (
@@ -201,7 +162,7 @@ class CodelabAnalytics extends HTMLElement {
201162
202163 this . eventHandler_ . listen ( document . body , PAGEVIEW_EVENT ,
203164 ( e ) => {
204- const detail = /** @type {! AnalyticsPageview } */ (
165+ const detail = /** @type {AnalyticsPageview } */ (
205166 e . getBrowserEvent ( ) . detail ) ;
206167 this . trackPageview_ ( detail [ 'page' ] , detail [ 'title' ] ) ;
207168 } ) ;
@@ -255,7 +216,6 @@ class CodelabAnalytics extends HTMLElement {
255216 * @private
256217 */
257218 trackEvent_ ( category , opt_action , opt_label ) {
258- // UA related section.
259219 const params = {
260220 // Always event for trackEvent_ method
261221 'hitType' : 'event' ,
@@ -267,30 +227,6 @@ class CodelabAnalytics extends HTMLElement {
267227 'eventLabel' : opt_label || '' ,
268228 } ;
269229 this . gaSend_ ( params ) ;
270-
271- // GA4 related section.
272- if ( ! this . getGa4Ids_ ( ) . length ) {
273- return ;
274- }
275-
276- window [ CODELAB_DATA_LAYER ] = window [ CODELAB_DATA_LAYER ] || [ ] ;
277- window [ GTAG ] = window [ GTAG ] || function ( ) {
278- window [ CODELAB_DATA_LAYER ] . push ( arguments ) ;
279- } ;
280-
281- for ( const ga4Id of this . getGa4Ids_ ( ) ) {
282- window [ GTAG ] ( 'event' , category , {
283- // Snakecase naming convention is followed for all built-in GA4 event
284- // properties.
285- 'send_to' : ga4Id ,
286- // Camelcase naming convention is followed for all custom dimensions
287- // constructed in the custom element.
288- 'eventAction' : opt_action || '' ,
289- 'eventLabel' : opt_label || '' ,
290- 'codelabEnv' : this . codelabEnv_ || '' ,
291- 'codelabId' : this . codelabId_ || '' ,
292- } ) ;
293- }
294230 }
295231
296232 /**
@@ -299,7 +235,6 @@ class CodelabAnalytics extends HTMLElement {
299235 * @private
300236 */
301237 trackPageview_ ( opt_page , opt_title ) {
302- // UA related section.
303238 const params = {
304239 'hitType' : 'pageview' ,
305240 'dimension1' : this . codelabEnv_ ,
@@ -309,33 +244,6 @@ class CodelabAnalytics extends HTMLElement {
309244 'title' : opt_title || ''
310245 } ;
311246 this . gaSend_ ( params ) ;
312-
313- // GA4 related section.
314- if ( ! this . getGa4Ids_ ( ) . length ) {
315- return ;
316- }
317-
318- window [ CODELAB_DATA_LAYER ] = window [ CODELAB_DATA_LAYER ] || [ ] ;
319- window [ GTAG ] = window [ GTAG ] || function ( ) {
320- window [ CODELAB_DATA_LAYER ] . push ( arguments ) ;
321- } ;
322-
323- for ( const ga4Id of this . getGa4Ids_ ( ) ) {
324- window [ GTAG ] ( 'event' , 'page_view' , {
325- // Snakecase naming convention is followed for all built-in GA4 event
326- // properties.
327- 'send_to' : ga4Id ,
328- 'page_location' :
329- `${ document . location . origin } ${ document . location . pathname } ` ,
330- 'page_path' : opt_page || '' ,
331- 'page_title' : opt_title || '' ,
332- // Camelcase naming convention is followed for all custom dimensions
333- // constructed in the custom element.
334- 'codelabCategory' : this . codelabCategory_ || '' ,
335- 'codelabEnv' : this . codelabEnv_ || '' ,
336- 'codelabId' : this . codelabId_ || '' ,
337- } ) ;
338- }
339247 }
340248
341249 /**
@@ -477,68 +385,6 @@ class CodelabAnalytics extends HTMLElement {
477385 }
478386 return isCreated ;
479387 }
480-
481- /**
482- * Gets all GA4 IDs for the current page.
483- * @return {!Array<string> }
484- * @private
485- */
486- getGa4Ids_ ( ) {
487- if ( ! this . ga4Id_ ) {
488- return [ ] ;
489- }
490- const ga4Ids = [ ] ;
491- ga4Ids . push ( this . ga4Id_ ) ;
492- const codelabGa4Id = this . getAttribute ( CODELAB_GA4ID_ATTR ) ;
493- if ( codelabGa4Id ) {
494- ga4Ids . push ( codelabGa4Id ) ;
495- }
496- if ( ga4Ids . length ) {
497- return ga4Ids ;
498- }
499- return [ ] ;
500- }
501-
502- /**
503- * Initialize the gtag script element and namespaced data layer based on the
504- * codelabs primary GA4 ID.
505- * @private
506- */
507- initializeGa4_ ( ) {
508- if ( ! this . ga4Id_ ) {
509- return ;
510- }
511-
512- // First, set the GTAG data layer before pushing anything to it.
513- window [ CODELAB_DATA_LAYER ] = window [ CODELAB_DATA_LAYER ] || [ ] ;
514-
515- const firstScriptElement = document . getElementsByTagName ( 'script' ) [ 0 ] ;
516- const gtagScriptElement = /** @type {!HTMLScriptElement } */ (
517- document . createElement ( 'script' ) ) ;
518- gtagScriptElement . async = true ;
519- // Key for the formatted params below:
520- // 'id': the stream id for the GA4 analytics property. The gtag script
521- // element must only be created once, and only the ID of the primary
522- // stream is appended when creating the src for that element.
523- // Additional streams are initialized via the function call
524- // `window[GTAG]('config', ga4Id...`
525- // 'l': the namespaced dataLayer used to separate codelabs related GA4
526- // data from other data layers that may exist on a site or page.
527- safeScriptEl . setSrc (
528- gtagScriptElement , TrustedResourceUrl . formatWithParams (
529- Const . from ( '//www.googletagmanager.com/gtag/js' ) ,
530- { } , { 'id' : this . ga4Id_ , 'l' : CODELAB_DATA_LAYER } ) ) ;
531- firstScriptElement . parentNode . insertBefore (
532- gtagScriptElement , firstScriptElement ) ;
533-
534- window [ GTAG ] = function ( ) {
535- window [ CODELAB_DATA_LAYER ] . push ( arguments ) ;
536- } ;
537- window [ GTAG ] ( 'js' , new Date ( Date . now ( ) ) ) ;
538-
539- // Set send_page_view to false. We send pageviews manually.
540- window [ GTAG ] ( 'config' , this . ga4Id_ , { send_page_view : false } ) ;
541- }
542388}
543389
544390exports = CodelabAnalytics ;
0 commit comments