• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {getCurrentChannel} from '../common/channels';
16import {VERSION} from '../gen/perfetto_version';
17
18import {globals} from './globals';
19import {Router} from './router';
20
21type TraceCategories = 'Trace Actions'|'Record Trace'|'User Actions';
22const ANALYTICS_ID = 'UA-137828855-1';
23const PAGE_TITLE = 'no-page-title';
24
25export function initAnalytics() {
26  // Only initialize logging on the official site and on localhost (to catch
27  // analytics bugs when testing locally).
28  // Skip analytics is the fragment has "testing=1", this is used by UI tests.
29  // Skip analytics in embeddedMode since iFrames do not have the same access to
30  // local storage.
31  if ((window.location.origin.startsWith('http://localhost:') ||
32       window.location.origin.endsWith('.perfetto.dev')) &&
33      !globals.testing && !globals.embeddedMode) {
34    return new AnalyticsImpl();
35  }
36  return new NullAnalytics();
37}
38
39const gtagGlobals = window as {} as {
40  dataLayer: any[];
41  gtag: (command: string, event: string|Date, args?: {}) => void;
42};
43
44export interface Analytics {
45  initialize(): void;
46  updatePath(_: string): void;
47  logEvent(_x: TraceCategories|null, _y: string): void;
48  logError(_x: string, _y?: boolean): void;
49  isEnabled(): boolean;
50}
51
52export class NullAnalytics implements Analytics {
53  initialize() {}
54  updatePath(_: string) {}
55  logEvent(_x: TraceCategories|null, _y: string) {}
56  logError(_x: string) {}
57  isEnabled(): boolean {
58    return false;
59  }
60}
61
62class AnalyticsImpl implements Analytics {
63  private initialized_ = false;
64
65  constructor() {
66    // The code below is taken from the official Google Analytics docs [1] and
67    // adapted to TypeScript. We have it here rather than as an inline script
68    // in index.html (as suggested by GA's docs) because inline scripts don't
69    // play nicely with the CSP policy, at least in Firefox (Firefox doesn't
70    // support all CSP 3 features we use).
71    // [1] https://developers.google.com/analytics/devguides/collection/gtagjs .
72    gtagGlobals.dataLayer = gtagGlobals.dataLayer || [];
73
74    function gtagFunction(..._: any[]) {
75      // This needs to be a function and not a lambda. |arguments| behaves
76      // slightly differently in a lambda and breaks GA.
77      gtagGlobals.dataLayer.push(arguments);
78    }
79    gtagGlobals.gtag = gtagFunction;
80    gtagGlobals.gtag('js', new Date());
81  }
82
83  // This is callled only after the script that sets isInternalUser loads.
84  // It is fine to call updatePath() and log*() functions before initialize().
85  // The gtag() function internally enqueues all requests into |dataLayer|.
86  initialize() {
87    if (this.initialized_) return;
88    this.initialized_ = true;
89    const script = document.createElement('script');
90    script.src = 'https://www.googletagmanager.com/gtag/js?id=' + ANALYTICS_ID;
91    script.defer = true;
92    document.head.appendChild(script);
93    const route = Router.parseUrl(window.location.href).page || '/';
94    console.log(
95        `GA initialized. route=${route}`,
96        `isInternalUser=${globals.isInternalUser}`);
97    // GA's reccomendation for SPAs is to disable automatic page views and
98    // manually send page_view events. See:
99    // https://developers.google.com/analytics/devguides/collection/gtagjs/pages#manual_pageviews
100    gtagGlobals.gtag('config', ANALYTICS_ID, {
101      allow_google_signals: false,
102      anonymize_ip: true,
103      page_path: route,
104      referrer: document.referrer.split('?')[0],
105      send_page_view: false,
106      page_title: PAGE_TITLE,
107      dimension1: globals.isInternalUser ? '1' : '0',
108      dimension2: VERSION,
109      dimension3: getCurrentChannel(),
110    });
111    this.updatePath(route);
112  }
113
114  updatePath(path: string) {
115    gtagGlobals.gtag(
116        'event', 'page_view', {page_path: path, page_title: PAGE_TITLE});
117  }
118
119  logEvent(category: TraceCategories|null, event: string) {
120    gtagGlobals.gtag('event', event, {event_category: category});
121  }
122
123  logError(description: string, fatal = true) {
124    gtagGlobals.gtag('event', 'exception', {description, fatal});
125  }
126
127  isEnabled(): boolean {
128    return true;
129  }
130}
131