Class: GoogleAnalytics

Inherits:
Object
  • Object
show all
Defined in:
lib/analytics/GoogleAnalytics.rb

Constant Summary collapse

SETUP_CODE =
"""
<!-- Google Analytics -->
<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','https://www.google-analytics.com/analytics.js','ga');

%s
</script>
<!-- End Google Analytics -->
"""
ID_RE =
/^UA-\d+-\d+$/
INITIALIZE_CODE =
"ga('create', '%s', 'auto');"
PAGEVIEW_CODE =
"ga('send', 'pageview', { 'page': location.pathname + location.search + location.hash});"
ANONYMIZE_IP_CODE =
"ga('set', 'anonymizeIp', %s);"

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ GoogleAnalytics

Returns a new instance of GoogleAnalytics.



23
24
25
26
27
28
29
30
31
32
# File 'lib/analytics/GoogleAnalytics.rb', line 23

def initialize(config)
    if !(ID_RE.match(config["id"]))
        raise ArgumentError, 'Invalid Google analytics key. Id must look like UA-XXXXXX-Y'
    end

    @commands = []
    @commands.push(INITIALIZE_CODE % config["id"])
    @commands.push(PAGEVIEW_CODE)
    _get_other_commands(config)
end

Instance Method Details

#renderObject



34
35
36
# File 'lib/analytics/GoogleAnalytics.rb', line 34

def render()
    return SETUP_CODE % @commands.join("\n\t")
end