Javascript FBQ(Facebook事件)和Angular 4
我已经在HTML中插入了Facebook像素,我需要从角度组件调用FBQ函数。但是,我在调用window.fbq尝试注册事件时遇到问题,bbq在类型“window”上不存在 有人实施过吗?你知道如何从角度组件调用index.html中的js函数吗Javascript FBQ(Facebook事件)和Angular 4,javascript,facebook,angular,Javascript,Facebook,Angular,我已经在HTML中插入了Facebook像素,我需要从角度组件调用FBQ函数。但是,我在调用window.fbq尝试注册事件时遇到问题,bbq在类型“window”上不存在 有人实施过吗?你知道如何从角度组件调用index.html中的js函数吗 谢谢大家! 答案可能来得有点晚,但我是如何实现的: 我在index.html中添加了facebook像素代码。然后我创建了一个处理跟踪的服务: import { Injectable } from '@angular/core'; declare c
谢谢大家! 答案可能来得有点晚,但我是如何实现的: 我在index.html中添加了facebook像素代码。然后我创建了一个处理跟踪的服务:
import { Injectable } from '@angular/core';
declare const fbq;
@Injectable()
export class FacebookPixelEventTrackerService {
constructor() { }
public trackEvent(properties: Object) {
if (typeof fbq === 'undefined') {
return;
}
fbq('track', 'ViewContent', properties);
}
}
在需要的地方注入服务,一切都会顺利进行: 答案可能来得有点晚,但我是如何实现的: 我在index.html中添加了facebook像素代码。然后我创建了一个处理跟踪的服务:
import { Injectable } from '@angular/core';
declare const fbq;
@Injectable()
export class FacebookPixelEventTrackerService {
constructor() { }
public trackEvent(properties: Object) {
if (typeof fbq === 'undefined') {
return;
}
fbq('track', 'ViewContent', properties);
}
}
在需要的地方注入服务,一切都会顺利进行: 创建一个服务并在app.component.ts中调用它
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class FacebookPixelService {
constructor() { }
load() {
(function (f: any, b, e, v, n, t, s) {
if (f.fbq) return; n = f.fbq = function () {
n.callMethod ?
n.callMethod.apply(n, arguments) : n.queue.push(arguments)
}; if (!f._fbq) f._fbq = n;
n.push = n; n.loaded = !0; n.version = '2.0'; n.queue = []; t = b.createElement(e); t.async = !0;
t.src = v; s = b.getElementsByTagName(e)[0]; s.parentNode.insertBefore(t, s)
})(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js');
(window as any).fbq.disablePushState = true; //not recommended, but can be done
(window as any).fbq('init', '<your id>');
(window as any).fbq('track', 'PageView');
}
}
创建一个服务并在app.component.ts中调用它
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class FacebookPixelService {
constructor() { }
load() {
(function (f: any, b, e, v, n, t, s) {
if (f.fbq) return; n = f.fbq = function () {
n.callMethod ?
n.callMethod.apply(n, arguments) : n.queue.push(arguments)
}; if (!f._fbq) f._fbq = n;
n.push = n; n.loaded = !0; n.version = '2.0'; n.queue = []; t = b.createElement(e); t.async = !0;
t.src = v; s = b.getElementsByTagName(e)[0]; s.parentNode.insertBefore(t, s)
})(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js');
(window as any).fbq.disablePushState = true; //not recommended, but can be done
(window as any).fbq('init', '<your id>');
(window as any).fbq('track', 'PageView');
}
}
因此,如果Facebook像素脚本此时还没有加载,这将根本无法跟踪页面视图。。。?我不确定这是否是解决当前问题的正确“解决方案”。因此,如果此时没有加载Facebook pixel脚本,这将根本无法跟踪页面视图。。。?我不确定我会称之为手头问题的正确“解决方案”