0

I'm working on a project where I need to trigger specific Google Tag Manager (GTM) events only when a user is coming from Facebook. The goal is to track certain actions (e.g., InitiateCheckout) in a way that ensures they are only recorded if the user originated from Facebook. I've encountered challenges with the accuracy of using the HTTP referrer method and am looking for a more reliable solution.

Here's the approach I've been experimenting with:

  1. I use the standard Facebook Pixel tracking code to track the InitiateCheckout event.
  2. In addition to the standard tracking, I've attempted to leverage a custom JavaScript function to send a custom event to GTM's data layer. This is intended to allow GTM to trigger specific tags based on this event.

Here's the code snippet:

<script>
// Standard Facebook Pixel tracking for InitiateCheckout
fbq('track', 'InitiateCheckout');

// Additionally, call the custom function to send the event to GTM's data layer
trackFBEvent('InitiateCheckout', {});
</script>

<script>
// Define a function to handle Facebook events and push them to GTM's data layer
window.trackFBEvent = function(eventName, eventParams) {
    dataLayer.push({
        'event': 'fb_initiate_checkout', // Custom event name for GTM trigger
        'fbEventName': eventName, // Pass the Facebook event name
        'fbEventParams': eventParams // Pass the event parameters, if any
    });
};
</script>

The challenge with this method is that it always fires, regardless of whether the user is coming from Facebook, because it relies on the trigger of InitiateCheckout, which is not exclusive to Facebook referrals.

I'm seeking advice on a better approach to:

  • Accurately identify and trigger events in GTM only for users coming specifically from Facebook. Ensure that my tracking setup does not capture events from users who did not originate from Facebook.

  • What are the best practices or alternative methods that could help achieve this with greater accuracy? Any insights or suggestions would be greatly appreciated!

1 Answer 1

0

I think the problem is not the code.

It's the trigger so we can filter the user coming from facebook.


First we need to define what is coming from facebook:

  • FB Post?
  • FB Login?
  • FB FanPage?
  • FB Ads?

Second is do we have the solution to identify the user coming from above:

  • UTM?
  • Other specific url parameter?
  • Referral?

Let's say we already solve those 2 parts

We might have the rule like:

  1. When the referral is *facebook.com*
  2. When the url has parameter fbclid

Then create a trigger for allPage view.

create a customHtml tag to check the rules

<script>
    var referrer = document.referrer;
    var urlParams = new URLSearchParams(window.location.search);

    if (referrer.indexOf("facebook.com") !== -1 || urlParams.has("fbclid")){
        localStorage.setItem("fromFb", "true"); 
    }
</script>

Then create another trigger to check the localStorage fromFb is true.

There is a downside for this method.

Once the user is coming from facebook so the browser will remember it all the time.

Not sure it fits your requirement or not and you need to make the decision.

If no then we need to modify how it store or set a time to the value.

Not the answer you're looking for? Browse other questions tagged or ask your own question.