pyc-website

main website for pyc inc.

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

commit 8b37d11181d8cfa7c6b966241ebc1fa551d2ab6b
parent a5f3474c56e96b970d353a2292479149732ae161
Author: Jul <jul@9o.is>
Date:   Wed,  5 Mar 2014 21:18:24 -0500

added google analytics

Diffstat:
Msrc/main/resources/props/production.default.props | 5+++--
Msrc/main/scala/bootstrap/liftweb/Boot.scala | 2++
Asrc/main/scala/com/pyd/config/GoogleAnalytics.scala | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/src/main/resources/props/production.default.props b/src/main/resources/props/production.default.props @@ -9,4 +9,6 @@ mongo.default.host= mongo.default.port= mongo.default.name= mongo.default.user= -mongo.default.pwd= -\ No newline at end of file +mongo.default.pwd= + +google.analytics.id= diff --git a/src/main/scala/bootstrap/liftweb/Boot.scala b/src/main/scala/bootstrap/liftweb/Boot.scala @@ -86,6 +86,8 @@ class Boot extends Loggable { // Mailer Mailer.devModeSend.default.set((m: MimeMessage) => logger.info("Dev mode message:\n" + prettyPrintMime(m))) Mailer.testModeSend.default.set((m: MimeMessage) => logger.info("Test mode message:\n" + prettyPrintMime(m))) + + GoogleAnalytics.init } private def prettyPrintMime(m: MimeMessage): String = { diff --git a/src/main/scala/com/pyd/config/GoogleAnalytics.scala b/src/main/scala/com/pyd/config/GoogleAnalytics.scala @@ -0,0 +1,61 @@ +package com.pyd.config + +import net.liftweb.util.Props +import net.liftweb.common.Loggable +import net.liftweb.http.js.JsCmd +import net.liftweb.http.Req +import net.liftweb.http.LiftSession +import net.liftweb.http.S + +/* + * Taken from: + * https://github.com/d6y/liftmodules-googleanalytics/blob/master/src/main/scala/bootstrap/liftmodules/GoogleAnalytics.scala + * + * Needed Google Analytics Universal for Async. + * TODO: switch to module after enhancement has been implemented. (issue #4) + * https://github.com/d6y/liftmodules-googleanalytics/issues/4 + */ +object GoogleAnalytics extends Loggable { + + def init: Unit = init( ()⇒true ) + + def init(includeTest: () ⇒ Boolean): Unit = Props.get("google.analytics.id") map Async.headJs foreach { js => + def addTracking(s: LiftSession, r: Req) : Unit = if (includeTest()) S.putInHead(js) + LiftSession.onBeginServicing = addTracking _ :: LiftSession.onBeginServicing + } + + // noticeJs is by-name to allow you to side-effect (eg., set cookies) + def alertUser(cond: () ⇒ Boolean)(noticeJs: ⇒ JsCmd): Unit = { + + def addNotice(s: LiftSession, r: Req) : Unit = try { + if (cond()) S.appendJs(noticeJs) + } catch { + case e : Throwable => logger.error("Unhandled exception from alertUser", e) + } + + LiftSession.onBeginServicing = addNotice _ :: LiftSession.onBeginServicing + } + + object dsl { + object only { + def when(f: => Boolean) = f _ + } + } + + +} + +object Async { + + def headJs(id: String): xml.Elem = xml.XML.loadString(s""" +<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', '$id', 'auto'); +ga('send', 'pageview'); +</script> + """) +} +\ No newline at end of file