Reporting API Tutorial: Get your top 10 keywords

This tutorial will show you how easy it is to request the yesterday's top 10 keywords in XML format.

Build the URL

To build the URL of the API call, you need:

  • your Piwik base URL (replace demo.piwik.org with the URL and path of your Piwik server

    http://demo.piwik.org/?module=API

  • the name of the method you want to call. It has the format moduleName.methodToCall (see the list on API Methods). You need to request the last keywords from the plugin Referrers: the method parameter is:

    method=Referrers.getKeywords

  • the website id.

    idSite=1

  • the date parameter. This can be today, yesterday, or any date with the format YYYY-MM-DD

    date=yesterday

  • the period parameter. This can be day, week, month or year

    period=day

    Alternatively, if you wanted to request all of the keywords from a given date, you could use a date range parameter. For example, to request all of the keywords since January 1st 2011:**period=range&date=2011-01-01,yesterday**

  • the format parameter. Defines the output format of the data: XML, JSON, CSV, PHP (serialized PHP), HTML (simple html)

    format=xml

  • (optional) the filter_limit parameter that defines the number of rows returned

    filter_limit=10

The final url is http://demo.piwik.org/?module=API&method=Referrers.getKeywords&idSite=3&date=yesterday&period=day&format=xml&filter_limit=10

XML Output

Here is the output of this request:

<?xml version="1.0" encoding="utf-8" ?>
<result>
	<row>
		<label>Keyword not defined</label>
		<nb_uniq_visitors>12</nb_uniq_visitors>
		<nb_visits>14</nb_visits>
		<nb_actions>45</nb_actions>
		<nb_users>0</nb_users>
		<max_actions>13</max_actions>
		<sum_visit_length>3300</sum_visit_length>
		<bounce_count>7</bounce_count>
		<nb_visits_converted>0</nb_visits_converted>
		<idsubdatatable>6</idsubdatatable>
	</row>
	<row>
		<label>book about exorcism in the philippines</label>
		<nb_uniq_visitors>1</nb_uniq_visitors>
		<nb_visits>1</nb_visits>
		<nb_actions>3</nb_actions>
		<nb_users>0</nb_users>
		<max_actions>3</max_actions>
		<sum_visit_length>114</sum_visit_length>
		<bounce_count>0</bounce_count>
		<nb_visits_converted>0</nb_visits_converted>
		<idsubdatatable>7</idsubdatatable>
	</row>
	<row>
		<label>http://virtualdrums.com/</label>
		<nb_uniq_visitors>1</nb_uniq_visitors>
		<nb_visits>1</nb_visits>
		<nb_actions>2</nb_actions>
		<nb_users>0</nb_users>
		<max_actions>2</max_actions>
		<sum_visit_length>20</sum_visit_length>
		<bounce_count>0</bounce_count>
		<nb_visits_converted>0</nb_visits_converted>
		<idsubdatatable>2</idsubdatatable>
	</row>
	<row>
		<label>types of demonic afflictions</label>
		<nb_uniq_visitors>1</nb_uniq_visitors>
		<nb_visits>1</nb_visits>
		<nb_actions>1</nb_actions>
		<nb_users>0</nb_users>
		<max_actions>1</max_actions>
		<sum_visit_length>0</sum_visit_length>
		<bounce_count>1</bounce_count>
		<nb_visits_converted>0</nb_visits_converted>
		<idsubdatatable>8</idsubdatatable>
	</row>
	<row>
		<label>virtuaal drum</label>
		<nb_uniq_visitors>1</nb_uniq_visitors>
		<nb_visits>1</nb_visits>
		<nb_actions>1</nb_actions>
		<nb_users>0</nb_users>
		<max_actions>1</max_actions>
		<sum_visit_length>0</sum_visit_length>
		<bounce_count>1</bounce_count>
		<nb_visits_converted>0</nb_visits_converted>
		<idsubdatatable>3</idsubdatatable>
	</row>
	<row>
		<label>virtual snare drum games</label>
		<nb_uniq_visitors>1</nb_uniq_visitors>
		<nb_visits>1</nb_visits>
		<nb_actions>1</nb_actions>
		<nb_users>0</nb_users>
		<max_actions>1</max_actions>
		<sum_visit_length>0</sum_visit_length>
		<bounce_count>1</bounce_count>
		<nb_visits_converted>0</nb_visits_converted>
		<idsubdatatable>4</idsubdatatable>
	</row>
	<row>
		<label>vvirtual drumms</label>
		<nb_uniq_visitors>1</nb_uniq_visitors>
		<nb_visits>1</nb_visits>
		<nb_actions>1</nb_actions>
		<nb_users>0</nb_users>
		<max_actions>1</max_actions>
		<sum_visit_length>0</sum_visit_length>
		<bounce_count>1</bounce_count>
		<nb_visits_converted>0</nb_visits_converted>
		<idsubdatatable>5</idsubdatatable>
	</row>
</result>

Other useful examples

There are also functions for Websites, Users, Goals, PDF Reports (create, update, delete operations) and a lot more, such as: adding Annotations, creating custom Segments,

Check out the Piwik API Reference