@adobe/spacecat-shared-google-client 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [@adobe/spacecat-shared-google-client-v1.1.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-google-client-v1.0.2...@adobe/spacecat-shared-google-client-v1.1.0) (2024-05-24)
2
+
3
+
4
+ ### Features
5
+
6
+ * filter fetched organic search data from google by base url ([#239](https://github.com/adobe/spacecat-shared/issues/239)) ([0bfba19](https://github.com/adobe/spacecat-shared/commit/0bfba19d08122c969f513154c697dd75017b8e43))
7
+
8
+ # [@adobe/spacecat-shared-google-client-v1.0.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-google-client-v1.0.1...@adobe/spacecat-shared-google-client-v1.0.2) (2024-05-22)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * getOrganicSearchData interface params ([#236](https://github.com/adobe/spacecat-shared/issues/236)) ([acfd48b](https://github.com/adobe/spacecat-shared/commit/acfd48baaaa7594d0b9d7f00f7c9d52118b137e9))
14
+
1
15
  # [@adobe/spacecat-shared-google-client-v1.0.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-google-client-v1.0.0...@adobe/spacecat-shared-google-client-v1.0.1) (2024-05-21)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-google-client",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Shared modules of the Spacecat Services - Google Client",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/index.d.ts CHANGED
@@ -10,18 +10,17 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import { UniversalContext } from '@adobe/helix-universal';
14
13
  import { OAuth2Client } from 'google-auth-library';
15
14
 
16
15
  export default class GoogleClient {
17
16
  /**
18
17
  * Static factory method to create an instance of GoogleClient.
19
18
  *
20
- * @param {UniversalContext} context - An object containing the AWS Lambda context information
19
+ * @param {object} context - An object containing the AWS Lambda context information
21
20
  * @param {string} url - The URL of the site to be audited.
22
21
  * @returns An instance of GoogleClient.
23
22
  */
24
- static createFrom(context: UniversalContext, url: string): GoogleClient;
23
+ static createFrom(context: object, url: string): GoogleClient;
25
24
 
26
25
  /**
27
26
  * Constructor for creating an instance of GoogleClient.
@@ -33,12 +32,13 @@ export default class GoogleClient {
33
32
  /**
34
33
  * Retrieves the Google Search Console data for the specified date range.
35
34
  *
36
- * @param baseURL - The base URL of the site to be audited.
37
- * @param startDate - The start date of the date range.
38
- * @param endDate - The end date of the date range.
39
- * @param dimensions - The dimensions to be included in the report.
35
+ * @param {Date} startDate - The start date of the date range.
36
+ * @param {Date} endDate - The end date of the date range.
37
+ * @param {Array} dimensions - The dimensions to be included in the report.
40
38
  * this parameter is optional and defaults to ['date'],
41
39
  * which means that the report will be grouped by date.
40
+ * @param {Number} rowLimit - The maximum number of rows to return, defaults to 1000.
41
+ * @param {Number} startRow - The row number to start from, defaults to 0.
42
42
  * @returns {Promise<JSON>} The Google Search Console data.
43
43
  * Format: {
44
44
  * "rows": [
@@ -56,10 +56,11 @@ export default class GoogleClient {
56
56
  * }
57
57
  */
58
58
  getOrganicSearchData(
59
- baseURL: string,
60
59
  startDate: Date,
61
60
  endDate: Date,
62
- dimensions: string[]
61
+ dimensions?: string[],
62
+ rowLimit?: number,
63
+ startRow?: number
63
64
  ): Promise<JSON>;
64
65
 
65
66
  /**
package/src/index.js CHANGED
@@ -14,14 +14,19 @@ import { google } from 'googleapis';
14
14
  import { OAuth2Client } from 'google-auth-library';
15
15
  import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
16
16
  import {
17
+ composeAuditURL,
17
18
  isArray,
18
19
  isInteger,
19
- isValidDate,
20
+ isValidDate, isValidUrl,
20
21
  resolveCustomerSecretsName,
21
22
  } from '@adobe/spacecat-shared-utils';
22
23
 
23
24
  export default class GoogleClient {
24
25
  static async createFrom(context, baseURL) {
26
+ if (!isValidUrl(baseURL)) {
27
+ throw new Error('Error creating GoogleClient: Invalid base URL');
28
+ }
29
+
25
30
  try {
26
31
  const customerSecret = resolveCustomerSecretsName(baseURL, context);
27
32
  const client = new SecretsManagerClient({});
@@ -36,6 +41,7 @@ export default class GoogleClient {
36
41
  tokenType: secrets.token_type,
37
42
  expiryDate: secrets.expiry_date,
38
43
  siteUrl: secrets.site_url,
44
+ baseUrl: baseURL,
39
45
  clientId: context.env.GOOGLE_CLIENT_ID,
40
46
  clientSecret: context.env.GOOGLE_CLIENT_SECRET,
41
47
  redirectUri: context.env.GOOGLE_REDIRECT_URI,
@@ -61,6 +67,10 @@ export default class GoogleClient {
61
67
  throw new Error('Missing refresh token in secret');
62
68
  }
63
69
 
70
+ if (!isValidUrl(config.siteUrl)) {
71
+ throw new Error('Invalid site URL in secret');
72
+ }
73
+
64
74
  authClient.setCredentials({
65
75
  access_token: config.accessToken,
66
76
  refresh_token: config.refreshToken,
@@ -69,6 +79,7 @@ export default class GoogleClient {
69
79
  this.authClient = authClient;
70
80
  this.expiryDate = config.expiryDate;
71
81
  this.siteUrl = config.siteUrl;
82
+ this.baseUrl = config.baseUrl;
72
83
  this.log = log;
73
84
  }
74
85
 
@@ -100,6 +111,8 @@ export default class GoogleClient {
100
111
 
101
112
  await this.#refreshTokenIfExpired();
102
113
 
114
+ const auditUrl = await composeAuditURL(this.baseUrl);
115
+
103
116
  const webmasters = google.webmasters({
104
117
  version: 'v3',
105
118
  auth: this.authClient,
@@ -115,6 +128,17 @@ export default class GoogleClient {
115
128
  dimensions,
116
129
  startRow,
117
130
  rowLimit,
131
+ dimensionFilterGroups: [
132
+ {
133
+ filters: [
134
+ {
135
+ dimension: 'page',
136
+ operator: 'contains',
137
+ expression: auditUrl,
138
+ },
139
+ ],
140
+ },
141
+ ],
118
142
  },
119
143
  };
120
144
  this.log.info(`Retrieving organic search data: ${JSON.stringify(params)}`);