|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { Context } from "./context.js"; |
| 6 | +import EventMetricType from "./metrics/types/event.js"; |
| 7 | +import { EVENTS_PING_NAME } from "./constants.js"; |
| 8 | +import { Lifetime } from "./metrics/lifetime.js"; |
| 9 | +import log, { LoggingLevel } from "./log.js"; |
| 10 | + |
| 11 | +const LOG_TAG = "core.glean_metrics"; |
| 12 | + |
| 13 | +interface PageLoadParams { |
| 14 | + url?: string; |
| 15 | + referrer?: string; |
| 16 | + title?: string; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * This namespace contains functions to support Glean's auto-instrumentation |
| 21 | + * capability. These functions are either running automatically if enabled |
| 22 | + * via the Glean config or they are called manually by the client. |
| 23 | + */ |
| 24 | +namespace GleanMetrics { |
| 25 | + // Object to hold all automatic instrumentation metrics. |
| 26 | + const metrics = { |
| 27 | + pageLoad: new EventMetricType( |
| 28 | + { |
| 29 | + category: "glean", |
| 30 | + name: "page_load", |
| 31 | + sendInPings: [EVENTS_PING_NAME], |
| 32 | + lifetime: Lifetime.Ping, |
| 33 | + disabled: false, |
| 34 | + }, |
| 35 | + // Page load extras defined in `src/metrics.yaml`. |
| 36 | + ["url", "referrer", "title"] |
| 37 | + ) |
| 38 | + }; |
| 39 | + |
| 40 | + /** |
| 41 | + * For a standard web project `initialize` is called every time a page |
| 42 | + * loads. For every page load if the client has auto page loads enabled, |
| 43 | + * we will record a page load event. |
| 44 | + * |
| 45 | + * @param overrides Overrides for each page_load extra key. |
| 46 | + */ |
| 47 | + export function pageLoad(overrides?: PageLoadParams) { |
| 48 | + // Cannot record an event if Glean has not been initialized. |
| 49 | + if (!Context.initialized) { |
| 50 | + log( |
| 51 | + LOG_TAG, |
| 52 | + "Attempted to record a page load event before Glean was initialized. This is a no-op.", |
| 53 | + LoggingLevel.Warn |
| 54 | + ); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + // Each key defaults to the override. If no override is provided, we fall |
| 59 | + // back to the default value IF the `window` or the `document` objects |
| 60 | + // are available. |
| 61 | + // |
| 62 | + // If neither of those are available, then we default to a value that shows |
| 63 | + // that no value is available. |
| 64 | + metrics.pageLoad.record({ |
| 65 | + url: |
| 66 | + overrides?.url ?? (typeof window !== "undefined" |
| 67 | + ? window.location.href |
| 68 | + : "URL_NOT_PROVIDED_OR_AVAILABLE" |
| 69 | + ), |
| 70 | + referrer: |
| 71 | + overrides?.referrer ?? (typeof document !== "undefined" |
| 72 | + ? document.referrer |
| 73 | + : "REFERRER_NOT_PROVIDED_OR_AVAILABLE" |
| 74 | + ), |
| 75 | + title: |
| 76 | + overrides?.title ?? (typeof document !== "undefined" |
| 77 | + ? document.title |
| 78 | + : "TITLE_NOT_PROVIDED_OR_AVAILABLE" |
| 79 | + ), |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +export default GleanMetrics; |
0 commit comments