All files / app/services analyticsIntegration.service.ts

54.21% Statements 45/83
75% Branches 3/4
75% Functions 3/4
54.21% Lines 45/83

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 841x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 39x 39x 39x 39x 1x 1x 42x 42x     42x 42x                             42x 42x 1x 1x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 1x 1x                                             1x  
import { isPlatformBrowser } from '@angular/common';
import { Injectable, NgZone, PLATFORM_ID, Renderer2, RendererFactory2, inject } from '@angular/core';
import { environment } from '@environments/environment';
 
@Injectable({
	providedIn: 'root',
})
export class AnalyticsIntegrationService {
	private zone = inject(NgZone);
	private platformId = inject(PLATFORM_ID);
 
	private renderer: Renderer2;
	private checkInterval = 500;
	private chatWidgetId = 'hubspot-messages-iframe-container';
	private hasShownWidget = false;
 
	constructor() {
		const rendererFactory = inject(RendererFactory2);
 
		this.renderer = rendererFactory.createRenderer(null, null);
	}
 
	hideChatWidget() {
		// If the widget has already been shown, don't hide it again
		if (this.hasShownWidget) {
			return;
		}
 
		const interval = setInterval(() => {
			const chatWidget = document.getElementById(this.chatWidgetId);
			if (chatWidget) {
				chatWidget.style.opacity = '0';

				clearInterval(interval);

				this.zone.runOutsideAngular(() => {
					setTimeout(() => {
						chatWidget.style.opacity = '1';

						this.hasShownWidget = true;
					}, 20000);
				});
			}
		}, this.checkInterval);
	}
 
	loadScript() {
		if (isPlatformBrowser(this.platformId)) {
			this.hideChatWidget();
		}
		const s = this.renderer.createElement('script');
		s.type = 'text/javascript';
		s.src = '//js-eu1.hs-scripts.com/139818938.js';
		s.id = 'hs-script-loader';
		s.async = true;
		s.defer = true;
		this.renderer.appendChild(document.head, s);
	}
 
	loadBingAdsScript() {
		if (isPlatformBrowser(this.platformId)) {
			const bingScript = this.renderer.createElement('script');
			bingScript.type = 'text/javascript';
			bingScript.async = true;
			bingScript.src = '//bat.bing.com/bat.js';

			bingScript.onload = () => {
				const bingCode = environment.BINGADS_CODE;
				const uetq = (window as any).uetq || [];
				const o = { ti: bingCode, q: uetq };
				(window as any).uetq = new (window as any).UET(o);
				(window as any).uetq.push('pageLoad');
			};

			this.renderer.appendChild(document.head, bingScript);

			// Add <noscript> fallback manually if needed:
			const noscript = this.renderer.createElement('noscript');
			noscript.innerHTML = `<img src="//bat.bing.com/action/0?ti=${environment.BINGADS_CODE}&Ver=2" height="0" width="0" style="display:none; visibility: hidden;" />`;
			this.renderer.appendChild(document.body, noscript);
		}
	}
}