Learn R Programming

piwikproR

R package for accessing data from Piwik PRO via API.

PIWIK PRO is a tool to measure traffic of a website. They offer an API for fetching all metrics and dimensions so you can use them in your own program.

This R-Package uses the API to fetch the data from PIWIK PRO using R-code. You get the data as tibble (or as a data.frame). So it's easy to analyze the data with the whole power of R.

Installation

CRAN Version

The package is now available on CRAN. So you can install it by running

install.packages("piwikproR")

Development Version

Using devtools it's easy to install piwikproR:

devtools::install_github("dfv-ms/piwikproR")

Usage

Load the library

library(piwikproR)

Credentials for API, token

First you need to setup an access to the API. See here: [https://developers.piwik.pro/en/latest/platform/getting_started.html#create-api-credentials-and-an-access-token]

Let's say you put them into a list:

piwik_pro_credentials <- list(
  client_id = "my_client_id",
  client_secret = "my_client_secret",
  url = "https://my_site.piwik.pro"
 )

Using these credential you can fetch a token

token <- get_login_token(piwik_pro_credentials)

Website id

Set the website_id and the date range.

website_id <- 'my_website_id'
start.date <- "2021-04-01"
end.date <- "2021-04-30"

Defining the columns to be fetched

Now we define the columns we want to fetch. Here's an example:

We want to fetch the date, the url (only the path without the hostname) and the page_views:

columns <- tibble::tribble(
  ~column, ~transformation,
  "timestamp", "",
  "event_url", "to_path",
  "page_views", "",
)

Filters

We're only interested in -- let's say -- Desktop requests. So let's set a filter:

filters <- tibble::tribble(
 ~column, ~operator, ~value,
 "device_type", "eq", 0
)
filters <- build_filter(filters, "and")

Fetching the data

query <- build_query(lubridate::ymd(start.date), lubridate::ymd(end.date), website_id,
                    filters = filters,
                    columns, max_lines = 0
)
data <- send_query(query, token, caching = TRUE, fetch_by_day = FALSE)

Metrics and Dimensions Documentation

PIWIK PRO offers a great documentation of all metrics and dimensions starting here [https://developers.piwik.pro/en/latest/custom_reports/index.html]

Developing

I'm using unit tests to test my code. But these tests run against a special website_id whose data is not publicly available. So I put all those sensitive data into a private package piwikproRTests. If this package is not available all tests are skipped.

Issues

If you find a bug or if you have a feature request feel free and open an issue

Documentation

Further documentation can be found here: https://PiwikProR.rstats-tips.net/index.html

Thanks

Thanks to my employer dfv media group for the permission to publish this package as open source.

Copy Link

Version

Install

install.packages('piwikproR')

Monthly Downloads

225

Version

0.4.0

License

GPL (>= 3)

Maintainer

Martin Stingl

Last Published

September 1st, 2022

Functions in piwikproR (0.4.0)

build_filter

Build filter from tribble
get_users_list

get_users_list fetch all users
MAX_LINES_PER_REQUEST_ANALYTICS_API

Maximum number of lines requested in Analytics-API
MAX_LINES_PER_REQUEST_USERS_API

Maximum number of lines requested in Users-API
release_questions

Ask package specific question during release-process
get_login_token

Fetch login token
send_query

Send the query and receive the result
send_query_single

Send the query and receive the result
get_metasites_list

get_metasites_list
get_permissions_for_app

get_permissions_for_app
get_test_credentials

get_test_credentials getting credentials for testing
get_permissions_for_metasite

get_permissions_for_metasite
get_usergroup_members

get_usergroup_members
get_usergroups_list

get_usergroups_list fetch all usergroups
get_column_type

Convert column-type according to column_name
get_generic_list

get_generic_list fetches all rows of a given api
build_query

Build the query
apply_types

Convert column-type according to column_name
get_apps_list

get_apps_list fetch all apps
get_app_detail

get_app_detail fetching details about an app
is_column_a_metric

is_column_a_metric Checks if column_name indicates numeric values Uses https://developers.piwik.pro/en/latest/custom_reports/columns.html