<commit_before>// Docs on GA's event interface: https://developers.google.com/analytics/devguides/collection/analyticsjs/events
interface GAEvent {
    hitType: string
    eventCategory: string
    eventAction: string
    eventLabel?: string
    eventValue?: number
}

export class Analytics {
    static logChartError(error: any, info: any) {
        this.logToAmplitude("CHART_ERROR", { error, info })
        this.logToGA("Errors", "Chart")
    }

    static logExploreError(error: any, info: any) {
        this.logToAmplitude("EXPLORE_ERROR", { error, info })
        this.logToGA("Errors", "Explore")
    }

    static logChartTimelinePlay(slug?: string) {
        this.logToAmplitude("CHART_TIMELINE_PLAY")
        this.logToGA("Chart", "TimelinePlay", slug)
    }

    static logPageNotFound(url: string) {
        this.logToAmplitude("NOT_FOUND", { href: url })
        this.logToGA("Errors", "NotFound", url)
    }

    static logChartsPageSearchQuery(query: string) {
        this.logToAmplitude("Charts Page Filter", { query })
        this.logToGA("ChartsPage", "Filter", query)
    }

    static logGlobalEntityControl(
        action: "open" | "change" | "close",
        note?: string
    ) {
        this.logToAmplitude("GLOBAL_ENTITY_CONTROL", { action, note })
        this.logToGA("GlobalEntityControl", action, note)
    }

    static logSiteClick(text: string, href?: string, note?: string) {
        this.logToAmplitude("OWID_SITE_CLICK", {
            text,
            href,
            note
        })
        this.logToGA("SiteClick", note || "unknown-category", text)
    }

    static logPageLoad() {
        this.logToAmplitude("OWID_PAGE_LOAD")
    }

    private static logToAmplitude(name: string, props?: any) {
        props = Object.assign(
            {},
            {
                context: {
                    pageHref: window.location.href,
                    pagePath: window.location.pathname,
                    pageTitle: document.title.replace(/ - [^-]+/, "")
                }
            },
            props
        )
        if (window.amplitude)
            window.amplitude.getInstance().logEvent(name, props)
    }

    private static logToGA(
        eventCategory: string,
        eventAction: string,
        eventLabel?: string,
        eventValue?: number
    ) {
        const event: GAEvent = {
            hitType: "event",<commit_msg>Log Analytics events in development<commit_after>import { ENV } from "settings"

const IS_DEV = ENV === "development"

// Docs on GA's event interface: https://developers.google.com/analytics/devguides/collection/analyticsjs/events
interface GAEvent {
    hitType: string
    eventCategory: string
    eventAction: string
    eventLabel?: string
    eventValue?: number
}

export class Analytics {
    static logChartError(error: any, info: any) {
        this.logToAmplitude("CHART_ERROR", { error, info })
        this.logToGA("Errors", "Chart")
    }

    static logExploreError(error: any, info: any) {
        this.logToAmplitude("EXPLORE_ERROR", { error, info })
        this.logToGA("Errors", "Explore")
    }

    static logChartTimelinePlay(slug?: string) {
        this.logToAmplitude("CHART_TIMELINE_PLAY")
        this.logToGA("Chart", "TimelinePlay", slug)
    }

    static logPageNotFound(url: string) {
        this.logToAmplitude("NOT_FOUND", { href: url })
        this.logToGA("Errors", "NotFound", url)
    }

    static logChartsPageSearchQuery(query: string) {
        this.logToAmplitude("Charts Page Filter", { query })
        this.logToGA("ChartsPage", "Filter", query)
    }

    static logGlobalEntityControl(
        action: "open" | "change" | "close",
        note?: string
    ) {
        this.logToAmplitude("GLOBAL_ENTITY_CONTROL", { action, note })
        this.logToGA("GlobalEntityControl", action, note)
    }

    static logSiteClick(text: string, href?: string, note?: string) {
        this.logToAmplitude("OWID_SITE_CLICK", {
            text,
            href,
            note
        })
        this.logToGA("SiteClick", note || "unknown-category", text)
    }

    static logPageLoad() {
        this.logToAmplitude("OWID_PAGE_LOAD")
    }

    private static logToAmplitude(name: string, props?: any) {
        if (IS_DEV) {
            console.log("Analytics.logToAmplitude", name, props)
        }
        props = Object.assign(
            {},
            {
                context: {
                    pageHref: window.location.href,
                    pagePath: window.location.pathname,
                    pageTitle: document.title.replace(/ - [^-]+/, "")
                }
            },
            props
        )
        if (window.amplitude)
            window.amplitude.getInstance().logEvent(name, props)
    }

    private static logToGA(
        eventCategory: string,
        eventAction: string,
        eventLabel?: string,
        eventValue?: number
    ) {
        if (IS_DEV) {
            console.log(
                "Analytics.logToGA",
                eventCategory,
                eventAction,
                eventLabel,
                eventValue
            )
        }
        const event: GAEvent = {
            hitType: "event",
{'fraction_non_alphanumeric': 0.11771472392638037, 'fraction_numerical': 0.0, 'mean_word_length': 2.088809946714032, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6167281', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1511, 'n_words': 295}