javascriptclojureclojurescripthoplon

Translate javascript to Clojurescript


Basically, I'm new to programming and was told to start with Clojure and Hoplon (which, uses Clojurescript). I'm trying to translate the Google Analytics code to clojurescript, and I realized I have no idea how to go about it. Can someone please translate this code? I'd also really appreciate an explanation of how to figure this stuff out on my own, if possible. I appreciate any help I can get on this. (I've searched and all I've found were little snippets of translations, but never the whole thing, so I don't know how to implement it.)

<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', 'GOOGLE-ANALYTICS-ID', 'auto');
  ga('send', 'pageview');

</script>

Again, thanks for any help.


Solution

  • You don't need to port that code necessarily (or even preferably). Here as an example a snippet paste of the 'learndatalogtoday' project :

    https://github.com/jonase/learndatalogtoday/blob/master/src/clj/learndatalogtoday/views.clj

    (def google-analytics-string
      "(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-40247950-2', 'learndatalogtoday.org');
      ga('send', 'pageview');")
    

    and later on :

      (html5
       [:head
        (include-css "/third-party/bootstrap/css/bootstrap.css")
        (include-css "/style.css")
        [:title "Learn Datalog Today!"]
        [:script google-analytics-string]]