UNPKG

@revenuegrid/churnzerojs

Version:

Typescript wrapper for ChurnZero JScript API

60 lines (59 loc) 2.2 kB
"use strict"; // https://support.churnzero.net/hc/en-us/articles/360004683552-Integrate-ChurnZero-using-Javascript Object.defineProperty(exports, "__esModule", { value: true }); exports.Client = void 0; const churnZeroAPI = window; class Client { constructor(methods) { this.methods = methods; } static async embedScript(url) { return new Promise((resolve, reject) => { const f = document.getElementsByTagName('script')[0], j = document.createElement('script'); j.async = true; j.src = url; j.crossOrigin = 'anonymous'; f.parentNode.insertBefore(j, f); j.onload = () => { resolve(); }; j.onerror = (event, source, lineno, colno, error) => { reject(new Error(`Failed to load ChurnZero script. ${typeof event == 'string' ? event : ''} ${error === null || error === void 0 ? void 0 : error.message}`)); }; }); } static async connect(config) { await Client.embedScript(config.url); const churnZero = churnZeroAPI.ChurnZero; if (!churnZero) throw new Error('ChurnZero object is not initialized by embedded script.'); churnZero.push(['setAppKey', config.apiKey]); churnZero.push(['setContact', config.accountId, config.contactId]); return new Client(churnZero); } trackEvent(...args) { this.methods.push(['trackEvent', ...args]); } setAttribute(entity, attributes) { this.methods.push(['setAttribute', entity, attributes]); } incrementAttribute(entity, name, value) { this.methods.push(['incrementAttribute', entity, name, value]); } setModule(...args) { this.methods.push(['setModule', ...args]); } urltracking(...args) { this.methods.push(['urltracking', ...args]); } silent(...args) { this.methods.push(['silent', ...args]); } open(...args) { this.methods.push(['open', ...args]); } close(...args) { this.methods.push(['close', ...args]); } } exports.Client = Client;