1-
21/**
32 * Fetches performance data (clicks, impressions, etc.) for a verified site
43 * via the Google Search Console Search Analytics API.
5- *
4+ *
65 * Full usage docs in README.md
76 */
87
9-
108import { axios } from "@pipedream/platform" ;
119import gsConsole from "../../google_search_console.app.mjs" ;
12- import { removeCustomPropFields , trimIfString } from "../../common/utils.mjs"
10+ import {
11+ removeCustomPropFields , trimIfString ,
12+ } from "../../common/utils.mjs" ;
1313
14- /*
15- Define prop metadata separately, including custom fields used for extended validation
14+ /*
15+ Define prop metadata separately, including custom fields used for extended validation
1616 and runtime behavior.
1717
18- These extended fields (like `extendedType`, `postBody`, etc.) are not part of the standard
18+ These extended fields (like `extendedType`, `postBody`, etc.) are not part of the standard
1919 Pipedream prop schema.
2020
2121 A helper function (`removeCustomPropFields`) will later strip these non-standard fields,
2222 returning only valid Pipedream props for use in the UI.
2323
24- Keeping the full metadata in closure allows access to helpful context (e.g. validation rules)
24+ Keeping the full metadata in closure allows access to helpful context (e.g. validation rules)
2525 during runtime.
2626*/
2727const propsMeta = {
@@ -55,15 +55,25 @@ const propsMeta = {
5555 type : "string" ,
5656 label : "Search Type" ,
5757 optional : true ,
58- options : [ "web" , "image" , "video" , "news" , "googleNews" , "discover" ] ,
58+ options : [
59+ "web" ,
60+ "image" ,
61+ "video" ,
62+ "news" ,
63+ "googleNews" ,
64+ "discover" ,
65+ ] ,
5966 default : "web" ,
6067 postBody : true ,
6168 } ,
6269 aggregationType : {
6370 type : "string" ,
6471 label : "Aggregation Type" ,
6572 optional : true ,
66- options : [ "auto" , "byPage" ] ,
73+ options : [
74+ "auto" ,
75+ "byPage" ,
76+ ] ,
6777 postBody : true ,
6878 } ,
6979 rowLimit : {
@@ -89,34 +99,33 @@ const propsMeta = {
8999 type : "string" ,
90100 label : "Data State" ,
91101 optional : true ,
92- options : [ "all" , "final" ] ,
102+ options : [
103+ "all" ,
104+ "final" ,
105+ ] ,
93106 default : "final" ,
94107 postBody : true ,
95108 } ,
96-
97-
98-
99109} ;
100110
101111export default {
102112 name : "Retrieve Site Performance Data" ,
103113 description : "Fetches search analytics from Google Search Console for a verified site." ,
104- key : "retrieve-site-performance-data" ,
105- version : "0.0.2 " ,
114+ key : "google_search_console- retrieve-site-performance-data" ,
115+ version : "0.0.1 " ,
106116 type : "action" ,
107117 props : {
108118 gsConsole,
109119 // Remove non-standard fields and expose only valid props to Pipedream UI
110120 ...removeCustomPropFields ( propsMeta ) ,
111121 } ,
112122
113-
114123 //=================== RUN ==============================
115124 //======================================================
116125
117126 async run ( { $ } ) {
118127
119- /*
128+ /*
120129 `dimensionFilterGroups` is expected to be an object.
121130 If a JSON string is passed instead (e.g. from UI input), attempt to parse it.
122131 - Returns parsed object if successful
@@ -131,24 +140,24 @@ export default {
131140 // Accumulator for non-blocking input warnings
132141 const warnings = [ ] ;
133142
134- /*
143+ /*
135144 This loop:
136145 - Trims and validates all defined props
137146 - Skips empty optional fields
138147 - Accumulates non-blocking warnings
139148 - Adds valid props to the POST request payload (`body`) if marked with `postBody: true`
140149 */
141- for ( let propName in propsMeta ) {
150+ for ( let propName in propsMeta ) {
142151
143152 // Just for convenience.
144153 const meta = propsMeta [ propName ] ;
145154
146- // Trim the value if it's a string
147- this [ propName ] = trimIfString ( this [ propName ] ) ;
155+ // Trim the value if it's a string
156+ this [ propName ] = trimIfString ( this [ propName ] ) ;
148157
149158 // Skip if the prop is optional and empty (null, undefined, or blank string)
150- if ( meta . optional === true && ( ( this [ propName ] ?? '' ) === '' ) ) continue ;
151-
159+ if ( meta . optional === true && ( ( this [ propName ] ?? "" ) === "" ) ) continue ;
160+
152161 // Validate input (may throw or return warning messages)
153162 const validationResult = this . gsConsole . validateUserInput ( meta , this [ propName ] ) ;
154163
@@ -166,24 +175,23 @@ export default {
166175 let response ;
167176
168177 try {
169- response = await axios ( $ , {
170- method : "POST" ,
171- url : `https://searchconsole.googleapis.com/webmasters/v3/sites/${ encodeURIComponent ( url ) } /searchAnalytics/query` ,
172- headers : {
173- Authorization : `Bearer ${ this . gsConsole . $auth . oauth_access_token } ` ,
174- "Content-Type" : "application/json" ,
175- } ,
176- data : body ,
177- } )
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+ } ) ;
178187
179188 } catch ( error ) {
180- // Identify if the error was thrown by internal validation or by the API call
189+ // Identify if the error was thrown by internal validation or by the API call
181190 const thrower = this . gsConsole . checkWhoThrewError ( error ) ;
182-
191+
183192 throw new Error ( `Failed to fetch data ( ${ thrower . whoThrew } error ) : ${ error . message } . ` + warnings . join ( "\n- " ) ) ;
184-
185193 } ;
186-
194+
187195 // Output summary and any warnings for the user
188196 $ . export ( "$summary" , ` Fetched ${ response . rows ?. length || 0 } rows of data. ` + warnings . join ( "\n- " ) ) ;
189197 return response ;
0 commit comments