Alaa
Alaa

Reputation: 21

Cannot pull Data Layer Variable in Google Tag Manager

I want to implement Facebook Advanced Matching Manually using Google Tag Manager. I did the following:

On GTM, I did the following:

  1. Created a variable named AdvancedMatchingVariable of the type Data Layer Variable with Variable Name advancedMatching
  2. I Updated Facebook Base Code Tag to include the new variable as follows:

<!-- Facebook Pixel Code -->
<script>
...
fbq('init', 'pixel id',
  '{{AdvancedMatchingVariable}}'
);
</script>
<!-- End Facebook Pixel Code -->

On my Website, I added this code:

dataLayer.push({
  'advancedMatching': {
      'em': '$email',
      'fn': '$fname',
      'ln': '$lname'
     }
});

However, It didn't work. I get Object Object in the facebook pixel base code. As the following:

<!-- Facebook Pixel Code -->
<script>
fbq('init', '*ID*', 
    Object Object
  );
</script>
<!-- End Facebook Pixel Code -->

So please what is the problem with my code? I have been searching and trying to fix it for hours!

Upvotes: 0

Views: 591

Answers (2)

XTOTHEL
XTOTHEL

Reputation: 5208

You just need to modify your pixel code to remove the quotes around like so:

<!-- Facebook Pixel Code -->
<script>
...
fbq('init', 'pixel id',
  {{AdvancedMatchingVariable}}
);
</script>
<!-- End Facebook Pixel Code -->

Upvotes: 0

Beck Johnson
Beck Johnson

Reputation: 1210

I think you have to parse the values of the advancedMatching object yourself in the FB pixel, like

fbq('init', 'pixel id', {
    em: advancedMatching.em,
    fn: advancedMatching.fn,
    ln: advancedMatching.ln
});

You could probably write a helper function to map this for you, if you have dynamic tracking parameters in advancedMatching.

Upvotes: 1

Related Questions