/lovable-integrations

Lovable and Facebook Ads integration: Step-by-Step Guide 2025

Discover how to integrate Lovable with Facebook Ads to elevate your marketing strategy. Follow our step-by-step guide for a seamless connection and optimized campaigns.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to integrate Lovable with Facebook Ads?

 

Creating the Facebook Ads Service File

 
  • Create a new file named facebookAds.ts in your project’s source folder (for example, under src/).
  • This file will define a FacebookAdsService class that loads the Facebook Pixel script and allows you to track events.

export class FacebookAdsService {
  private pixelId: string;

  constructor(pixelId: string) {
    this.pixelId = pixelId;
  }

  // Initialize the Facebook Pixel by injecting its script into the document.
  public init(): void {
    if ((window as any).fbq) {
      return;
    }
    (function(f: any, b: any, e: any, v: any, n?: any, t?: any, s?: any) {
      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 = true;
      n.version = '2.0';
      n.queue = [];
      t = b.createElement(e);
      t.async = true;
      t.src = v;
      s = b.getElementsByTagName(e)[0];
      s.parentNode.insertBefore(t, s);
    }(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js'));

    // Initialize the pixel with your unique Pixel ID.
    (window as any).fbq('init', this.pixelId);
    (window as any).fbq('track', 'PageView');
  }

  // Use this method to track custom events.
  public trackEvent(event: string, parameters?: any): void {
    (window as any).fbq('track', event, parameters);
  }
}

 

Inserting the Facebook Pixel Base Code into HTML

 
  • Locate your main HTML template file (for example, index.html).
  • Add the Facebook Pixel base code inside the <head> section. This snippet ensures that even before your TypeScript code loads, Facebook can start tracking page views.

<!-- Facebook Pixel Base Code - Place this inside the <head> tag -->
<script>
  !function(f,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');
  fbq('init', 'YOURPIXELID_HERE');
  fbq('track', 'PageView');
</script>
<noscript>
  <img height="1" width="1" style="display:none"
       src="https://www.facebook.com/tr?id=YOURPIXELID_HERE&ev=PageView&noscript=1"/>
</noscript>

 

Initializing the Facebook Ads Service in Your Application

 
  • Open your main application file (for example, app.ts or index.ts).
  • Import the FacebookAdsService, instantiate it with your Facebook Pixel ID, and call the init() function when the application starts.

// Import the FacebookAdsService from its file
import { FacebookAdsService } from './facebookAds';

// Replace 'YOURPIXELID_HERE' with your actual Facebook Pixel ID.
const fbAdsService = new FacebookAdsService('YOURPIXELID_HERE');

// Initialize Facebook Pixel tracking
fbAdsService.init();

// Example usage: Track a custom event when a user action happens.
function onUserAction() {
  fbAdsService.trackEvent('CustomEvent', { customData: 'example' });
}

// Call onUserAction() wherever necessary in your code.

 

Adding Dependencies Without a Terminal

 
  • Since Lovable does not have terminal access, dependencies must be added manually.
  • If your project already uses a package.json file, open it and add any missing dependencies under the dependencies section. For this integration no additional npm packages are required because the Facebook Pixel is loaded directly from Facebook’s servers.
  • If you ever need to add a dependency manually, simply add it into your package.json file and then Lovable will install it when you run your project.

 

Verifying the Integration

 
  • After saving all changes, load your Lovable project in a web browser.
  • Open your browser's developer console and look for error messages that might indicate issues with loading the Facebook Pixel script.
  • Use Facebook's Pixel Helper (a browser extension) to verify that your Pixel events are being sent properly.

Still stuck?
Copy this prompt into ChatGPT and get a clear, personalized explanation.

This prompt helps an AI assistant understand your setup and guide you through the fix step by step, without assuming technical knowledge.

AI AI Prompt

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022