[REST API] Retrieve all page URLs with visits through

Hi there!
I am trying to extract data from Matomo through the REST API.
My problem is that, when I look in the Matomo dashboard > Behavior > Pages, I can expand the row wordpress to reveal index.php, which I can further expand to reveal all posts visited.

However, when querying through REST, I only get one single row named wordpress, and I can’t understand how to access the nested layers underneath. My goal here is to end up with an array of page URL <-> visits for all URLs in a given time range. Can somebody help?

Here is a full minimum working example:

add_action( 'admin_menu', 'admin_menus' );
function admin_menus() {
    add_menu_page( 'REST Test', 'Rest Test', 'manage_options', 'rest-test', 'rest_test');
}

function rest_test() {
    $req = new WP_REST_Request( 'GET', '/matomo/v1/api/processed_report');
    $req->set_query_params(array(
        'period' => 'range',
        'date' => '2022-11-01,2022-11-30',
        'filter_limit' => -1,
        'format' => 'json',
        'expanded' => 1, // no difference without
        'apiModule' => 'Actions',
        'apiAction' => 'getPageUrls'));
    $res = rest_do_request($req);
    var_dump($res->get_data());
}

an extract of the result is

"reportData":[{"label":"wordpress","nb_visits":5,"nb_hits":7,"bounce_rate":"0%","avg_time_on_page":"00:01:54","exit_rate":"40%"}],"reportMetadata":[{"idsubdatatable_in_db":1,"segment":"pageUrl=^http%253A%252F%252Flocalhost%252Fwordpress%252Fwordpress","idsubdatatable":2}],

For reference, here is the full result of the request:

string(2634) "{"website":"Profeta","prettyDate":"November 1 \u2013 30, 2022","metadata":{"category":"Actions","subcategory":"Pages","name":"Page URLs","module":"Actions","action":"getPageUrls","dimension":"Page URL","documentation":"This report contains information about the page URLs that have been visited.
The table is organized hierarchically, the URLs are displayed as a folder structure.
Use the plus and minus icons on the left to navigate.","metrics":{"nb_hits":"Pageviews","nb_visits":"Unique Pageviews"},"metricsDocumentation":{"nb_hits":"The number of times this page was visited.","nb_visits":"The number of visits that included this page. If a page was viewed multiple times during one visit, it is only counted once.","avg_time_on_page":"The average amount of time visitors spent on this page (only the page, not the entire website).","bounce_rate":"The percentage of visits that started on this page and left the website straight away.","exit_rate":"The percentage of visits that left the website after viewing this page."},"processedMetrics":{"avg_time_on_page":"Avg. time on page","bounce_rate":"Bounce Rate","exit_rate":"Exit rate","avg_time_generation":"Avg. generation time"},"actionToLoadSubTables":"getPageUrls","relatedReports":[{"name":"Entry pages","module":"Actions","action":"getEntryPageUrls"},{"name":"Exit pages","module":"Actions","action":"getExitPageUrls"}],"imageGraphUrl":"index.php?module=API&method=ImageGraph.get&idSite=1&apiModule=Actions&apiAction=getPageUrls&period=range&date=2022-11-01,2022-11-30","imageGraphEvolutionUrl":"index.php?module=API&method=ImageGraph.get&idSite=1&apiModule=Actions&apiAction=getPageUrls&period=day&date=2022-11-01,2022-11-30","uniqueId":"Actions_getPageUrls"},"columns":{"label":"Page URL","nb_hits":"Pageviews","nb_visits":"Unique Pageviews","avg_time_on_page":"Avg. time on page","bounce_rate":"Bounce Rate","exit_rate":"Exit rate"},"reportData":[{"label":"wordpress","nb_visits":5,"nb_hits":7,"bounce_rate":"0%","avg_time_on_page":"00:01:54","exit_rate":"40%"}],"reportMetadata":[{"idsubdatatable_in_db":1,"segment":"pageUrl=^http%253A%252F%252Flocalhost%252Fwordpress%252Fwordpress","idsubdatatable":2}],"reportTotal":{"nb_visits":5,"nb_hits":7,"sum_time_spent":795,"sum_time_server":0.5740000000000001,"nb_hits_with_time_server":7,"min_time_server":0.29900000000000004,"max_time_server":0.33,"sum_time_dom_processing":1.038,"nb_hits_with_time_dom_processing":7,"min_time_dom_processing":0.387,"max_time_dom_processing":0.96,"entry_nb_visits":2,"entry_nb_actions":7,"entry_sum_visit_length":797,"entry_bounce_count":0,"exit_nb_visits":2},"timerMillis":"19"}" 

Hi @TheCrowned can you try adding the parameter ‘flat’ => 1,

This worked! Many thanks!

1 Like