How can I fetch report data using API?

Analytics

When you need to get raw data from a report via API, you can use an API call definition and ask directly the database for information.

To fetch data directly from a report, follow these steps:

  1. Go to Menu > Analytics.
  2. Navigate to ReportsCustom reportsGoals, or Ecommerce .
  3. On the left, click a report that you want to work with.
  4. Click the three-dot icon next to the report section that you want to use.
    View API call definition in Piwik PRO
  5. Click View API call definition.
    View API call definition in Piwik PRO
  6. Here’s your API endpoint and API query.
    View API call definition in Piwik PRO
  7. Prepare your Client ID and Client secret (Where to find it?)
  8. Send the first call to get your access token. Our API uses a bearer token to authenticate each call.
    curl -X POST 'https://<account-name>.piwik.pro/auth/token' -H "Content-Type: application/json" --data '{
        "grant_type": "client_credentials",
        "client_id": "<client-id>",
        "client_secret": "<client-secret>"
    }'

    Change in this code:

    Response example:

    {
        "token_type": "Bearer",
        "expires_in": 1800,
        "access_token": "<bearer-token>"
    }
  9. Copy your bearer token. And then make a second call that will fetch the data.
    curl -X POST 'https://<account-name>.piwik.pro/api/analytics/v1/query/' -H "Authorization: Bearer <bearer-token>" \
    -H "Content-Type: application/vnd.api+json" --data '{
      {
    	"date_from": "2022-07-01",
    	"date_to": "2022-09-30",
    	"website_id": "<site-or-app-id>",
    	"offset": 0,
    	"limit": 10,
    	"columns": [
    		{
    			"column_id": "referrer_type"
    		},
    		{
    			"column_id": "source_medium"
    		},
    		{
    			"column_id": "visitors"
    		},
    		{
    			"column_id": "sessions"
    		},
    		{
    			"column_id": "bounce_rate"
    		},
    		{
    			"column_id": "goal_conversions"
    		},
    		{
    			"column_id": "goal_conversion_rate"
    		},
    		{
    			"transformation_id": "sum",
    			"column_id": "goal_revenue"
    		}
    	],
    	"order_by": [
    		[
    			2,
    			"desc"
    		]
    	],
    	"filters": null,
    	"metric_filters": null
    }'

    In this code, change the following:

    • <account-name>: Use your account name.
    • <bearer-token>: Use the bearer token from the previous call. Each token is valid for 30 minutes only.
    • API query: Change the content of your API query to the one from step 6 (API call definition). You can also modify it.

    Response example:

    {
        "data": [
            [
                [
                    3,
                    "Website"
                ],
                "help.piwik.pro / referral",
                19,
                20,
                0.65,
                0,
                0.0,
                null
            ],
            [
                [
                    1,
                    "Direct entry"
                ],
                "direct / direct",
                8,
                11,
                0.8181818181818182,
                0,
                0.0,
                null
            ]
        ],
        "meta": {
            "columns": [
                "referrer_type",
                "source_medium",
                "visitors",
                "sessions",
                "bounce_rate",
                "goal_conversions",
                "goal_conversion_rate",
                "goal_revenue__sum"
            ],
            "count": 2
        }
    }

Tip: For more about our API, read our developer documentation.

Was this article helpful?

Technical support

If you still have some questions, visit our community.
There’s always someone ready to help!

Back to help center