1

I'm trying to put the following Facebook bot code into Google Tag Manager but am getting the "Invalid HTML, CSS, or JavaScript found in template." error. Any idea why?

<!-- Load Facebook SDK for JavaScript -->
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = 'https://connect.facebook.net/en_US/sdk/xfbml.customerchat.js#xfbml=1&version=v2.12&autoLogAppEvents=1';
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

<!-- Your customer chat code -->
<div class="fb-customerchat"
  attribution="setup_tool"
  page_id="1393263590925062">
</div>
1
  • The "why" is that Google does not consider "attribution" and "page_id" to be valid attributes for a div element. Since the implied question is "how do I make this work" this is not a sufficient answer, though (I assume simply removing the attributes will break the Facebook script) Commented May 28, 2018 at 15:36

2 Answers 2

7

GTM does not allow non-standard HTML attributes. Hence "attribution" and "page_id" in the HTML element will cause GTM throw error.

You can use Javascript to generate the chat code. Tested and it works.

<script>
  (function() {
    var el = document.createElement('div');
    el.className = 'fb-customerchat';
    el.setAttribute('page_id', '{{Constant - Facebook Page ID}}');
    el.setAttribute('attribution', 'setup_tool');
    document.body.appendChild(el);
  })();
</script>

p/s: You can replace the variable {{Constant - Facebook Page ID}} with the page ID. It is best practice to use user-defined constant variables, read here.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Works like a charm
great thank you! Not sure who's fault this is more FB or Google tag manager, but I'm thinking Google because the tag says "custom JS/HTML" and it being valid or not is kinda non of their business
0

I found that adding data- in front of each custom attribute also works (though I don't see it documented anywhere).

<div class="fb-customerchat"
  data-attribution="setup_tool"
  data-page_id="1393263590925062">
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.