Blog

Creating a Custom Facebook Audience by Passing Variables from Your Website with Google Tag Manager

By
Tim Flint
August 27, 2024
Share this post
Flint Analytics + Inc. 5000 custom graphic

Motivation

Let’s say your company owns 10 car dealerships in 3 different cities. Dealerships A, B, & C are located in City 1, Dealerships D, E, & F are located in City 2, and Dealerships G, H, I, & J are located in City 3. Each of these dealerships offers 3 types of car: Trucks, Sedans, and Minivans.

Let’s say a potential customer visits Dealership A’s website and looks at a Sedan, but does not convert. We might assume that the user is looking for a Sedan in City 1, but has not yet made a decision on which Sedan to buy. Using a custom Facebook audience, we could show this user an ad containing links to the different Sedan offerings at Dealerships A, B & C, since they are all located in City 1. Using the Data Layer and Facebook’s Conversion Pixel, this is actually a fairly simple setup.

Define Variables with Data Layer and Google Tag Manager

For our example, we really only need to know two things:

1. The city where the dealership is located

2. The car type that the user viewed

Let’s define these variables as dealerCity and carType. You will need to pass these variables to the data layer, which is a subject you can learn more about here.

Now you will need to create these variables within Google Tag Manager. Navigate to the User-Defined Variables box in the Variables tab, and click New. Assuming that the values of these variables will be available in your site’s data layer, you can choose the Data Layer Variable configuration type, and simply input the variable name in the field.

Note: If you are having trouble getting your data layer to populate properly, but you can define either of these variables with URL information (such as a query string containing something like ?dealerCity=Indianapolis&&carType=Sedan), then you might try creating a lookup table instead of a data layer variable. You can read more about the lookup table macro here and here.

Facebook Pixel Tag

If you have not yet created your Facebook pixel, read how to do so here.

Now you’ll need to add your Facebook Pixel as a tag in GTM. Create a new tag and name this tag Facebook Pixel All Pages, or, whatever naming convention you’d like to use to indicate that this is your base tag for Facebook. Paste the code generated by Facebook into a Custom HTML Tag Configuration, and set this tag to fire on all pages. This will ensure that Facebook’s pixel is always active and ready to pass data back to your Facebook Ad Account.

Tag for Custom Facebook Event

Now we want to create a custom event tag that passes your custom data into Facebook. Facebook uses the fbq call to indicate data arrays, and the trackCustom call for passing non-standard arrays (Facebook supports 9 standard data types, which you can read about here).

The code below is intended to send a City_Cars event array to your Facebook conversion pixel, containing the variables dealerCity and carType. Within Facebook, the variables dealercity_id and cartype_id will carry the data layer values in {{dealerCity}} and {{carType}}.

<script>
fbq('track','City_Cars', {
dealercity_id: '{{dealerCity}}',
cartype_id: '{{carType}}'
});
</script>

<noscript>
<img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=1111111111111111&ev=City_Cars&cd[dealercity_id]={{dealerCity}}&cd[cartype_id]={{carType}}&noscript=1"
/></noscript>

Note that you must define these variables in both the fbq call and the query string on the URL within the <noscript> section. The number contained in id=1111111111111111 will be equal to the ID number in your standard Facebook conversion pixel. Set ev= to the name of the event (City_Cars in our case), and then follow the format &cd[variable_id]={{GTM variable name}} for all values contained within the fbq array.

Under Tag Sequencing, make sure that your Facebook Pixel All Pages tag is firing first. This is important, as your Facebook Pixel must be active before data can be passed through it.

Set Up Custom Audience in Facebook

Let’s now create the remarketing list for potential customers looking for Sedans in City 1.

Go to the Audiences section of your Ads Manager and click Create Audience. Under the Website Traffic dropdown, choose Custom Combination. Then, under the Include drop-down, choose Event, and enter City_Cars in the Choose an event field. Now you will be able to add the individual parameters dealercity_id and cartypes_id and choose the values City 1 and Sedan, respectively.

Now you can use your new audience to target potential buyers who are interested in finding a Sedan in City 1. You can also set up an audience for each city and car type combination, and set up ads that target each unique pairing.

 

Photo Credit: 123rf.com/graphiqa

Let us solve your Facebook advertising problem.

Share this post
Tim Flint
Founder

Contact Flint Today

Ready For A New Digital Marketing Partner?

We would love to discuss how we can help you reach your digital marketing goals.  Just call us at (317) 993-3411 or send in the following form today!