在Angular 2/4中使用Facebook广告跟踪像素 [英] Using Facebook advertising tracking pixels in Angular 2 / 4
问题描述
我正在尝试确定我如何在我的角度项目中使用Facebook跟踪像素.
I'm trying to work out how I use the Facebook tracking pixel with my angular project.
我已将基本像素添加到我的index.html
I have added the base pixel into my index.html
但是我想通过javascript触发事件,如何触发
but I want to fire an event from the javascript, how do I fire this
fbq('track', 'CompleteRegistration');
当我在代码中包含它时,Angular将无法编译,因为它找不到fbq
Angular will not compile when I have this in my code as it can't find fbq
推荐答案
即使fbq
在运行时在您的应用程序中全局可用,编译器也不知道,因为它是在index.html
中定义的(外部编译器已知的打字稿文件).
Even though fbq
is globally-available in your app at runtime, the compiler doesn't know about it because it is defined in index.html
(outside of the typescript files known to the compiler).
要解决此问题,请在使用fbq
的文件中添加环境声明,如下所示:
To fix this, add an ambient declaration to the files where you use fbq
, like so:
declare const fbq: any; // 1. add an ambient declaration
@Component({...})
export class ComponentUsingFacebookPixel {
fbq('track', 'CompleteRegistration'); // 2. This will compile now
}
这不会更改已编译的javascript或覆盖fbq
的值;向编译器保证在运行时将提供该名称的值.
This doesn't change the compiled javascript or overwrite the value of fbq
; it's a promise to the compiler that there will be a value by that name available at runtime.
如果需要编译器的其他帮助,则可以提供比any
更具体的类型,但是any
足以避免出现编译错误.
You can provide a more specific type than any
if you'd like additional help from the compiler, but any
is sufficient to avoid the compilation error you're seeing.
这篇关于在Angular 2/4中使用Facebook广告跟踪像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!