Class: Piwik

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

Constant Summary collapse

SETUP_CODE =
"""
<!-- Piwik -->
<script type=\"text/javascript\">
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u='//'+\"%{url}\";
_paq.push(['setTrackerUrl', u+'/piwik.php']);
_paq.push(['setSiteId', '%{siteId}']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'/piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Piwik Code -->
"""
DOMAINPATH_RE =

domain name (characters separated by a dot), optional port, optional URI path, no slash

/^(([^.\/?#@:]+\.)*[^.\/?#@:]+)+(:[0-9]+)?(\/[^\/?#@:]+)*$/
SITEID_RE =

numeric ID

/^\d+$/

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Piwik

Returns a new instance of Piwik.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/analytics/Piwik.rb', line 26

def initialize(config)
    
    if !(DOMAINPATH_RE.match(config['url']))
        raise ArgumentError, 'Invalid url. Must be a domain name, optionally followed by an URI path, no trailing slash (e.g. piwik.example.com or my.piwik.server/path)'
    end

    if !(SITEID_RE.match(config['siteId']))
        raise ArgumentError, 'Invalid site id. Must be a number.'
    end
    
    @config = Hash[config.map{ |k, v| [k.to_sym, v.to_s] }]

end

Instance Method Details

#renderObject



40
41
42
# File 'lib/analytics/Piwik.rb', line 40

def render
    return SETUP_CODE % @config
end