How can I fetch report data using the API?

Analytics

When you want to get raw data from a report through an API, you can use the API call definition and request information directly from the database.

To fetch report data, follow these steps:

  1. Go to Menu > Analytics.
  2. Navigate to ReportsCustom reportsGoals or Ecommerce .
  3. On the left, select the report you want to work with.
  4. Click the  three-dot icon next to the report 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>"
    }'

    In the code above, make the following changes:

    • <account-name>: Use your account name or use the custom account address <account-name>.example.com
    • <client-id>: Use your client ID. (Where to find it?)
    • <client-secret>: Use your client secret. (Where to find it?)

    Response example:

    {
        "token_type": "Bearer",
        "expires_in": 1800,
        "access_token": "<bearer-token>"
    }
  9. Copy your bearer token. And then make a second call to 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 the code above, make the following changes:

    • <account-name>: Use your account name or use the custom account address <account-name>.example.com
    • <bearer-token>: Use the bearer token from the previous call. Each token is only valid for 30 minutes.
    • 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
        }
    }
  10. All done!

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

Was this article helpful?

Technical support

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

Back to help center