import { axios } from "@pipedream/platform";
import gsConsole from "../../google_search_console.app.mjs";
import {
removeCustomPropFields, trimIfString,
} from "../../common/utils.mjs";
const propsMeta = {
siteUrl: {
type: "string",
extendedType: "url",
label: "Verified Site URL",
description: "Including https:// is strongly advised",
},
startDate: {
type: "string",
extendedType: "YYYY-MM-DD",
label: "Start Date (YYYY-MM-DD)",
postBody: true,
},
endDate: {
type: "string",
extendedType: "YYYY-MM-DD",
label: "End Date (YYYY-MM-DD)",
postBody: true,
},
dimensions: {
type: "string[]",
label: "Dimensions",
optional: true,
description: "e.g. ['query', 'page', 'country', 'device']",
postBody: true,
},
searchType: {
type: "string",
label: "Search Type",
optional: true,
options: [
"web",
"image",
"video",
"news",
"googleNews",
"discover",
],
default: "web",
postBody: true,
},
aggregationType: {
type: "string",
label: "Aggregation Type",
optional: true,
options: [
"auto",
"byPage",
],
postBody: true,
},
rowLimit: {
type: "integer",
label: "Max rows to return",
default: 10,
postBody: true,
},
startRow: {
type: "integer",
label: "Start row (for pagination)",
optional: true,
postBody: true,
},
dimensionFilterGroups: {
type: "object",
label: "Dimension Filters",
optional: true,
description: "Follow Search Console API structure for filters",
postBody: true,
},
dataState: {
type: "string",
label: "Data State",
optional: true,
options: [
"all",
"final",
],
default: "final",
postBody: true,
},
};
export default {
name: "Retrieve Site Performance Data",
description: "Fetches search analytics from Google Search Console for a verified site.",
key: "google_search_console-retrieve-site-performance-data",
version: "0.0.1",
type: "action",
props: {
gsConsole,
...removeCustomPropFields(propsMeta),
},
async run({ $ }) {
this.dimensionFilterGroups = this.gsConsole.parseIfJsonString(this.dimensionFilterGroups);
const body = {};
const warnings = [];
for (let propName in propsMeta) {
const meta = propsMeta[propName];
this[propName] = trimIfString(this[propName]);
if (meta.optional === true && ((this[propName] ?? "") === "")) continue;
const validationResult = this.gsConsole.validateUserInput(meta, this[propName]);
if (validationResult.warnings) warnings.push(...validationResult.warnings);
if (meta.postBody === true) body[propName] = this[propName];
};
const url = this.siteUrl;
let response;
try {
response = await axios($, {
method: "POST",
url: `https://searchconsole.googleapis.com/webmasters/v3/sites/${encodeURIComponent(url)}/searchAnalytics/query`,
headers: {
"Authorization": `Bearer ${this.gsConsole.$auth.oauth_access_token}`,
"Content-Type": "application/json",
},
data: body,
});
} catch (error) {
const thrower = this.gsConsole.checkWhoThrewError(error);
throw new Error(`Failed to fetch data ( ${thrower.whoThrew} error ) : ${error.message}. ` + warnings.join("\n- "));
};
$.export("$summary", ` Fetched ${response.rows?.length || 0} rows of data. ` + warnings.join("\n- "));
return response;
},
};