Search code examples
javascriptjavahtmlspringthymeleaf

Thymeleaf throwing error on Google Analytics Code


Im trying to add Google Analytics to my webpage but Thymeleaf is throwing the error org.xml.sax.SAXParseException: Referenz zu Entity "l" muss mit dem Begrenzungszeichen ";" enden.
Translation: after "l" the entitity must end with ";"

Any Ideas how to solve this Problem?

<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-5SNB8LZ')

The error refers to this line of the Google Analytics Code

j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=

Solution

  • Try this:

    <script type="text/javascript" th:inline="javascript">
      /*<![CDATA[*/
    ... Your script
      /*]]>*/
    </script>