UNPKG

@ima/plugin-analytic-fb-pixel

Version:

Seznam IMA.js analytic plugin for Facebook Pixel

175 lines (174 loc) 5.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "FacebookPixelAnalytic", { enumerable: true, get: function() { return FacebookPixelAnalytic; } }); const _pluginanalytic = require("@ima/plugin-analytic"); const FB_ROOT_VARIABLE = 'fbq'; class FacebookPixelAnalytic extends _pluginanalytic.AbstractAnalytic { #config; // A main function of Facebook Pixel. #fbq; static get $dependencies() { return [ '$Settings.plugin.analytic.fbPixel', ..._pluginanalytic.AbstractAnalytic.$dependencies ]; } /** * Creates a Facebook Pixel Helper instance. */ constructor(config, ...rest){ super(...rest); this._analyticScriptName = 'fb_pixel'; this._analyticScriptUrl = '//connect.facebook.net/en_US/fbevents.js'; this.#config = config; this.#fbq = null; } _applyPurposeConsents() { /* this implementation of FB pixel doesn't work with consents */ } /** * Gets the identifier for Facebook Pixel. * * @returns The identifier for Facebook Pixel. */ getId() { switch(typeof this.#config.id){ case 'number': return String(this.#config.id); case 'string': return this.#config.id; default: throw new TypeError('A Facebook Pixel identifier should be a number/string.'); } } /** * Hits an event. * * @override * @param eventName Name of the event. * @param eventData Data attached to the event. * @returns TRUE when event has been hit; otherwise FALSE. */ hit(eventName, eventData = null) { try { if (!this.#fbq) { throw new Error('Initialize the FacebookPixelHelper instance before calling hit() method.'); } else if (typeof eventName !== 'string' || !eventName) { throw new TypeError('Parameter eventName of hit() method is required and should be a string.'); } else if (typeof eventData !== 'object') { throw new TypeError('Parameter eventData of hit() method should be an object.'); } } catch (error) { this._processError(error); return false; } if (!eventData) { this.#fbq('track', eventName); } else { this.#fbq('track', eventName, eventData); } return true; } /** * Hits a page view event (optionally with page view data). * * @override * @param Page view data (containing path etc.). * @returns TRUE when event has been hit; otherwise FALSE. */ hitPageView(viewContentData = null) { try { if (!this.#fbq) { throw new Error('Initialize the FacebookPixelHelper instance before calling hitPageView() method.'); } else if (typeof viewContentData !== 'object') { throw new TypeError('Parameter data of hitPageView() method should be an object.'); } } catch (error) { this._processError(error); return false; } const hitResult = this.hit('PageView'); if (!hitResult) { return false; } else if (viewContentData) { return this.hit('ViewContent', viewContentData); } return true; } /** * Hits a search event (optionally with page name or other event data). * * @param Search query / event data. * @param queryOrData * @returns TRUE when event has been hit; otherwise FALSE. */ hitSearch(queryOrData = null) { try { if (!this.#fbq) { throw new Error('Initialize the FacebookPixelHelper instance before calling hitSearch() method.'); } else if ([ 'string', 'object' ].indexOf(typeof queryOrData) === -1) { throw new TypeError('Parameter queryOrData of hitSearch() method should be a string or an object.'); } } catch (error) { this._processError(error); return false; } let eventData; if (typeof queryOrData === 'string' && queryOrData) { eventData = { search_string: queryOrData }; } else { return this.hit('Search'); } return this.hit('Search', eventData); } /** * @override * @inheritdoc */ _configuration() { // _configuration is only called on client, therefore window is defined const clientWindow = this._window.getWindow(); if (this.isEnabled() || !clientWindow[FB_ROOT_VARIABLE] || typeof clientWindow[FB_ROOT_VARIABLE] !== 'function') { return; } this._enable = true; this.#fbq = clientWindow[FB_ROOT_VARIABLE]; this.#fbq('init', this.getId()); } /** * @override * @inheritdoc */ _createGlobalDefinition(window) { if (window[FB_ROOT_VARIABLE]) { return; } const fbAnalytic = window[FB_ROOT_VARIABLE] = function(...rest) { fbAnalytic.callMethod // eslint-disable-line @typescript-eslint/no-unused-expressions ? fbAnalytic.callMethod(...rest) : fbAnalytic.queue.push(...rest); }; if (!window[`_${FB_ROOT_VARIABLE}`]) { window[`_${FB_ROOT_VARIABLE}`] = fbAnalytic; } fbAnalytic.push = fbAnalytic; fbAnalytic.loaded = false; fbAnalytic.version = '2.0'; fbAnalytic.queue = []; this.#fbq = fbAnalytic; this._configuration(); } /** * Processes an error. * * @param error An error to be processed. */ _processError(error) { if ($Debug && error) { console.error(error); } } } //# sourceMappingURL=FacebookPixelAnalytic.js.map