|
1 |
| -import ReactGA from 'react-ga'; |
2 |
| - |
3 |
| -/** |
4 |
| - * Google Analytics custom event category. |
5 |
| - */ |
6 |
| -export class GACategory { |
7 |
| - static APP_ACTION = 'App action' |
8 |
| -} |
9 |
| - |
10 | 1 | /**
|
11 |
| - * Google Analytics custom event action. |
| 2 | + * Google Analytics custom event name. |
12 | 3 | */
|
13 |
| -export class GAAction { |
14 |
| - static SWITCH_LANG = 'Switch Language' |
| 4 | +export class GAEvent { |
| 5 | + static LANG_CHANGE = 'lang_change'; |
| 6 | + static LOGIN = 'login'; |
| 7 | + static PAGE_VIEW = 'page_view'; |
15 | 8 | }
|
16 | 9 |
|
17 | 10 | /**
|
18 |
| - * Class for sending custom GA event |
| 11 | + * Class for sending custom GA events. |
19 | 12 | */
|
20 |
| -export class GAEvent { |
| 13 | +export class GoogleAnalytics { |
21 | 14 | /**
|
22 | 15 | * Record the event of switching the language.
|
23 | 16 | *
|
| 17 | + * @param {string} oldLang old language |
24 | 18 | * @param {string} newLang new language
|
25 | 19 | */
|
26 |
| - static languageChange(newLang: string) { |
27 |
| - ReactGA.event({ |
28 |
| - category: GACategory.APP_ACTION, |
29 |
| - action: GAAction.SWITCH_LANG, |
30 |
| - label: newLang, |
31 |
| - nonInteraction: false, |
32 |
| - }); |
| 20 | + static languageChange(oldLang: string, newLang: string) { |
| 21 | + GoogleAnalytics.sendEvent( |
| 22 | + GAEvent.LANG_CHANGE, |
| 23 | + { |
| 24 | + 'old': oldLang, |
| 25 | + 'new': newLang, |
| 26 | + }, |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Record the event of an user logged in. |
| 32 | + * |
| 33 | + * @param {string} method method used for login |
| 34 | + * @param {boolean} success if the login succeed |
| 35 | + */ |
| 36 | + static login(method: string = 'Google', success: boolean = true) { |
| 37 | + GoogleAnalytics.sendEvent( |
| 38 | + GAEvent.LOGIN, |
| 39 | + { |
| 40 | + 'method': method, |
| 41 | + 'success': success, |
| 42 | + }, |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Record the event of a page view. |
| 48 | + * |
| 49 | + * @param {Location} location location object |
| 50 | + */ |
| 51 | + static pageView(location: Location) { |
| 52 | + GoogleAnalytics.sendEvent( |
| 53 | + GAEvent.PAGE_VIEW, |
| 54 | + { |
| 55 | + 'page_location': location.href, |
| 56 | + 'page_title': document.title, |
| 57 | + 'page_path': location.pathname, |
| 58 | + }, |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Send a Google Analytics event via gtag.js. |
| 64 | + * |
| 65 | + * @param {string} eventName name of the event |
| 66 | + * @param {Object} parameters parameters of the event |
| 67 | + */ |
| 68 | + private static sendEvent(eventName: string, parameters: Object) { |
| 69 | + // @ts-ignore |
| 70 | + window.gtag('event', eventName, parameters); |
33 | 71 | }
|
34 | 72 | }
|
0 commit comments