Class: Matomo

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

Constant Summary collapse

SETUP_CODE =
"""
<!-- Matomo -->

<!-- Matomo Image Tracker -->
<noscript>
  <img src=\"//%{url}?idsite=%{siteId}&amp;rec=1\" style=\"border:0\" alt=\"\" />
</noscript>
<!-- End 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.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/analytics/Matomo.rb', line 34

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



48
49
50
# File 'lib/analytics/Matomo.rb', line 48

def render
    return SETUP_CODE % @config
end