-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgsc-page-directory-worldwide-search-analytics.js
124 lines (105 loc) · 4.11 KB
/
gsc-page-directory-worldwide-search-analytics.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
(page,done)=>
{
var that = this;
let lable = "GSC";
let msg = "";
let type = "info";
let what = null;
let prio = null;
//GSC API
const { googleApiAccessToken } = this.getGlobals();
page.hasGscAccess(googleApiAccessToken, ()=>{
const gscApiRequest = (token, api, requestbody, success, fail) =>
{
window.fetch(api,
{
method: "POST",
mode: 'cors',
headers: new Headers(
{
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer '+token
}),
body: JSON.stringify(requestbody)
}).then((response)=>
{
success(response);
}).catch((err)=>{
fail(err);
});
}
let sd = that.dateMinus(29); //last 28 days starting yesterday
let ed = that.dateMinus(1); //yesterday
let url = page.getURL("first");
let uo = new URL(url);
let origin = uo.origin+'/';
let url_s = url.match('.*\/')[0];
if(url===url_s || url_s === origin)
{
done();
return null;
}
let tpl = 'https://search.google.com/search-console/performance/search-analytics?resource_id='+encodeURIComponent(origin)+'&breakdown=page&num_of_days=28&page=*'+encodeURIComponent(url_s);
let tpl_msg = '<a href="'+tpl+'" target="_blank">Pages</a>';
let tql = 'https://search.google.com/search-console/performance/search-analytics?resource_id='+encodeURIComponent(origin)+'&breakdown=query&num_of_days=28&page=*'+encodeURIComponent(url_s);
let tql_msg = '<a href="'+tql+'" target="_blank">Queries</a>';
let req = {
"startDate": sd,
"endDate": ed,
"dimensions": [
],
"dimensionFilterGroups": [
{
"filters": [
{
"dimension": "page",
"expression": url_s,
"operator": "contains"
}
]
}
],
"aggregationType": "auto",
"rowLimit":100
};
let api = 'https://www.googleapis.com/webmasters/v3/sites/'+encodeURIComponent(origin)+'/searchAnalytics/query?fields=responseAggregationType%2Crows&;';
gscApiRequest(googleApiAccessToken, api, req, (response) =>
{
if(response.status == 200)
{
let ca = [];
let nca = [];
let ra = [];
response.json().then(function(data)
{
console.log(data);
console.log(data.rows);
//debugger;
if(!data.rows)
{
type = "warning";
msg = "No Search Analytics global data for "+url_s+".* "+tql_msg
done(that.createResult(lable, msg, type, what, prio));
return null;
};
let clicks = data.rows[0].clicks;
let impressions = data.rows[0].impressions;
let ctr = data.rows[0].ctr;
let pctr = Math.round(ctr* 100 * 100) / 100
prio = 1470;
msg = "Search Analytics metrics (all markets) for "+url_s+".*: "+clicks+" clicks, "+impressions+" impressions, "+pctr+"% CTR "+that.partialCodeLink(JSON.stringify(data))+" "+tql_msg+" "+tpl_msg;
done(that.createResult(lable, msg, type, what, prio));
//
return null;
});
}
}, (err) =>
{
console.log(err)
done();
return null;
});
},
()=>{done(); return null;})
}