|
| 1 | +/** |
| 2 | + * Fetches performance data (clicks, impressions, etc.) for a verified site |
| 3 | + * via the Google Search Console Search Analytics API. |
| 4 | + * |
| 5 | + * Full usage docs in README.md |
| 6 | + */ |
| 7 | + |
| 8 | +import { axios } from "@pipedream/platform"; |
| 9 | +import gsConsole from "../../google_search_console.app.mjs"; |
| 10 | +import { |
| 11 | + removeCustomPropFields, trimIfString, |
| 12 | +} from "../../common/utils.mjs"; |
| 13 | + |
| 14 | +/* |
| 15 | + Define prop metadata separately, including custom fields used for extended validation |
| 16 | + and runtime behavior. |
| 17 | +
|
| 18 | + These extended fields (like `extendedType`, `postBody`, etc.) are not part of the standard |
| 19 | + Pipedream prop schema. |
| 20 | +
|
| 21 | + A helper function (`removeCustomPropFields`) will later strip these non-standard fields, |
| 22 | + returning only valid Pipedream props for use in the UI. |
| 23 | +
|
| 24 | + Keeping the full metadata in closure allows access to helpful context (e.g. validation rules) |
| 25 | + during runtime. |
| 26 | +*/ |
| 27 | +const propsMeta = { |
| 28 | + |
| 29 | + siteUrl: { |
| 30 | + type: "string", |
| 31 | + extendedType: "url", |
| 32 | + label: "Verified Site URL", |
| 33 | + description: "Including https:// is strongly advised", |
| 34 | + }, |
| 35 | + startDate: { |
| 36 | + type: "string", |
| 37 | + extendedType: "YYYY-MM-DD", |
| 38 | + label: "Start Date (YYYY-MM-DD)", |
| 39 | + postBody: true, |
| 40 | + }, |
| 41 | + endDate: { |
| 42 | + type: "string", |
| 43 | + extendedType: "YYYY-MM-DD", |
| 44 | + label: "End Date (YYYY-MM-DD)", |
| 45 | + postBody: true, |
| 46 | + }, |
| 47 | + dimensions: { |
| 48 | + type: "string[]", |
| 49 | + label: "Dimensions", |
| 50 | + optional: true, |
| 51 | + description: "e.g. ['query', 'page', 'country', 'device']", |
| 52 | + postBody: true, |
| 53 | + }, |
| 54 | + searchType: { |
| 55 | + type: "string", |
| 56 | + label: "Search Type", |
| 57 | + optional: true, |
| 58 | + options: [ |
| 59 | + "web", |
| 60 | + "image", |
| 61 | + "video", |
| 62 | + "news", |
| 63 | + "googleNews", |
| 64 | + "discover", |
| 65 | + ], |
| 66 | + default: "web", |
| 67 | + postBody: true, |
| 68 | + }, |
| 69 | + aggregationType: { |
| 70 | + type: "string", |
| 71 | + label: "Aggregation Type", |
| 72 | + optional: true, |
| 73 | + options: [ |
| 74 | + "auto", |
| 75 | + "byPage", |
| 76 | + ], |
| 77 | + postBody: true, |
| 78 | + }, |
| 79 | + rowLimit: { |
| 80 | + type: "integer", |
| 81 | + label: "Max rows to return", |
| 82 | + default: 10, |
| 83 | + postBody: true, |
| 84 | + }, |
| 85 | + startRow: { |
| 86 | + type: "integer", |
| 87 | + label: "Start row (for pagination)", |
| 88 | + optional: true, |
| 89 | + postBody: true, |
| 90 | + }, |
| 91 | + dimensionFilterGroups: { |
| 92 | + type: "object", |
| 93 | + label: "Dimension Filters", |
| 94 | + optional: true, |
| 95 | + description: "Follow Search Console API structure for filters", |
| 96 | + postBody: true, |
| 97 | + }, |
| 98 | + dataState: { |
| 99 | + type: "string", |
| 100 | + label: "Data State", |
| 101 | + optional: true, |
| 102 | + options: [ |
| 103 | + "all", |
| 104 | + "final", |
| 105 | + ], |
| 106 | + default: "final", |
| 107 | + postBody: true, |
| 108 | + }, |
| 109 | +}; |
| 110 | + |
| 111 | +export default { |
| 112 | + name: "Retrieve Site Performance Data", |
| 113 | + description: "Fetches search analytics from Google Search Console for a verified site.", |
| 114 | + key: "google_search_console-retrieve-site-performance-data", |
| 115 | + version: "0.0.1", |
| 116 | + type: "action", |
| 117 | + props: { |
| 118 | + gsConsole, |
| 119 | + // Remove non-standard fields and expose only valid props to Pipedream UI |
| 120 | + ...removeCustomPropFields(propsMeta), |
| 121 | + }, |
| 122 | + |
| 123 | + //=================== RUN ============================== |
| 124 | + //====================================================== |
| 125 | + |
| 126 | + async run({ $ }) { |
| 127 | + |
| 128 | + /* |
| 129 | + `dimensionFilterGroups` is expected to be an object. |
| 130 | + If a JSON string is passed instead (e.g. from UI input), attempt to parse it. |
| 131 | + - Returns parsed object if successful |
| 132 | + - Returns original input if not a string |
| 133 | + - Throws a descriptive error if JSON parsing fails |
| 134 | + */ |
| 135 | + this.dimensionFilterGroups = this.gsConsole.parseIfJsonString(this.dimensionFilterGroups); |
| 136 | + |
| 137 | + // Prepare the POST request payload |
| 138 | + const body = {}; |
| 139 | + |
| 140 | + // Accumulator for non-blocking input warnings |
| 141 | + const warnings = []; |
| 142 | + |
| 143 | + /* |
| 144 | + This loop: |
| 145 | + - Trims and validates all defined props |
| 146 | + - Skips empty optional fields |
| 147 | + - Accumulates non-blocking warnings |
| 148 | + - Adds valid props to the POST request payload (`body`) if marked with `postBody: true` |
| 149 | + */ |
| 150 | + for (let propName in propsMeta) { |
| 151 | + |
| 152 | + // Just for convenience. |
| 153 | + const meta = propsMeta[propName]; |
| 154 | + |
| 155 | + // Trim the value if it's a string |
| 156 | + this[propName] = trimIfString(this[propName]); |
| 157 | + |
| 158 | + // Skip if the prop is optional and empty (null, undefined, or blank string) |
| 159 | + if (meta.optional === true && ((this[propName] ?? "") === "")) continue; |
| 160 | + |
| 161 | + // Validate input (may throw or return warning messages) |
| 162 | + const validationResult = this.gsConsole.validateUserInput(meta, this[propName]); |
| 163 | + |
| 164 | + // Push the warnings into warnings accumulator if any. |
| 165 | + if (validationResult.warnings) warnings.push(...validationResult.warnings); |
| 166 | + |
| 167 | + // Include prop in the request body if marked as postBody |
| 168 | + if (meta.postBody === true) body[propName] = this[propName]; |
| 169 | + }; |
| 170 | + |
| 171 | + // Already trimmed earlier |
| 172 | + const url = this.siteUrl; |
| 173 | + |
| 174 | + // Response of the POST request. |
| 175 | + let response; |
| 176 | + |
| 177 | + try { |
| 178 | + response = await axios($, { |
| 179 | + method: "POST", |
| 180 | + url: `https://searchconsole.googleapis.com/webmasters/v3/sites/${encodeURIComponent(url)}/searchAnalytics/query`, |
| 181 | + headers: { |
| 182 | + "Authorization": `Bearer ${this.gsConsole.$auth.oauth_access_token}`, |
| 183 | + "Content-Type": "application/json", |
| 184 | + }, |
| 185 | + data: body, |
| 186 | + }); |
| 187 | + |
| 188 | + } catch (error) { |
| 189 | + // Identify if the error was thrown by internal validation or by the API call |
| 190 | + const thrower = this.gsConsole.checkWhoThrewError(error); |
| 191 | + |
| 192 | + throw new Error(`Failed to fetch data ( ${thrower.whoThrew} error ) : ${error.message}. ` + warnings.join("\n- ")); |
| 193 | + }; |
| 194 | + |
| 195 | + // Output summary and any warnings for the user |
| 196 | + $.export("$summary", ` Fetched ${response.rows?.length || 0} rows of data. ` + warnings.join("\n- ")); |
| 197 | + return response; |
| 198 | + }, |
| 199 | +}; |
| 200 | + |
0 commit comments