{-# LANGUAGE DeriveDataTypeable, PatternGuards, FlexibleContexts, TypeFamilies, OverloadedStrings, QuasiQuotes #-}
module HSP.Google.Analytics
( UACCT(..)
, analytics
, analyticsAsync
, universalAnalytics
) where
import Data.Generics (Data, Typeable)
import Data.Text.Lazy (Text,pack)
import HSP
import Prelude hiding (head)
import Language.Haskell.HSX.QQ (hsx)
newtype UACCT = UACCT String
deriving (Read, Show, Eq, Ord, Typeable, Data)
analyticsAsync :: (XMLGenerator m, StringType m ~ Text) =>
UACCT
-> GenXML m
analyticsAsync (UACCT uacct) = [hsx|
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<% pack uacct %>']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script> |]
universalAnalytics :: (XMLGenerator m, StringType m ~ Text) =>
UACCT
-> GenXML m
universalAnalytics (UACCT uacct) = [hsx|
<script type="text/javascript">
(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', '<% pack uacct %>', 'auto');
ga('send', 'pageview');
</script> |]
analytics :: (XMLGenerator m, StringType m ~ Text) => UACCT -> GenXMLList m
analytics (UACCT uacct) =
do a <- [hsx|
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script> |]
b <- [hsx|
<script type="text/javascript">
var pageTracker = _gat._getTracker("<% pack uacct %>");
pageTracker._initData();
pageTracker._trackPageview();
</script> |]
return [a,b]