Quiero agregar condiciones para cargar este script en la etiqueta principal. Necesito verificar si la URL es de nuestro dominio en vivo, entonces solo se ejecuta de lo contrario, no. Simplemente podría agregar este script al encabezado si no hubiera un requisito de condición. ¿Cómo puedo inyectar este script en la etiqueta principal en angular?
Por ejemplo:
window.origin == 'https://www.stackoverflow.com' luego inyecte este script en la etiqueta principal, de lo contrario no.
<script> (function(h,o,t,j,a,r){ h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)}; h._hjSettings={hjid:*******,hjsv:*}; a=o.getElementsByTagName('head')[0]; r=o.createElement('script');r.async=1; r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv; a.appendChild(r); })(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv='); </script>Puede manejar esto en uno de sus componentes, preferiblemente en app.component. Por supuesto, podría verificar la URL, pero sería más elegante tener algún indicador como enableHotjarTracking en su archivo environment.ts y configurarlo como verdadero solo para su entorno de producción.
Entonces podría llamarlo de la siguiente manera en su app.component.ts
import { environment } from './environments/environment'; import { DOCUMENT } from '@angular/common'; ... // inject document wrapper constructor( @Inject(DOCUMENT) private document: Document, ){} ngAfterContentInit() { if(environment.enableHotjarTracking) { ((h, o, t, j, a, r) => { h.hj = h.hj || /* tslint:disable:only-arrow-functions */ function () { (h.hj.q = h.hj.q || []).push(arguments); }; h._hjSettings = { hjid: xxxx, hjsv: x }; a = o.getElementsByTagName('head')[0]; r = o.createElement('script'); r.async = 1; r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv; a.appendChild(r); })(window as any, this.document, 'https://static.hotjar.com/c/hotjar-', '.js?sv='); } }También puede mantener su identificación de hotjar en el archivo de entorno.