1// Copyright (C) 2020 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {globals} from '../frontend/globals';
16import * as version from '../gen/perfetto_version';
17
18type TraceCategories = 'Trace Actions'|'Record Trace'|'User Actions';
19const ANALYTICS_ID = 'UA-137828855-1';
20const PAGE_TITLE = 'no-page-title';
21
22export function initAnalytics() {
23  // Only initialize logging on prod or staging
24  if (window.location.origin.startsWith('http://localhost:') ||
25      window.location.origin.endsWith('.perfetto.dev') ||
26      window.location.origin.endsWith('staging-dot-perfetto-ui.appspot.com')) {
27    return new AnalyticsImpl();
28  }
29  return new NullAnalytics();
30}
31
32const gtagGlobals = window as {} as {
33  // tslint:disable-next-line no-any
34  dataLayer: any[];
35  gtag: (command: string, event: string|Date, args?: {}) => void;
36};
37
38export interface Analytics {
39  initialize(): void;
40  updatePath(_: string): void;
41  logEvent(_x: TraceCategories|null, _y: string): void;
42  logError(_x: string, _y?: boolean): void;
43}
44
45export class NullAnalytics implements Analytics {
46  initialize() {}
47  updatePath(_: string) {}
48  logEvent(_x: TraceCategories|null, _y: string) {}
49  logError(_x: string) {}
50}
51
52class AnalyticsImpl implements Analytics {
53  private initialized_ = false;
54
55  constructor() {
56    // The code below is taken from the official Google Analytics docs [1] and
57    // adapted to TypeScript. We have it here rather than as an inline script
58    // in index.html (as suggested by GA's docs) because inline scripts don't
59    // play nicely with the CSP policy, at least in Firefox (Firefox doesn't
60    // support all CSP 3 features we use).
61    // [1] https://developers.google.com/analytics/devguides/collection/gtagjs .
62    gtagGlobals.dataLayer = gtagGlobals.dataLayer || [];
63
64    // tslint:disable-next-line no-any
65    function gtagFunction(..._: any[]) {
66      // This needs to be a function and not a lambda. |arguments| behaves
67      // slightly differently in a lambda and breaks GA.
68      gtagGlobals.dataLayer.push(arguments);
69    }
70    gtagGlobals.gtag = gtagFunction;
71    gtagGlobals.gtag('js', new Date());
72  }
73
74  // This is callled only after the script that sets isInternalUser loads.
75  // It is fine to call updatePath() and log*() functions before initialize().
76  // The gtag() function internally enqueues all requests into |dataLayer|.
77  initialize() {
78    if (this.initialized_) return;
79    this.initialized_ = true;
80    const script = document.createElement('script');
81    script.src = 'https://www.googletagmanager.com/gtag/js?id=' + ANALYTICS_ID;
82    script.defer = true;
83    document.head.appendChild(script);
84    const route = globals.state.route || '/';
85    console.log(
86        `GA initialized. route=${route}`,
87        `isInternalUser=${globals.isInternalUser}`);
88    // GA's reccomendation for SPAs is to disable automatic page views and
89    // manually send page_view events. See:
90    // https://developers.google.com/analytics/devguides/collection/gtagjs/pages#manual_pageviews
91    gtagGlobals.gtag('config', ANALYTICS_ID, {
92      allow_google_signals: false,
93      anonymize_ip: true,
94      page_path: route,
95      referrer: document.referrer.split('?')[0],
96      send_page_view: false,
97      page_title: PAGE_TITLE,
98      dimension1: globals.isInternalUser ? '1' : '0',
99      dimension2: version.VERSION,
100      dimension3: globals.channel,
101    });
102    this.updatePath(route);
103  }
104
105  updatePath(path: string) {
106    gtagGlobals.gtag(
107        'event', 'page_view', {page_path: path, page_title: PAGE_TITLE});
108  }
109
110  logEvent(category: TraceCategories|null, event: string) {
111    gtagGlobals.gtag('event', event, {event_category: category});
112  }
113
114  logError(description: string, fatal = true) {
115    gtagGlobals.gtag('event', 'exception', {description, fatal});
116  }
117}
118