2

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.

Aaron
  • 801
  • 6
  • 13
  • 3
    Have you considered the idea of starting to learn Clojurescript? – ale Sep 07 '15 at 21:12
  • From what I understand, Hoplon is a subset of Clojurescript, in that it's built off of Clojurescript and anything written in Clojurescript still works in Hoplon. – Aaron Sep 07 '15 at 21:15
  • @Aaron, if Hoplon is a subset of Clojurescript, that implies that there's Clojurescript code that's not Hoplon code. Wouldn't there then be things written in Clojurescript that wouldn't work in Hoplon? (Not to be too nitpicky--I don't know anything about Hoplon.) – Mars Sep 07 '15 at 22:52
  • Aaron, some friendly tips: No one will translate code for you here. Consider yourself lucky that Peter gave you a workaround solution. People will give you tips about how to get started, as WinterMute did. You have to do the work, first. StackOverflow is a site for helping you when you're working at a problem and you get stuck in the middle and have a specific problem that you're trying to solve. – Mars Sep 07 '15 at 22:56
  • 1
    @Mars, No, if Hoplon is a subset of Clojurescript, that does not imply that there's Clojurescript code that's not Hoplon. It just implies that there is no Hoplon which is no Clojurescript. Your conclusion would be adequate if it was a strict subset. I do not know anything about Clojurescript, nor Hoplon, I just corrected a mistake regarding set-theory. – Lajos Arpad Sep 07 '15 at 23:00
  • In addition to WinterMute's suggestion, which is the important one, you should figure out what the Javascript code is doing. The first step is to format it properly. To solve this problem, you may need to learn both Javascript and Clojurescript, unfortunately. On the other hand, you might already know what the code Javascript code is supposed to do; in that case, maybe you can avoid learning Javascript. Well, no. It's probably hard to use Clojurescript without knowing some Javascript first. So, you have a lot of work to do--but it will be fun! – Mars Sep 07 '15 at 23:00
  • 1
    OK, @LajosArpad, fair enough. I am wrong if Hoplon as a language is identical to Clojurescript. :-) – Mars Sep 07 '15 at 23:01
  • @mars Yeah, I was actually hoping for more of a method on translating from JS to Clojurescript. Don't get me wrong, I appreciate the solution Peter gave (it actually works as a general purpose API paste-this-code type of solution, which is awesome). You know, sometimes you're trying to do something else entirely and don't really care about how something is getting done (like including MathJax or something). But yeah, I'm finding more and more that I do have to learn both. lol – Aaron Sep 07 '15 at 23:20
  • And @LajosArpad you're right. I meant superset, which was also wrong. lol – Aaron Sep 07 '15 at 23:22
  • Aaron, I understand. I bet it would be easier to translate Clojurescript to Javascript than the reverse. Javascript is designed to support a variety of programming styles, while Clojurescript is designed mainly for what's known as functional programming. This is fundamental flaw of Clojurescript for people who don't like functional programming, and wonderful virtue for people who feel that functional programming makes code easier to understand and maintain. Javascript code that's not already in a functional style might have to be completely reconceptualized for translation to Clojure. – Mars Sep 08 '15 at 00:30
  • I did not know that about having to re-conceptualize some Javascript programs in order to write them in Clojurescript... That would explain why I didn't find any explanations on how to translate from Javascript to Clojurescript: It's not always a direct translation! – Aaron Sep 08 '15 at 05:19

1 Answers1

4

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]]
Peter
  • 47,963
  • 46
  • 132
  • 181
  • Cool! Yeah, man. That was a great idea, and I think it worked. (the script is showing up, but GA isn't working yet. Probably something else going wrong though...) :-) I didn't even think to put it as a string! Thanks a lot! – Aaron Sep 07 '15 at 22:28