@@ -2,52 +2,91 @@ import { Injectable } from '@angular/core'
22import { UtilitiesService } from './utilities.service'
33import { ApplicationService } from './app.service'
44import packageJson from '../../../package.json'
5+ import { UIService , UIShownChangedEventCallback } from './ui.service'
6+
7+ declare global {
8+ interface Window {
9+ gaData : any
10+ gaGlobal : any
11+ gaplugins : any
12+ }
13+ }
514
615@Injectable ( {
716 providedIn : 'root'
817} )
918export class AnalyticsService {
1019 constructor (
1120 public utils : UtilitiesService ,
12- public app : ApplicationService
21+ public app : ApplicationService ,
22+ private readonly ui : UIService
1323 ) { }
1424
15- private _tracker : UniversalAnalytics . Tracker
16-
17- public get tracker ( ) {
18- return new Promise < UniversalAnalytics . Tracker > ( async ( resolve , reject ) => {
19- try {
20- if ( ! this . _tracker ) {
21- await this . utils . waitForProperty ( window , 'ga' )
22- await this . utils . delay ( 1000 )
23- await this . utils . waitForProperty ( ga , 'getAll' )
24- this . _tracker = ga . getAll ( ) [ 0 ]
25- }
26- resolve ( this . _tracker )
27- } catch ( err ) {
28- reject ( err )
29- }
25+ private injected = false
26+ private readonly SCRIPT_ID = 'google-analytics'
27+ private readonly UIIsShownListener : UIShownChangedEventCallback = ( { isShown } ) => {
28+ if ( isShown ) {
29+ this . ping ( )
30+ }
31+ }
32+
33+ async init ( ) {
34+ if ( this . injected ) return
35+
36+ await UtilitiesService . injectScript ( {
37+ id : this . SCRIPT_ID ,
38+ src : 'https://www.google-analytics.com/analytics.js'
3039 } )
40+ this . injected = true
41+
42+ window . ga ( 'create' , 'UA-96287398-6' )
43+ this . send ( )
44+
45+ this . clearPingTimer ( )
46+ this . pingTimer = setInterval ( ( ) => {
47+ this . ping ( )
48+ } , this . pingIntervalMs ) as any
49+ this . ui . onShownChanged ( this . UIIsShownListener )
3150 }
3251
33- async send ( ) {
34- const [ tracker , info ] = await Promise . all ( [
35- this . tracker ,
36- this . app . getInfo ( )
37- ] )
52+ private async send ( ) {
53+ const info = await this . app . getInfo ( )
3854 const data = {
3955 appName : 'eqMac' ,
4056 appVersion : `${ info . version } ` ,
4157 screenName : 'Home' ,
42- dimension1 : `${ packageJson . version } `
58+ dimension1 : `${ packageJson . version } ` ,
59+ dimension2 : `${ info . isOpenSource } ` ,
60+ dimension3 : `${ this . ui . isRemote } `
4361 }
44- tracker . send ( 'screenview' , data )
62+ window . ga ( 'send' , 'screenview' , data )
4563 }
4664
47- async ping ( ) {
48- const tracker = await this . tracker
49- tracker . send ( 'screenview' , {
65+ private pingTimer : number
66+ private readonly pingIntervalMs = 10 * 60 * 1000
67+
68+ private async ping ( ) {
69+ if ( ! this . injected ) return
70+ window . ga ( 'send' , 'screenview' , {
5071 screenview : 'Home'
5172 } )
5273 }
74+
75+ private clearPingTimer ( ) {
76+ if ( this . pingTimer ) {
77+ clearInterval ( this . pingTimer )
78+ this . pingTimer = undefined
79+ }
80+ }
81+
82+ deinit ( ) {
83+ this . clearPingTimer ( )
84+ this . ui . offShownChanged ( this . UIIsShownListener )
85+ window . document . getElementById ( this . SCRIPT_ID ) ?. remove ( )
86+ delete window . ga
87+ delete window . gaData
88+ delete window . gaGlobal
89+ delete window . gaplugins
90+ this . injected = false
91+ }
5392}
0 commit comments