-
Notifications
You must be signed in to change notification settings - Fork 0
/
rank-connector.js
201 lines (188 loc) · 4.42 KB
/
rank-connector.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
function getAuthType() {
var response = {
'type': 'NONE'
};
return response;
}
function getConfig(request) {
var config = {
configParams: [
{
type: 'TEXTINPUT',
name: 'apiKey',
displayName: 'API Key',
helpText: 'Enter the API Key you get from the Tools > Utilities > API Console',
placeholder: 'Enter your API Key in here.',
},
{
type: 'TEXTINPUT',
name: 'campaignId',
displayName: 'Campaign ID',
helpText: 'Enter the unique identifier of the website',
placeholder: 'Add your Campaign ID.',
},
{
type: 'TEXTINPUT',
name: 'domain',
displayName: 'Root Domain',
helpText: 'Enter the root domain of your site.',
placeholder: 'Enter the root domain of your website.',
}
],
dateRangeRequired: true
};
return config;
}
var fixedSchema = [
{
name: 'Date',
label: 'Date',
description: 'Date',
dataType: 'STRING',
semantics: {
conceptType: 'DIMENSION',
semanticType: 'YEAR_MONTH_DAY',
semanticGroup: 'DATETIME'
}
},
{
name: 'Url',
label: 'Site Domain',
description: 'Site title',
dataType: 'STRING',
semantics: {
conceptType: 'DIMENSION',
semanticType: 'TEXT',
semanticGroup: 'TEXT',
},
},
{
name: 'LandingPage',
label: 'Landing Page',
dataType: 'STRING',
semantics: {
conceptType: 'DIMENSION',
semanticType: 'TEXT',
semanticGroup: 'TEXT'
}
},
{
name: 'Keyword',
label: 'Keyword',
description: 'Keyword',
dataType: 'STRING',
semantics: {
conceptType: 'DIMENSION',
semanticType: 'TEXT',
semanticGroup: 'TEXT',
},
},
{
name: 'searchEngine',
label: 'Search Engine',
description: 'Name of the search engine.',
dataType: 'STRING',
semantics: {
conceptType: 'DIMENSION',
semanticType: 'TEXT',
semanticGroup: 'TEXT'
}
},
{
name: 'searchEngineName',
label: 'Search Engine Name',
description: 'Full name of the search engine.',
dataType: 'STRING',
semantics: {
conceptType: 'DIMENSION',
semanticType: 'TEXT',
semanticGroup: 'TEXT'
}
},
{
name: 'Rank',
label: 'Rank',
description: 'Keyword ranking position',
dataType: 'NUMBER',
semantics: {
conceptType: 'METRIC',
semanticType: 'NUMBER',
semanticGroup: 'NUMERIC',
isReaggregatable: false
}
}
];
function getSchema(request) {
return {schema: fixedSchema};
};
function isAdminUser() {
return true;
}
function getData(request) {
// Prepare the schema for the fields requested.
var dataSchema = [];
request.fields.forEach(function(field) {
for (var i = 0; i < fixedSchema.length; i++) {
if (fixedSchema[i].name === field.name) {
dataSchema.push(fixedSchema[i]);
break;
}
}
});
// Build URL
var url = [
'https://www.rankranger.com/api/v2/?rank&key=',
request.configParams.apiKey,
'&start_date=',
request.dateRange.startDate,
'&end_date=',
request.dateRange.endDate,
'&campaign_id=',
request.configParams.campaignId,
'&domain=',
request.configParams.domain,
'&output=json'
];
// Fetch the data
var response = UrlFetchApp.fetch(url.join(''));
var parsedResponse = JSON.parse(response.getContentText());
// Prepare the tabular data.
var data = [];
parsedResponse.result.forEach(function(keyword_obj) {
var values = [];
dataSchema.forEach(function(field) {
switch(field.name) {
case 'Date':
values.push(keyword_obj.date.replace(/(\d\d)\/(\d\d)\/(\d{4})/, "$3-$1-$2").replace(/-/g,""));
break;
case 'Url':
values.push(keyword_obj.url);
break;
case 'LandingPage':
values.push(keyword_obj.lp);
break;
case 'Keyword':
values.push(keyword_obj.keyword);
break;
case 'searchEngine':
values.push(keyword_obj.se);
break;
case 'searchEngineName':
values.push(keyword_obj.se_name);
break;
case 'Rank':
values.push(keyword_obj.rank);
break;
default:
values.push('');
}
});
data.push({
values: values
});
});
return {
schema: dataSchema,
rows: data
};
}