Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use google analytics from jade file

I want to track the users of my website. Since I do not have an old fashioned HTML file, should I adapt the given code to the jade syntax or can i leave the script untouched and include it somehow?

In case I need to convert it to jade syntax, can this be auto generated by some tool.

<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','//www.google-analytics.com/analytics.js','ga');    ga('create', 'UA-XXXXXXXX-X', 'domain.com');   ga('send', 'pageview'); </script> 
like image 818
NSMutableString Avatar asked Aug 13 '13 21:08

NSMutableString


2 Answers

Instead of having it look for another code file and load it. Inline (like Trevor suggested is better).

In order to accomplish this you have to make use of the script. tag.... not just script

See below:

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','//www.google-analytics.com/analytics.js','ga');              ga('create', 'UA-11111111-1', 'yourdomain.com');             ga('send', 'pageview'); 

Make sure the spacing and such is actually indented one tab from your stuff

like image 111
BRogers Avatar answered Sep 25 '22 17:09

BRogers


The solution was easy.

Step 1: i created a file called 'analytics.js' and placed the code between the script tags in it. No conversion to jade syntax needed.

Step 2: i referenced the script from the jade file

script(src='/js/analytics.js') 
like image 33
NSMutableString Avatar answered Sep 25 '22 17:09

NSMutableString