Thursday, July 20, 2017

Add Google Analytics to OJS (Smarty Templates)

The Open Journal Systems (OJS) https://openjournalsystems.com/ is constructed with Smarty Templates https://www.smarty.net/.

Google Analytics instructs you to paste the tracking script directly before the </head> tag.  The </head> tag for ALL OJS pages is located in the ojs/lib/pkp/templates/common/header.tpl page.

So I tried this and it didn't work.  This is because the Smarty tempalte engine attempts to translate and interpret some keywords in Google Analytics script that conflicts with the Smarty template delimiter syntax.

The fix is to put {literal} and {/literal} tags around your block of code, which allows it to be taken literally.

So, this is what it looks like:
{literal}
     <script>
       (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
       (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
       m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
       })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

       ga('create', 'XXXXXXX', 'auto');

       ga('send', 'pageview');

     </script>

{/literal}

Obviously you would want to replace the XXXXXXX with your actual google analytics tracking code.  😁

No comments:

Post a Comment

Get the OpenURL of an Item in Primo New UI

I needed to view the OpenURL of an item in Primo New UI so that I could troubleshoot why one of our Alma General Electronic Services (ILLiad...