Search code examples
node.jsgoogle-apigoogle-search-consolegoogle-api-nodejs-client

Trying to Query the Google Search Console API for a specific URL


I'm trying to get results for a specific URL but only get the base URL result, not sure why my query doesn't query the specific URL?

i.e. result of query is for my-domain.co.uk, not my-domain.co.uk/pizza/2 as expected.

    const url = "https://www.my-domain.co.uk/pizza/2"
    
    searchconsole.searchanalytics.query(
            {
              siteUrl: "https://www.my-domain.co.uk",
              requestBody: {
                startDate: "2023-01-01",
                endDate: "2023-01-01",
                dimensions: ["page"],
                filters: [
                  {
                    dimension: "page",
                    operator: "equals",
                    expression: url,
                  },
                ],
                startRow: 0,
                rowLimit: 1,
              },
            },
            async (err, response) => {
              console.log(response);
              if (err) return res.status(500).send(`Error: ${err}`);
              res.send(response.data);
            }
          );
        });

Solution

  • If only Googles docs were easier to navigate. The issue appears to be that I should have been using the 'dimensionFilterGroups' key.

     searchconsole.searchanalytics.query(
        {
          siteUrl: process.env.SITE_URL,
          requestBody: {
            startDate: startDate,
            endDate: endDate,
            dimensions: ["page"],
            dimensionFilterGroups: [
              {
                groupType: "and",
                filters: [
                  {
                    dimension: "page",
                    operator: "equals",
                    expression: url,
                  },
                ],
              },
            ],
            aggregationType: "byPage",
            
            startRow: 0,
            rowLimit: 1,
          },
        },
        async (err, response) => {
          if (err) return res.status(500).send(`Error: ${err}`);
          res.send(response.data);
        }
      );