I've been trying to follow this post r.e setting up Facebook Pixel, but getting a few errors when compiling - I've commented inline where it's flagging the errors.
Here is my facebook-pixel-service.ts file:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',
})
export class FacebookPixelService {
private loadOk = false;
constructor() {}
load() {
if (!this.loadOk) {
(function(f: any, b, e, v, n, t, s) { // Getting 'non-arrow functions are forbidden' warning
if (f.fbq) {
return;
}
n = f.fbq = function() { // Getting Type '() => void' is not assignable to type 'undefined'
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); // Getting Type 'HTMLElement' is not assignable to type 'undefined'
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('init', {fbPixelId});
(window as any).fbq('track', 'PageView');
this.loadOk = true;
console.log('Facebook pixel init run!');
} else {
(window as any).fbq('track', 'PageView');
console.log('Facebook PageView event fired!');
}
}
}
I've implemented the suggested service and provider files, however I'm getting these errors when compiling the app:
1.) 'non-arrow functions are forbidden' warning (RESOLVED)
2.) Type '() => void' is not assignable to type 'undefined' (commented inline)
3.) Type 'HTMLElement' is not assignable to type 'undefined' (commented inline)
4.) For every instance of each function param is being assigned, it's throwing Object is possibly 'undefined'
Thanks in advance
EDIT - 30/03/2020
Changing (function(f: any, b, e, v, n, t, s) {
to ((f: any, b, e, v, n, t, s) => {
and n = f.fbq = function() {
to n = f.fbq = () => {
has resolved the non-arrow warning.
All errors are now resolved by adjusting the signature to follow TypeScripts optional chaining, as mentioned here
So the final signature should look something like this:
((f: any, b, e, v, n?: any, t?: any, s?: any) => {