Class: Matomo

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

Constant Summary collapse

SETUP_CODE =
"""
<!-- Matomo -->
<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 Matomo 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) ⇒ Matomo

Returns a new instance of Matomo.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/analytics/Matomo.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. matomo.example.com or my.matomo.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/Matomo.rb', line 40

def render
    return SETUP_CODE % @config
end