-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnostics.js
More file actions
360 lines (311 loc) · 12.4 KB
/
diagnostics.js
File metadata and controls
360 lines (311 loc) · 12.4 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* @fileoverview Missing Diagnostic Functions for Addocu v3.0
* @version 3.1 - Complete diagnostic functions for menu and coordinator
*/
// =================================================================
// MENU DIAGNOSTIC FUNCTIONS
// =================================================================
/**
* Shows account verification instructions from the troubleshooting menu.
*/
function showAccountVerification() {
try {
const ui = SpreadsheetApp.getUi();
const message = 'ACCOUNT VERIFICATION GUIDE\n\n' +
'95% of Addocu problems are caused by using different Google accounts\n' +
'in Chrome vs Google Sheets.\n\n' +
'MANUAL VERIFICATION REQUIRED:\n\n' +
'1. Check CHROME profile (top-right corner)\n' +
' • Which Google account are you signed in with?\n\n' +
'2. Check GOOGLE SHEETS account (top-right corner)\n' +
' • Which Google account is shown?\n\n' +
'3. COMPARE the two accounts:\n' +
' • Are they THE SAME account?\n' +
' • Same email address?\n\n' +
'IF THEY\'RE DIFFERENT:\n' +
'• Sign out of ALL Google accounts in Chrome\n' +
'• Sign in with only ONE account\n' +
'• Open Google Sheets with that same account\n' +
'• Try Addocu again\n\n' +
'IF THEY\'RE THE SAME:\n' +
'• The problem is NOT account mismatch\n' +
'• Try: Extensions > Addocu > Reauthorize Permissions\n\n' +
'This is the #1 cause of authorization problems!';
ui.alert('Manual Account Verification', message, ui.ButtonSet.OK);
logEvent('ACCOUNT_VERIFICATION', 'Account verification guide shown to user');
} catch (error) {
logError('ACCOUNT_VERIFICATION', `Error showing account verification: ${error.message}`);
try {
const ui = SpreadsheetApp.getUi();
ui.alert(
'Account Verification Error',
`Could not show verification guide: ${error.message}`,
ui.ButtonSet.OK
);
} catch (e) {
console.error('Critical error in account verification:', error.message);
}
}
}
/**
* Shows simplified diagnostics from the troubleshooting menu.
*/
function showSimplifiedDiagnostics() {
try {
logEvent('DIAGNOSTIC_MENU', 'Starting simplified diagnostics from menu...');
const results = simplifiedConnectionDiagnostics();
// Format results for display
let message = 'SIMPLIFIED DIAGNOSTICS\n\n';
results.forEach(result => {
const serviceName = result[0];
const account = result[1];
const status = result[2];
const details = result[3];
let indicator = '[?]';
if (status === 'OK' || status === 'SUCCESS') {
indicator = '[OK]';
} else if (status === 'ERROR' || status === 'PERMISSION_ERROR') {
indicator = '[FAIL]';
} else if (status === 'PENDING') {
indicator = '[PENDING]';
}
message += `${indicator} ${serviceName}\n`;
if (account && account !== 'N/A') {
message += ` Account: ${account}\n`;
}
if (details) {
message += ` ${details}\n`;
}
message += '\n';
});
// Add recommendations
const hasErrors = results.some(r => r[2] === 'ERROR' || r[2] === 'PERMISSION_ERROR');
const hasPending = results.some(r => r[2] === 'PENDING');
if (hasErrors) {
message += 'ISSUES DETECTED:\n';
message += 'Try: Extensions > Addocu > Reauthorize Permissions\n';
message += 'If problems persist: Manual account verification\n';
} else if (hasPending) {
message += 'ACTION NEEDED:\n';
message += 'Execute "Audit GA4" to complete authorization\n';
} else {
message += 'ALL SYSTEMS WORKING:\n';
message += 'Addocu is ready to use!\n';
}
const ui = SpreadsheetApp.getUi();
ui.alert('Simplified Diagnostics', message, ui.ButtonSet.OK);
logEvent('DIAGNOSTIC_MENU', 'Simplified diagnostics completed and shown');
} catch (error) {
logError('DIAGNOSTIC_MENU', `Error in simplified diagnostics: ${error.message}`);
try {
const ui = SpreadsheetApp.getUi();
ui.alert(
'Diagnostic Error',
`Could not complete diagnostics: ${error.message}\n\n` +
'Try manually verifying that Chrome and Google Sheets use the same account.',
ui.ButtonSet.OK
);
} catch (e) {
console.error('Critical error in simplified diagnostics:', error.message);
}
}
}
// =================================================================
// COORDINATOR CONNECTION DIAGNOSTIC FUNCTIONS
// =================================================================
/**
* Simplified connection diagnostics for coordinator and sidebar.
* @returns {Array} Array with diagnostic results.
*/
function simplifiedConnectionDiagnostics() {
try {
logEvent('DIAGNOSTIC', 'Starting simplified connection diagnostics...');
const results = [];
const servicesToTest = [
{ id: 'ga4', name: 'Google Analytics 4 Admin API' },
{ id: 'gtm', name: 'Google Tag Manager API' },
{ id: 'looker', name: 'Looker Studio API' },
{ id: 'searchConsole', name: 'Google Search Console API' },
{ id: 'youtube', name: 'YouTube Data API' },
{ id: 'googleBusinessProfile', name: 'Google Business Profile API' },
{ id: 'googleAds', name: 'Google Ads API' },
{ id: 'googleMerchantCenter', name: 'Google Merchant API' },
{ id: 'bigquery', name: 'BigQuery API' },
{ id: 'adsense', name: 'AdSense Management API' }
];
// First check basic permissions
const permissionTest = typeof testBasicPermissionsEnhanced !== 'undefined' ?
testBasicPermissionsEnhanced() :
testBasicPermissions();
if (!permissionTest.success) {
// If basic permissions fail, return permission error information
return [
['System Permissions', 'Authorization Required', 'ERROR', 'Execute reauthorization via menu'],
['Google Analytics 4 Admin API', 'Pending Authorization', 'PENDING', 'OAuth2 pending'],
['Google Tag Manager API', 'Pending Authorization', 'PENDING', 'OAuth2 pending'],
['Looker Studio API', 'Pending Authorization', 'PENDING', 'OAuth2 pending'],
['Google Search Console API', 'Pending Authorization', 'PENDING', 'OAuth2 pending'],
['YouTube Data API', 'Pending Authorization', 'PENDING', 'OAuth2 pending'],
['Google Business Profile API', 'Pending Authorization', 'PENDING', 'OAuth2 pending'],
['Google Ads API', 'Pending Authorization', 'PENDING', 'OAuth2 pending'],
['Google Merchant API', 'Pending Authorization', 'PENDING', 'OAuth2 pending'],
['BigQuery API', 'Pending Authorization', 'PENDING', 'OAuth2 pending'],
['AdSense Management API', 'Pending Authorization', 'PENDING', 'OAuth2 pending']
];
}
// Test each service
servicesToTest.forEach(service => {
try {
const result = validateService(service.id);
let status = 'ERROR';
let account = 'Unknown';
let message = result.message || 'Unknown error';
if (result.status === 'OK') {
status = 'OK';
account = result.account || 'OAuth2 connected';
message = 'Working correctly';
} else if (result.status === 'PERMISSION_ERROR') {
status = 'ERROR';
account = 'No permissions';
message = 'Reauthorization required';
} else if (result.status === 'AUTH_ERROR') {
status = 'ERROR';
account = 'Auth error';
message = 'OAuth2 authorization needed';
} else if (service.id === 'googleBusinessProfile' && (result.message && (result.message.includes('429') || result.message.includes('Rate limit')))) {
status = 'WARNING';
account = 'Quota Restricted';
message = 'GBP API quota is 0 by default. Contact Google Support to enable it.';
} else {
status = 'ERROR';
account = 'Error';
message = result.message || 'Unknown error';
}
results.push([service.name, account, status, message]);
} catch (e) {
logError('DIAGNOSTIC', `Error testing ${service.id}: ${e.message}`);
results.push([
service.name,
'Error',
'ERROR',
`Test failed: ${e.message}`
]);
}
});
logEvent('DIAGNOSTIC', `Simplified diagnostics completed for ${results.length} services`);
return results;
} catch (error) {
logError('DIAGNOSTIC', `Error in simplified diagnostics: ${error.message}`);
return [
['Addocu System', 'Critical Error', 'ERROR', `Diagnostic error: ${error.message}`],
['Recommended Action', 'Reauthorization', 'ACTION', 'Extensions > Addocu > 🔄 Reauthorize Permissions']
];
}
}
/**
* Complete connection diagnostics with detailed information.
* @returns {Array} Array with complete diagnostic results.
*/
function diagnoseConnectionsComplete() {
try {
logEvent('DIAGNOSTIC_COMPLETE', 'Starting complete connection diagnostics...');
// Use the existing complete diagnostic function if available
if (typeof runCompleteConnectivityDiagnostic !== 'undefined') {
const detailedResults = runCompleteConnectivityDiagnostic();
// Convert to expected format
return detailedResults.map(result => [
result[0], // Service name
result[1], // Account
result[2], // Status
result[3] // Message
]);
}
// Fallback to simplified diagnostics
return simplifiedConnectionDiagnostics();
} catch (error) {
logError('DIAGNOSTIC_COMPLETE', `Error in complete diagnostics: ${error.message}`);
return [
['Diagnostic System', 'Error', 'ERROR', `Complete diagnostic failed: ${error.message}`],
['Fallback', 'Simplified Mode', 'INFO', 'Using simplified diagnostics instead']
];
}
}
// =================================================================
// BASIC PERMISSION TESTING (FALLBACK)
// =================================================================
/**
* Tests basic permissions required for Add-on functionality.
* Fallback version when enhanced version is not available.
* @returns {Object} Permission test results.
*/
function testBasicPermissions() {
const results = {
success: true,
permissions: {},
errors: []
};
// Test 1: UserProperties
try {
const userProps = PropertiesService.getUserProperties();
userProps.setProperty('ADDOCU_PERMISSION_TEST', Date.now().toString());
const testValue = userProps.getProperty('ADDOCU_PERMISSION_TEST');
results.permissions.userProperties = !!testValue;
} catch (error) {
results.permissions.userProperties = false;
results.errors.push(`UserProperties: ${error.message}`);
results.success = false;
}
// Test 2: Spreadsheet
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
results.permissions.spreadsheet = !!ss;
} catch (error) {
results.permissions.spreadsheet = false;
results.errors.push(`Spreadsheet: ${error.message}`);
results.success = false;
}
// Test 3: OAuth Token
try {
const token = ScriptApp.getOAuthToken();
results.permissions.oauth = !!token;
} catch (error) {
results.permissions.oauth = false;
results.errors.push(`OAuth: ${error.message}`);
results.success = false;
}
// Test 4: UI Access
try {
const ui = SpreadsheetApp.getUi();
results.permissions.ui = !!ui;
} catch (error) {
results.permissions.ui = false;
results.errors.push(`UI: ${error.message}`);
results.success = false;
}
return results;
}
// =================================================================
// FALLBACK DIAGNOSTIC FUNCTIONS
// =================================================================
/**
* Emergency diagnostic function when all else fails.
* @returns {Array} Basic diagnostic information.
*/
function emergencyDiagnostic() {
try {
const user = Session.getActiveUser().getEmail();
const timestamp = new Date().toISOString();
return [
['Emergency Diagnostic', 'Active', 'INFO', `User: ${user}`],
['Timestamp', timestamp, 'INFO', 'Diagnostic executed'],
['Recommendation', 'Manual Reauthorization', 'ACTION', 'Extensions > Addocu > 🔄 Reauthorize Permissions'],
['Alternative', 'Account Verification', 'ACTION', 'Verify Chrome/Sheets use same account']
];
} catch (error) {
return [
['Emergency Diagnostic', 'Failed', 'ERROR', error.message],
['Critical Issue', 'System Error', 'ERROR', 'Contact support'],
['Immediate Action', 'Reinstall Add-on', 'ACTION', 'Uninstall and reinstall from Marketplace']
];
}
}