import { removeElement } from '../utils' export interface ConfigMeta { [key: string]: any } export interface ParamsMeta { [key: string]: any } export interface PageMeta { title?: string query?: { [key: string]: string | undefined } path?: string href?: string referrer?: string } export interface UserMeta { id: string | null name?: string [key: string]: any } export interface ScreenMeta { name: string app?: string } export interface EventMeta { action: string category?: string page?: string label?: string [key: string]: any } export interface BaseGather { setUser?(userMeta: UserMeta): void } export abstract class BaseGather { protected tagCalled = false public abstract init(): Promise public isInited(): boolean { return this.tagCalled } public $vendor: any public abstract config(configMeta: ConfigMeta): void public abstract page(pageMeta: PageMeta): void public abstract event(eventMeta: EventMeta): void public abstract screen(screenMeta: ScreenMeta): void protected createScript(src: string, id: string) { const script = document.createElement('script') script.async = true script.src = src if (id) { script.id = id } return script } protected loadScript( script: HTMLScriptElement, key: string, removeAfterLoad = true, onLoad: () => void ) { script.onerror = () => { window[key] = null removeElement(script) } script.onload = () => { if (onLoad) { onLoad() } if (removeAfterLoad) { removeElement(script) } } const firstScript = document.getElementsByTagName('script')[0] firstScript.parentNode?.insertBefore(script, firstScript) } }