Graphql query to get paths and number of visits

I have a page I just put up that is PHP based that gets stats using php and graphql

https://vonwallace.com/stats/cloudflare-stats.php

I would like to add to it a query that pulls the paths and number of visits for the last 30 days.

Here is the query I am running now

$query = ‘query {
viewer {
zones(filter: { zoneTag: "’ . $zoneId . ‘" }) {
httpRequests1dGroups(
filter: {
date_gt: "’ . $startDate . ‘"
}
orderBy: [date_ASC]
limit: 10000
) {
dimensions { date }
uniq {
uniques
}
sum {
requests,
pageViews
}
}
}
}
}’;

Trying to figuer out what query I would use for paths and number of visits.

As much as I distrust ChatGPT responses, here’s its recommendation:

$query = 'query {
  viewer {
    zones(filter: { zoneTag: "' . $zoneId . '" }) {
      httpRequestsAdaptiveGroups(
        filter: {
          date_gt: "' . $startDate . '"
        }
        orderBy: [date_ASC]
        limit: 10000
      ) {
        dimensions { date, path }
        sum {
          pageViews
        }
      }
    }
  }
}';

You can take it with a grain of salt…or maybe it’ll actually work.

1 Like

I looked at the nework traffic going back and forth via the browser developer tools.

Then asked chat gpt to pair it down

This is what it came up with
$query = ‘query GetZoneTopPaths {
viewer {
zones(filter: {zoneTag: "’ . $zoneId . ‘" }) {
topPaths: httpRequestsAdaptiveGroups(
filter: {
date_gt: "’ . $startDate . ‘"
}
limit: 10000
orderBy: [date_ASC]
) {
dimensions {
path: clientRequestPath
__typename
date
}
sum {
pageViews: visits
__typename
}
__typename
}
__typename
}
__typename
}
}’;

Not working though

Here is the code
$timeInterval = 30;

$startDate = date(‘Y-m-d’, strtotime(“-$timeInterval days”));

    // GraphQL query
$query = 'query GetZoneTopPaths {
  viewer {
    zones(filter: {zoneTag: "' . $zoneId . '" }) {
      topPaths: httpRequestsAdaptiveGroups(
      filter:  {
      date_gt: "' . $startDate . '"
      }
       limit: 10000
      orderBy: [date_ASC]
      ) {
        dimensions {
          path: clientRequestPath
          __typename
          date
        }
        sum {
          pageViews: visits
          __typename
        }
        __typename
      }
      __typename
    }
    __typename
  }
}';





// Use the provided HTTP client to make the request
$response = $client->post('https://api.cloudflare.com/client/v4/graphql', [
    'json' => ['query' => $query],
]);

// Assuming $response is an instance of Psr\Http\Message\ResponseInterface
if ($response->getStatusCode() === 200) {
    // Parse the JSON response
    $responseArray = json_decode($response->getBody()->getContents(), true);

var_dump($responseArray);
// Extract the relevant data
$topPaths = $responseArray[‘data’][‘viewer’][‘zones’][0][‘topPaths’];

    // Check if there are any results
    if (empty($topPaths)) {
        echo 'No data available.';
    } else {
        // Start building the HTML table
        echo '<div class="table_style2"><table border="1">';
        echo '<tr><th>Path</th><th>Page Views</th></tr>';

        foreach ($topPaths as $pathData) {
            echo '<tr>';
            echo '<td>' . $pathData['dimensions']['path'] . '</td>';
            echo '<td>' . $pathData['sum']['pageViews'] . '</td>';
            echo '</tr>';
        }

        // Close the table
        echo '</table></div>';
    }
} else {
    echo "API request failed with status code: " . $response->getStatusCode();
}

I figured it out.

check out the page now.

I will add it to my github

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.