class Piwik::Site

Piwik::Site is a wrapper class used to expose a more ruby-friendly (and sane) interface to the Piwik API. The Piwik API is under development, and there are quite a few incosistencies that are ironed out when it is used through the Piwik::Site metaclass.

Example usage using the bundled terminal script. Uses the official Piwik demo server as a sandbox

$ ./script/terminal -u http://demo.piwik.org -t anonymous
> site = Piwik::Site.load(7)
=> #<Piwik::Site[snip]> 
> summary = site.visits.summary
=> #<Piwik::VisitsSummary[snip]>
> summary.bounce_rate
=> "68%"
> site.visits.count
=> 467
> site.annotations.all
=> #<Piwik::Annotations::All[snip]>
> site.annotations.add(:date => 'today', :note => 'twitter account online', :starred => '1')
=> Piwik::ApiError: The current user is not allowed to add notes for site #7
> site.referers.website_count
=> 12

This class creates site-aware proxies (called api_scopes) to the various client classes. This lets you call api methods for a selected site without having to resubmit the site id all the time. Furthermore, API methods are redefined as proxy_methods, allowing for ruby-friendlier names, default parameters and other nice things.

Public Class Methods

collection() click to toggle source
# File lib/piwik/site.rb, line 59
def collection
  Piwik::SitesManager
end
id_attr() click to toggle source
# File lib/piwik/site.rb, line 63
def id_attr
  :idSite
end

Public Instance Methods

give_admin_access_to(login) click to toggle source

Gives read and write access ('admin') for the supplied user login for the current site.

# File lib/piwik/site.rb, line 48
def give_admin_access_to(login)
  give_access_to(:admin, login)
end
give_no_access_to(login) click to toggle source

Removes all access (gives a 'noaccess') for the supplied user login for the current site.

# File lib/piwik/site.rb, line 53
def give_no_access_to(login)
  give_access_to(:noaccess, login)
end
Also aliased as: remove_access_for
give_view_access_to(login) click to toggle source

Gives read access ('view') to the supplied user login for the current site.

# File lib/piwik/site.rb, line 43
def give_view_access_to(login)
  give_access_to(:view, login)
end
remove_access_for(login)
Alias for: give_no_access_to
seo_info() click to toggle source

Returns search engine information for site home

# File lib/piwik/site.rb, line 38
def seo_info
  Piwik::SEO.getRank(:url => self.main_url)
end

Private Instance Methods

give_access_to(access, login) click to toggle source

Gives the supplied access for the supplied user, for the current site.

  • access can be one of :view, :admin or :noaccess

  • login is the user login on Piwik

# File lib/piwik/site.rb, line 73
def give_access_to(access, login)
  Piwik::UsersManager.setUserAccess(:idSites => id, :access => access.to_s, :userLogin => login.to_s).data
end