I am currently making and operating the web using the angular 11 version. For page data analysis, I had to add marketing-related scripts to the head. When I searched it, I found out that I could add scripts to components using rederer2 and Inject, and I added them to the ngOninit part as below
import { Component, OnInit, Renderer2, Inject } from '@angular/core';
import { DOCUMENT } from '@angular/common';
@Component({
selector: 'app-teamroom-apply',
templateUrl: './teamroom-apply.component.html',
styleUrls: ['./teamroom-apply.component.css']
})
export class TeamroomApplyComponent implements OnInit {
constructor(
private renderer2: Renderer2,
@Inject(DOCUMENT) private _document
) {
const r = this.renderer2.createElement('script');
r.type = 'text/javascript';
r.src = '//wcs.naver.net/wcslog.js'
this.renderer2.appendChild(this._document.head, r);
}
ngOnInit(): void {
const s = this.renderer2.createElement('script');
s.type = 'text/javascript';
s.text = `
var _nasa={};
if(window.wcs) _nasa["cnv"] = wcs.cnv("5","1");
`;
this.renderer2.appendChild(this._document.head, s);
}
}
And after building, I deployed it, but it was inserted into the head twice, so I keep getting errors
Can I know how to fix it?
I think, when build, something is done twice. But I don't know for sure

Check if the script exists before adding which can be done with
document.head.querySelectorStackblitz
main.ts
index.html