Google Analytics - tracking code script implementation

Google Analytics - tracking code script implementation

  

Hey folks,

I have a responsive web app and want to include the <script> tracking code from Google Analytics.

I have a shared FOOTER web block that I figured would be great to put it so I get the basics of page-level tracking. 

HOWEVER, I tried using the Development\JS widget from SilkUI but it does not like the format of the Google code (see below) when I paste it into the JavaScript window.

QUESTION: Is there a smarter way to integrate Google Analytics? Any clues why the Google tracking code below gives me an error?


<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', 'UA-87647101-2', 'auto');
  ga('send', 'pageview');
---------------------------------------------------
</script>

Hi Bruce,

I pasted your code into a JS widget without any problems. Did you enclose the code in quotation marks ("")?

WOW! Okay, I think I've spent far too many late nights low-coding in OutSystems!

Yup, I totally missed enclosing it in quotes!

Okay ... well, I still need some help. Yes, putting quotes worked.

Now, for some reason I can't get this JS code to actually appear in the generated pages! I've tried using the SilkUI\Script widget - but - I think that just puts the tags around it. I don't think it puts it INTO the page.

So, then I tried to drop the JS into a simple page in the ADVANCED JavaScript section of the page. Still no luck.

Not sure if it has to be wrapped in <script> tag or not and not sure if needs to be enclosed in quotes or not.


BOTTOM LINE ... SOOOO ... what am I missing here? How to I force a chunck of JS to appear in a page, better yet - in EVERYPAGE?


Thanks!

Hi,

You can try creating a webblock and adding an expression to it with the following content (don't forget to set the expression's "Escape Content" to No):

"
<script type='text/javascript'>
    (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', 'UA-87647101-2', 'auto');
    ga('send', 'pageview');
</script>
"

Then just drop this block in the pages where you need the script. If it's every page in your application, you can drop the block inside another block that is already being used by all your pages, like a header or a menu.

Hi Bruce,

You can add this to the module javascript. Select the module node on the tree to the right of the studio, it should have the name of your app. Then in the properties below you'll find a javascript element, double click it and paste the google analytics code there. This javascript will be included in every page of this module. In this case you can really just paste the code, no need to quote or double quote since this is not an expression.

Cheers

Solution

André Vieira wrote:

Hi Bruce,

You can add this to the module javascript. Select the module node on the tree to the right of the studio, it should have the name of your app. Then in the properties below you'll find a javascript element, double click it and paste the google analytics code there. This javascript will be included in every page of this module. In this case you can really just paste the code, no need to quote or double quote since this is not an expression.

Cheers

Andre, would I need to enclose the JS code with a <script> tag?


Solution

Bruce,

No you don't.

André Vieira wrote:

Bruce,

No you don't.

That did the trick --- I just put the tracking JS in the top-level module JS and it worked.

Thanks!