Adflix Consulting | Web Analytics & Data Tracking for Measurable Growth

How to Capture fbclid and Store in Cookie & LocalStorage with Google Tag Manager

Introduction

If you’re running Meta (Facebook) Ads and relying on accurate conversion tracking, capturing fbclid is critical.

Many businesses face this issue:

  • Users click on ads
  • Visit the website
  • Make a purchase

But…

The conversion doesn’t show properly in Ads Manager

This leads to:

  • Inaccurate ROAS
  • Poor campaign optimization
  • Lost revenue opportunities

 

The root cause?

fbclid is not being captured and persisted properly.

In this guide, I’ll show you a complete implementation using Google Tag Manager (GTM) so you can:

  • Capture fbclid from URL
  • Store it in cookie + localStorage
  • Use it later for Conversion API & event tracking

 

What is fbclid? 

fbclid (Facebook Click Identifier) is a unique parameter Meta automatically adds to your URL when someone clicks your ad.

https://example.com/?fbclid=IwAR123ABCxyz

This value helps Meta understand which ad click led to a conversion.

 

Why Capturing fbclid is Important ?

If you don’t capture fbclid: 

  • Meta can’t attribute conversions correctly 
  • Your campaigns look less profitable than they actually are 
  • Optimization becomes unreliable

 

Especially important for: 

  • Multi-step funnels 
  • Cross-domain setups (e.g., Wix → Kajabi) 
  • Server-side tracking (CAPI)

 

Implementation Overview: We’ll do 3 things: 

  1. Capture fbclid from URL 
  2. Store it in localStorage + cookie 
  3. Create GTM variable for reuse

 

🔹 STEP 1: Create URL Variable (fbclid) In GTM: 

Go to Variables → New Variable 

Type: URL 

Component Type: Query 

Query Key: fbclid

 

Variable Name: URL – fbclid

 

👉 This variable will extract fbclid from the URL.

 

🔹 STEP 2: Create Custom HTML Tag (Store fbclid) 

Tag Type: 👉 Custom HTML

<script>
(function() {
  try {
    var fbclid = {{URL – fbclid}};
   
    if (fbclid) {
      // Store in localStorage
      localStorage.setItem(‘fbclid’, fbclid);

      // Store in cookie (30 days)
      document.cookie = “fbclid=” + fbclid + “; path=/; max-age=2592000”;

      console.log(‘fbclid captured:’, fbclid);
    }
  } catch(e) {}
})();
</script>

🔹 STEP 3: Create Trigger 

Trigger Type:  All Pages – DOM Ready 

 

Why DOM Ready? 

  • Ensures URL is fully available 
  • Safe execution timing

 

🔹 STEP 4: Create Variable (Retrieve fbclid from localStorage) 

Variable Type:  Custom JavaScript

Variable Name: JS – fbclid (localStorage)

function() {
  return localStorage.getItem(‘fbclid’);
}

 

🔹 STEP 5:  Create Variable (Retrieve fbclid from Cookie) 

Variable Type:  1st Party Cookie 

Cookie Name: fbclid

Variable Name: Cookie – fbclid

 

🔹 STEP 6: Generate _fbc Value (Important for Meta CAPI) 

Meta expects a specific format for _fbc:

fb.1.<timestamp>.<fbclid>

Now create a Custom JavaScript Variable:

Variable Name: JS – _fbc

function() {
  var fbclid = localStorage.getItem(‘fbclid’);
  if (!fbclid) return;

  return “fb.1.” + Date.now() + “.” + fbclid;
}

🔹 STEP 7: Use in Events (Example: Purchase Event) 

 

You can now pass this data into: 

Meta Pixel / CAPI: fbc → {{JS – _fbc}} 

GA4: Custom parameter → fbclid

 

📊 Result After Implementation Once implemented correctly: 

  1. Meta can match conversions accurately 
  2. Attribution improves 
  3. Event Match Quality increases 
  4. Better optimization signals

 

Your ads start optimizing based on real data, not partial data

 

Most businesses focus on ads… 

But the real problem often happens after the click 

 

If your data is broken, your decisions will be too 

Fixing fbclid tracking is one of the highest leverage improvements you can make in your funnel.

 

👋 Need Help? 

If you’re facing issues like: 

Missing conversions in Meta 

Poor attribution 

Broken tracking across platforms 

 

I can help you fix your tracking system end-to-end.

 

📩 Feel free to reach out: 

Book A Callhttps://adflix.net/contact/

Email: arafat@adflix.net

LinkedIn: https://www.linkedin.com/in/adflixconsulting/

WhatsApp: https://wa.link/w83l5u

Track. Measure. Scale 

Smarter Tracking > Clearer Decisions > Higher ROI

Leave a Reply

Your email address will not be published. Required fields are marked *