Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Facebook advertising tracking pixels in Angular 2 / 4

I'm trying to work out how I use the Facebook tracking pixel with my angular project.

I have added the base pixel into my index.html

but I want to fire an event from the javascript, how do I fire this

fbq('track', 'CompleteRegistration');

Angular will not compile when I have this in my code as it can't find fbq

like image 290
James Lewis Avatar asked Mar 05 '18 22:03

James Lewis


People also ask

Why Google Analytics and Facebook pixel doesn't work with an angular app?

An Angular app is a Single Page Application (SPA) hence Google Analytics and Facebook Pixel won’t work efficiently with it. Angular CLI. Firebase CLI. A Firebase Project (assume the namespace is sample-app-xxxx).

How do I use the Facebook pixel?

It’ll be automatically logged in the Event Manager section of your Facebook pixel page. Through the Event Manager, you can see all your customers' actions and you can also retarget them with Facebook ads. Once the Pixel is active, it allows you to achieve the following: Tracking and Retargeting

What is the Facebook pixel for ads?

The Advantage of using Facebook Pixel for Ads The Facebook Pixel is triggered by cookies placed on your sites, which your audience or customers have to activate. This activation typically happens when a customer takes action, which Facebook Pixel will record as an Event.

How many events can I add to my Facebook pixel?

These 17 events are the most common actions that customers take when they visit sites; hence they are a good starting point if you are just getting started with your Facebook Pixel. You can use these events to create campaigns and ad sets with specific ads.


3 Answers

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).

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

} 

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.

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.

like image 196
A. Beach Avatar answered Sep 21 '22 10:09

A. Beach


create one service and call it in your 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');
        }
    }
like image 20
Sourav Raj Avatar answered Sep 19 '22 10:09

Sourav Raj


add this in ngOnInit() if you have already added to index.html

(window as any).fbq('track', 'CompleteRegistration'); // this Worked for me
like image 39
Himanshu Patni Avatar answered Sep 20 '22 10:09

Himanshu Patni