pyc-website

main website for pyc inc.

git clone https://9o.is/git/pyc-website.git

GoogleAnalytics.scala

(2807B)


      1 package inc.pyc
      2 package config
      3 
      4 import net.liftweb.util.Props
      5 import net.liftweb.common._
      6 import net.liftweb.http.js.JsCmd
      7 import net.liftweb.http.Req
      8 import net.liftweb.http.LiftSession
      9 import net.liftweb.http.S
     10 
     11 /*
     12  * Taken from:
     13  * https://github.com/d6y/liftmodules-googleanalytics/blob/master/src/main/scala/bootstrap/liftmodules/GoogleAnalytics.scala
     14  * 
     15  * Needed Google Analytics Universal for Async.
     16  * TODO: switch to module after enhancement has been implemented. (issue #4)
     17  * https://github.com/d6y/liftmodules-googleanalytics/issues/4
     18  */
     19 object GoogleAnalytics extends Loggable {
     20   
     21   val id: Box[String] = Props.get("google.analytics.id")
     22 
     23   def init: Unit = init( ()⇒true )
     24 
     25   def init(includeTest: () ⇒ Boolean): Unit = id map Async.headJs foreach { js =>
     26     def addTracking(s: LiftSession, r: Req) : Unit = if (includeTest()) S.putInHead(js)
     27     LiftSession.onBeginServicing = addTracking _ :: LiftSession.onBeginServicing
     28   }
     29 
     30   // noticeJs is by-name to allow you to side-effect (eg., set cookies)
     31   def alertUser(cond: () ⇒ Boolean)(noticeJs: ⇒ JsCmd): Unit = {
     32 
     33     def addNotice(s: LiftSession, r: Req) : Unit = try {
     34         if (cond()) S.appendJs(noticeJs)
     35       } catch {
     36         case e : Throwable => logger.error("Unhandled exception from alertUser", e)
     37     }
     38 
     39     LiftSession.onBeginServicing = addNotice _ :: LiftSession.onBeginServicing
     40   }
     41 
     42   object dsl {
     43     object only {
     44       def when(f: => Boolean) = f _
     45     }
     46   }
     47   
     48   def angularInit: Unit = angularInit( ()⇒true ) _
     49 
     50   /**
     51    * Configures an angular module with angular-google-analytics
     52    * https://github.com/revolunet/angular-google-analytics
     53    */
     54   def angularInit(includeTest: () ⇒ Boolean)(moduleName: String = "app", analytics: Boolean = true): Unit = id foreach { id =>
     55     val config =
     56       moduleName + """.config(function(AnalyticsProvider) {
     57         AnalyticsProvider.setAccount('"""+id+"""');
     58         AnalyticsProvider.useAnalytics("""+analytics.toString+""");
     59         AnalyticsProvider.setPageEvent('$stateChangeSuccess');
     60         });
     61       """ +
     62       moduleName + ".run(function(Analytics){});"
     63     
     64     def addTracking(s: LiftSession, r: Req) : Unit = 
     65       if (includeTest()) S.putAtEndOfBody(<script>{config}</script>)
     66     LiftSession.onBeginServicing = addTracking _ :: LiftSession.onBeginServicing
     67   }
     68 
     69 }
     70 
     71 object Async {
     72   
     73   def headJs(id: String): xml.Elem = xml.XML.loadString(s"""
     74 <script>
     75 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
     76 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
     77 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
     78 })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
     79 
     80 ga('create', '$id', 'auto');
     81 ga('send', 'pageview');
     82 </script>
     83   """)
     84 }