如何在我的angular4应用中添加自定义数据层?测试我只将脚本附加到了标记的末尾,我想将脚本添加到顶部,如
之后的第二行这是我创建的服务,我也不知道这是否是最佳实践
import { Inject, Injectable } from '@angular/core';
import { DOCUMENT} from '@angular/platform-browser';
@Injectable()
export class DataLayerService {
private headElement: HTMLElement;
constructor(
@Inject(DOCUMENT) private document,
) {
this.headElement = this.document.head;
}
public setDataLayerContent() {
var element: HTMLElement;
element = this.document.write(`
<script>
dataLayer = [{
'pageCategory': 'test name',
'visitorType': 'any-value'
}];
</script>
`);
this.headElement.appendChild(element);
}
}
和index.html
<!DOCTYPE html>
<html lang="en">
<head>
<base href="/">
<!-- the script to be injected here -->
<script async src="https://www.googletagmanager.com...........
有什么想法吗?