|
@@ -1,159 +0,0 @@
|
1
|
|
-/* global ga */
|
2
|
|
-
|
3
|
|
-import { getJitsiMeetGlobalNS } from '../../base/util/helpers';
|
4
|
|
-
|
5
|
|
-import AbstractHandler, { IEvent } from './AbstractHandler';
|
6
|
|
-
|
7
|
|
-/**
|
8
|
|
- * Analytics handler for Google Analytics.
|
9
|
|
- */
|
10
|
|
-class GoogleAnalyticsHandler extends AbstractHandler {
|
11
|
|
- _userProperties: Object;
|
12
|
|
- _userPropertiesString: string;
|
13
|
|
-
|
14
|
|
- /**
|
15
|
|
- * Creates new instance of the GA analytics handler.
|
16
|
|
- *
|
17
|
|
- * @param {Object} options - The Google Analytics options.
|
18
|
|
- * @param {string} options.googleAnalyticsTrackingId - The GA track id
|
19
|
|
- * required by the GA API.
|
20
|
|
- */
|
21
|
|
- constructor(options: any) {
|
22
|
|
- super(options);
|
23
|
|
-
|
24
|
|
- this._userProperties = {};
|
25
|
|
-
|
26
|
|
- if (!options.googleAnalyticsTrackingId) {
|
27
|
|
- throw new Error('Failed to initialize Google Analytics handler, no tracking ID');
|
28
|
|
- }
|
29
|
|
-
|
30
|
|
- this._enabled = true;
|
31
|
|
- this._initGoogleAnalytics(options);
|
32
|
|
- }
|
33
|
|
-
|
34
|
|
- /**
|
35
|
|
- * Initializes the ga object.
|
36
|
|
- *
|
37
|
|
- * @param {Object} options - The Google Analytics options.
|
38
|
|
- * @param {string} options.googleAnalyticsTrackingId - The GA track id
|
39
|
|
- * required by the GA API.
|
40
|
|
- * @returns {void}
|
41
|
|
- */
|
42
|
|
- _initGoogleAnalytics(options: any) {
|
43
|
|
- /**
|
44
|
|
- * TODO: Keep this local, there's no need to add it to window.
|
45
|
|
- */
|
46
|
|
- /* eslint-disable */ // @ts-ignore
|
47
|
|
- (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
48
|
|
- // @ts-ignore
|
49
|
|
- (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
50
|
|
- })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
51
|
|
- /* eslint-enable */
|
52
|
|
- // @ts-ignore
|
53
|
|
- ga('create', options.googleAnalyticsTrackingId, 'auto');
|
54
|
|
-
|
55
|
|
- // @ts-ignore
|
56
|
|
- ga('send', 'pageview');
|
57
|
|
- }
|
58
|
|
-
|
59
|
|
- /**
|
60
|
|
- * Extracts the integer to use for a Google Analytics event's value field
|
61
|
|
- * from a lib-jitsi-meet analytics event.
|
62
|
|
- *
|
63
|
|
- * @param {Object} event - The lib-jitsi-meet analytics event.
|
64
|
|
- * @returns {number} - The integer to use for the 'value' of a Google
|
65
|
|
- * analytics event, or NaN if the lib-jitsi-meet event doesn't contain a
|
66
|
|
- * suitable value.
|
67
|
|
- * @private
|
68
|
|
- */
|
69
|
|
- _extractValue(event: IEvent) {
|
70
|
|
- let value: string | number | undefined = event?.attributes?.value;
|
71
|
|
-
|
72
|
|
- // Try to extract an integer from the "value" attribute.
|
73
|
|
- value = Math.round(parseFloat(value ?? ''));
|
74
|
|
-
|
75
|
|
- return value;
|
76
|
|
- }
|
77
|
|
-
|
78
|
|
- /**
|
79
|
|
- * Extracts the string to use for a Google Analytics event's label field
|
80
|
|
- * from a lib-jitsi-meet analytics event.
|
81
|
|
- *
|
82
|
|
- * @param {Object} event - The lib-jitsi-meet analytics event.
|
83
|
|
- * @returns {string} - The string to use for the 'label' of a Google
|
84
|
|
- * analytics event.
|
85
|
|
- * @private
|
86
|
|
- */
|
87
|
|
- _extractLabel(event: IEvent) {
|
88
|
|
- const { attributes = {} } = event;
|
89
|
|
- const labelsArray
|
90
|
|
- = Object.keys(attributes).map(key => `${key}=${attributes[key]}`);
|
91
|
|
-
|
92
|
|
- labelsArray.push(this._userPropertiesString);
|
93
|
|
-
|
94
|
|
- return labelsArray.join('&');
|
95
|
|
- }
|
96
|
|
-
|
97
|
|
- /**
|
98
|
|
- * Sets the permanent properties for the current session.
|
99
|
|
- *
|
100
|
|
- * @param {Object} userProps - The permanent portperties.
|
101
|
|
- * @returns {void}
|
102
|
|
- */
|
103
|
|
- setUserProperties(userProps: any = {}) {
|
104
|
|
- if (!this._enabled) {
|
105
|
|
- return;
|
106
|
|
- }
|
107
|
|
-
|
108
|
|
- // The label field is limited to 500B. We will concatenate all
|
109
|
|
- // attributes of the event, except the user agent because it may be
|
110
|
|
- // lengthy and is probably included from elsewhere.
|
111
|
|
- const filter = [ 'user_agent', 'callstats_name' ];
|
112
|
|
-
|
113
|
|
- this._userPropertiesString
|
114
|
|
- = Object.keys(userProps)
|
115
|
|
- .filter(key => filter.indexOf(key) === -1)
|
116
|
|
- .map(key => `permanent_${key}=${userProps[key]}`)
|
117
|
|
- .join('&');
|
118
|
|
- }
|
119
|
|
-
|
120
|
|
- /**
|
121
|
|
- * This is the entry point of the API. The function sends an event to
|
122
|
|
- * google analytics. The format of the event is described in
|
123
|
|
- * analyticsAdapter in lib-jitsi-meet.
|
124
|
|
- *
|
125
|
|
- * @param {Object} event - The event in the format specified by
|
126
|
|
- * lib-jitsi-meet.
|
127
|
|
- * @returns {void}
|
128
|
|
- */
|
129
|
|
- sendEvent(event: IEvent) {
|
130
|
|
- if (this._shouldIgnore(event)) {
|
131
|
|
- return;
|
132
|
|
- }
|
133
|
|
-
|
134
|
|
- const gaEvent: {
|
135
|
|
- eventAction?: string;
|
136
|
|
- eventCategory: string;
|
137
|
|
- eventLabel: string;
|
138
|
|
- eventValue?: number;
|
139
|
|
- } = {
|
140
|
|
- 'eventCategory': 'jitsi-meet',
|
141
|
|
- 'eventAction': this._extractName(event),
|
142
|
|
- 'eventLabel': this._extractLabel(event)
|
143
|
|
- };
|
144
|
|
- const value = this._extractValue(event);
|
145
|
|
-
|
146
|
|
- if (!isNaN(value)) {
|
147
|
|
- gaEvent.eventValue = value;
|
148
|
|
- }
|
149
|
|
-
|
150
|
|
- // @ts-ignore
|
151
|
|
- ga('send', 'event', gaEvent);
|
152
|
|
- }
|
153
|
|
-
|
154
|
|
-}
|
155
|
|
-
|
156
|
|
-const globalNS = getJitsiMeetGlobalNS();
|
157
|
|
-
|
158
|
|
-globalNS.analyticsHandlers = globalNS.analyticsHandlers || [];
|
159
|
|
-globalNS.analyticsHandlers.push(GoogleAnalyticsHandler);
|