forked from fusebit/google-searchconsole-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (58 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// - Enable the API at:
// https://console.developers.google.com/apis/api/searchconsole.googleapis.com
// - Create a Service Account in the Google Developers Console at:
// https://console.cloud.google.com/iam-admin/serviceaccounts
// Save the keys in the local directory
// - Add the email address of this newly created Service Account as an owner in the Search Console
// https://search.google.com/search-console/users
const { google } = require("googleapis");
const { JWT } = require("google-auth-library");
const searchconsole = google.searchconsole("v1");
async function main() {
// Authentication
const keys = require("PATH_TO_KEYS.JSON_FILE");
const client = new JWT({
email: keys.client_email,
key: keys.private_key,
scopes: [
"https://www.googleapis.com/auth/webmasters",
"https://www.googleapis.com/auth/webmasters.readonly",
],
});
google.options({ auth: client });
// Check which sites belong to this authorized account
const resSiteList = await searchconsole.sites.list({});
console.log(resSiteList.data);
// Submit a URL for Inspection
const res = await searchconsole.sitemaps.submit({
feedpath: 'https://fusebit.io/sitemap.xml',
siteUrl: 'https://fusebit.io/',
});
console.log(res.data);
// Inspect if a URL has been indexed
const resInspectURL = await searchconsole.urlInspection.index.inspect({
requestBody: {
inspectionUrl: "https://fusebit.io/blog/google-search-console-nodejs/",
languageCode: "en-US",
siteUrl: "https://fusebit.io/",
},
});
console.log(resInspectURL.data);
// Search Analytics on your Website
const resSearchAnalytics = await searchconsole.searchanalytics.query({
siteUrl: 'https://fusebit.io/',
// Detailed Query Parameters: https://developers.google.com/webmaster-tools/v1/searchanalytics/query
requestBody: {
"endDate": "2022-03-08",
"startDate": "2022-03-01",
"dimensionFilterGroupsfilters": ["query contains node"],
"dimensions": ["query"],
"rowLimit": 10
},
});
console.log(resSearchAnalytics.data.rows);
}
main().catch((e) => {
console.error(e);
throw e;
});