UNPKG

@ima/plugin-analytic-google

Version:

Seznam IMA.js google analytic plugin

121 lines (120 loc) 4.06 kB
import { AbstractAnalytic } from '@ima/plugin-analytic'; const GTAG_ROOT_VARIABLE = 'gtag'; /** * Google analytic 4 class */ export class GoogleAnalytics4 extends AbstractAnalytic { #config; #referrer; _consentSettings; static get $dependencies() { return [ '$Settings.plugin.analytic.google4', ...AbstractAnalytic.$dependencies ]; } set _ga4Script(value) { const clientWindow = this._window.getWindow(); clientWindow[GTAG_ROOT_VARIABLE] = value; } get _ga4Script() { const clientWindow = this._window.getWindow(); return clientWindow[GTAG_ROOT_VARIABLE]; } get config() { return this.#config; } /** * Initializes the Google Analytics 4 plugin. */ constructor(config, ...rest){ super(...rest); this.#config = config; this.#referrer = ''; this._analyticScriptName = 'google_analytics_4'; this._analyticScriptUrl = `https://www.googletagmanager.com/gtag/js?id=${this.config.service}`; this._consentSettings = this.config.consentSettings; } /** * Hits custom event of given with given data */ hit(eventName, eventData) { if (!this.isEnabled()) { return; } this._ga4Script('event', eventName, eventData); } /** * Hit page view event to analytic with defined data. */ hitPageView(pageData) { if (!this.isEnabled()) { return; } const pageViewData = this._getPageViewData(pageData); this._ga4Script('event', 'page_view', pageViewData); this.#referrer = pageViewData.page_location; } /** * Updates user consents in Google Analytics script * * @param purposeConsents Purpose Consents of TCModel, see: https://www.npmjs.com/package/@iabtcf/core#tcmodel */ updateConsent(purposeConsents) { this._applyPurposeConsents(purposeConsents); this._ga4Script('consent', 'update', { ...this._consentSettings }); } /** * @override * @inheritdoc */ _applyPurposeConsents(purposeConsents) { if (purposeConsents && typeof purposeConsents === 'object' && this._consentSettings) { if (purposeConsents['1']) { this._consentSettings.analytics_storage = 'granted'; } else { this._consentSettings.analytics_storage = 'denied'; } } } /** * @override * @inheritdoc */ _configuration() { if (this.isEnabled() || !this._ga4Script || typeof this._ga4Script !== 'function') { return; } this._enable = true; this._ga4Script('consent', 'default', { ...this._consentSettings, wait_for_update: this.config.waitForUpdateTimeout }); this._ga4Script('js', new Date()); this._ga4Script('config', this.config.service, { send_page_view: false }); } /** * Returns page view data derived from pageData param. */ _getPageViewData(pageData) { const page_location = this._window?.getUrl(); const clientDocument = this._window?.getDocument(); const page_referrer = this.#referrer || clientDocument?.referrer || ''; return { page_path: pageData.path, page_location, page_referrer, page_route: pageData?.route?.getName() || '', page_status: pageData?.response?.status, page_title: clientDocument?.title || '' }; } /** * @override * @inheritdoc */ _createGlobalDefinition(window) { window.dataLayer = window.dataLayer || []; this._ga4Script = function() { // We are passing a set of arguments to the gtag function of GTM. This MUST be an `arguments` object, not an array. window.dataLayer.push(arguments); // eslint-disable-line prefer-rest-params }; this._configuration(); } } //# sourceMappingURL=GoogleAnalytics4.js.map