简体   繁体   中英

Google Tag Manager: How to use “Custom Javascript” in a “Custom HTML Tag?”

I have a custom javascript variable that is checking to see what the eventAction is so I can know whether to fire some conversion pixels or not. The custom Javascript is called "FacebookConversion" and looks like this:

function () {
  if({{eventAction}} == "completedSignUp" || {{eventAction}} == "upgrade" || {{eventAction}} == "frontUpgrade"){
    fbq('track', 'Purchase', {value: '{{eventValue}}', currency: 'USD'});
  }
  if({{eventAction}} == "submittedEnterpriseContactForm"){
    fbq('track', 'Lead');
  }
  console.log("HELLO");
  return;

}

Then I made a custom tag that fires on a page where the eventAction conversion takes place. The custom HTML tag is my conversion pixel and looks something like this:

<script>
!function(f,b,e,v,n,t,s){....facebook code....);

fbq('init', 'xxxxxx');
fbq('track', "PageView");
  {{FacebookConversion}}
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=xxxxxx&ev=PageView&noscript=1"
/></noscript>

But this doesn't seem to be working. Am I doing this right? Can I call {{FacebookConversion}} within my Custom Tag and have it run the custom Javascript?

Your methodology for firing these conditional tags is all wrong and you need to reconsider how you use Google Tag manager. There is absolutely no need to use a custom variable in this setup.

To achieve your desired setup, you need 3 custom HTML tags each being fired off different triggers:

Tag 1 - Main FB Pageview Tag

This is the main pageview tag that fires on 'All Pages'

<script>
  !function(f,b,e,v,n,t,s){....facebook code....);

  fbq('init', 'xxxxxx');
  fbq('track', "PageView");
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=xxxxxx&ev=PageView&noscript=1"
/></noscript>

Tag 2 - Lead Tag

This is fired by a Trigger you need to create of type = custom event and with value = "submittedEnterpriseContactForm"

<script>
  fbq('track', 'Lead');
</script>

Tag 3 - conversion Tag

This is fired by any of 3 Triggers you need to create of type = custom event and with value = "completedSignUp" or value="upgrade" or value="frontUpgrade"

<script>
   fbq('track', 'Purchase', {value: '{{eventValue}}', currency: 'USD'});
</script>

And that's it. The triggers decide when to fire tags so no need for any additional logic. You may need to play about with the variables you use in the conversion tag to get the values but thats pretty much setup dependent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM