@@ -5,6 +5,9 @@ export function BUAInitGlobals() {
55 function ( ) {
66 dataLayer . push ( arguments ) ;
77 } ;
8+ window . isGA = false ;
9+ window . isFBP = false ;
10+ window . isGTM = false ;
811}
912
1013export function BUAInitializeFacebookPixel ( FBPID ) {
@@ -32,6 +35,8 @@ export function BUAInitializeFacebookPixel(FBPID) {
3235 "https://connect.facebook.net/en_US/fbevents.js"
3336 ) ;
3437 fbq ( "init" , FBPID ) ;
38+ fbq ( "track" , "PageView" ) ;
39+ window . isFBP = true ;
3540}
3641
3742export function BUAInitializeGoogleAnalytics ( GAID ) {
@@ -41,17 +46,31 @@ export function BUAInitializeGoogleAnalytics(GAID) {
4146 document . head . appendChild ( script ) ;
4247 gtag ( "js" , new Date ( ) ) ;
4348 gtag ( "config" , GAID ) ;
49+ window . isGA = true ;
4450}
4551
46- export function BUATrackNavigation ( url , GAID ) {
47- if ( typeof fbq == "function" ) {
48- fbq ( "track" , "PageView" ) ;
49- }
52+ export function BUAInitializeGTM ( GTMID ) {
53+ ( function ( w , d , s , l , i ) {
54+ w [ l ] = w [ l ] || [ ] ;
55+ w [ l ] . push ( {
56+ "gtm.start" : new Date ( ) . getTime ( ) ,
57+ event : "gtm.js" ,
58+ } ) ;
59+ var f = d . getElementsByTagName ( "head" ) [ 0 ] ,
60+ j = d . createElement ( s ) ,
61+ dl = l != "dataLayer" ? "&l=" + l : "" ;
62+ j . async = true ;
63+ j . src = "https://www.googletagmanager.com/gtm.js?id=" + i + dl ;
64+ f . appendChild ( j , f ) ;
65+ dataLayer . push ( { event : "pageview" } ) ;
66+ window . isGTM = true ;
67+ } ) ( window , document , "script" , "dataLayer" , GTMID ) ;
68+ }
5069
51- if ( typeof gtag == "function" ) {
52- var path = url . replace ( window . location . origin , "" ) ;
53- gtag ( "config" , GAID , { page_path : path } ) ;
54- }
70+ export function BUATrackNavigation ( url , GAID ) {
71+ GTMPageView ( url ) ;
72+ FacebookPixelPageView ( ) ;
73+ GoogleAnalyticsPageView ( url , GAID ) ;
5574}
5675
5776export function BUATrackEventsGtag (
@@ -60,27 +79,55 @@ export function BUATrackEventsGtag(
6079 eventCategory ,
6180 eventLabel
6281) {
63- gtag ( ' event' , eventName , {
64- ' event_category' : eventCategory ,
65- ' event_label' : eventLabel ,
66- ' value' : eventValue ,
82+ gtag ( " event" , eventName , {
83+ event_category : eventCategory ,
84+ event_label : eventLabel ,
85+ value : eventValue ,
6786 } ) ;
6887}
6988
70- export function BUATrackEventsGtagByObjectValue (
71- eventName ,
72- objectValue
73- ) {
74- gtag ( 'event' , eventName , objectValue ) ;
89+ export function BUATrackEventsGtagByObjectValue ( eventName , objectValue ) {
90+ gtag ( "event" , eventName , objectValue ) ;
7591}
7692
77- export function BUATrackEventsFacebookPixel (
78- eventName ,
79- objectValue
80- ) {
81- if ( typeof objectValue === 'object' && objectValue !== null ) {
82- fbq ( 'track' , eventName , objectValue ) ;
93+ export function BUATrackEventsFacebookPixel ( eventName , objectValue ) {
94+ if ( typeof objectValue === "object" && objectValue !== null ) {
95+ fbq ( "track" , eventName , objectValue ) ;
8396 } else {
84- fbq ( 'track' , eventName ) ;
97+ fbq ( "track" , eventName ) ;
98+ }
99+ }
100+
101+ export function BUATrackEventsGTM ( eventName , objectValue ) {
102+ if ( eventName !== null && objectValue !== null ) {
103+ objectValue [ 'event' ] = eventName ;
104+ dataLayer . push ( objectValue ) ;
105+ }
106+ }
107+
108+ function GetPath ( url ) {
109+ return url . replace ( window . location . origin , "" ) ;
110+ }
111+
112+ function FacebookPixelPageView ( callFromGTM = false ) {
113+ if ( ( window . isFBP || callFromGTM ) && typeof fbq == "function" ) {
114+ fbq ( "track" , "PageView" ) ;
115+ }
116+ }
117+
118+ function GoogleAnalyticsPageView ( url , GAID , callFromGTM ) {
119+ if ( ( window . isGA || callFromGTM ) && typeof gtag == "function" ) {
120+ gtag ( "config" , GAID , { page_path : GetPath ( url ) } ) ;
121+ }
122+ }
123+
124+ function GTMPageView ( url ) {
125+ if ( window . isGTM ) {
126+ dataLayer . push ( {
127+ event : "pageview" ,
128+ page : {
129+ path : GetPath ( url )
130+ }
131+ } ) ;
85132 }
86133}
0 commit comments