-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSource.gs
More file actions
455 lines (415 loc) · 13.2 KB
/
DataSource.gs
File metadata and controls
455 lines (415 loc) · 13.2 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// DataSource.gs - Base data source interface and management
function getDataSourceConfig() {
return {
'ga4': {
name: 'Google Analytics 4',
authType: 'oauth2',
scopes: [
'https://www.googleapis.com/auth/analytics.readonly',
'https://www.googleapis.com/auth/userinfo.email'
],
baseUrl: 'https://analyticsdata.googleapis.com/v1beta',
adminUrl: 'https://analyticsadmin.googleapis.com/v1beta',
icon: '📊',
color: '#ff6d01'
},
'hubspot': {
name: 'HubSpot',
authType: 'oauth2',
scopes: ['crm.objects.contacts.read', 'crm.objects.companies.read', 'crm.objects.deals.read'],
baseUrl: 'https://api.hubapi.com',
icon: '🔄',
color: '#ff7a59'
},
'google_ads': {
name: 'Google Ads',
authType: 'oauth2',
scopes: [
'https://www.googleapis.com/auth/adwords'
],
baseUrl: 'https://googleads.googleapis.com/v14',
icon: '🎯',
color: '#4285f4'
},
'facebook_ads': {
name: 'Facebook Ads',
authType: 'oauth2',
scopes: ['ads_read', 'ads_management'],
baseUrl: 'https://graph.facebook.com/v18.0',
icon: '📘',
color: '#1877f2'
},
'spotify': {
name: 'Spotify',
authType: 'oauth2',
scopes: ['user-read-private', 'user-read-email', 'user-read-playbook-state'],
baseUrl: 'https://api.spotify.com/v1',
icon: '🎵',
color: '#1db954'
}
};
}
function getAllDataSourceStatus() {
try {
var config = getDataSourceConfig();
var status = {};
var sourceIds = Object.keys(config);
for (var i = 0; i < sourceIds.length; i++) {
var sourceId = sourceIds[i];
status[sourceId] = {
connected: isDataSourceConnected(sourceId),
lastSync: getDataSourceLastSync(sourceId),
syncCount: getDataSourceUsage(sourceId)
};
}
return status;
} catch (error) {
Logger.log('Error getting all data source status: ' + error.toString());
return {};
}
}
function isDataSourceConnected(dataSourceId) {
try {
var key = 'AUTH_' + dataSourceId.toUpperCase() + '_CONNECTED';
var connected = PropertiesService.getScriptProperties().getProperty(key);
return connected === 'true';
} catch (error) {
Logger.log('Error checking connection for ' + dataSourceId + ': ' + error.toString());
return false;
}
}
function setDataSourceConnected(dataSourceId, connected) {
try {
var key = 'AUTH_' + dataSourceId.toUpperCase() + '_CONNECTED';
PropertiesService.getScriptProperties().setProperty(key, connected.toString());
if (connected) {
setDataSourceLastSync(dataSourceId, new Date().getTime());
}
return { success: true };
} catch (error) {
Logger.log('Error setting connection status for ' + dataSourceId + ': ' + error.toString());
return { success: false, error: error.toString() };
}
}
function getDataSourceLastSync(dataSourceId) {
try {
var key = 'LAST_SYNC_' + dataSourceId.toUpperCase();
var timestamp = PropertiesService.getScriptProperties().getProperty(key);
return timestamp ? parseInt(timestamp) : null;
} catch (error) {
Logger.log('Error getting last sync for ' + dataSourceId + ': ' + error.toString());
return null;
}
}
function setDataSourceLastSync(dataSourceId, timestamp) {
try {
var key = 'LAST_SYNC_' + dataSourceId.toUpperCase();
PropertiesService.getScriptProperties().setProperty(key, timestamp.toString());
return { success: true };
} catch (error) {
Logger.log('Error setting last sync for ' + dataSourceId + ': ' + error.toString());
return { success: false, error: error.toString() };
}
}
function connectDataSource(dataSourceId) {
try {
var config = getDataSourceConfig();
if (!config[dataSourceId]) {
return {
success: false,
error: 'Unknown data source: ' + dataSourceId
};
}
var sourceConfig = config[dataSourceId];
// Handle different authentication types
if (sourceConfig.authType === 'oauth2') {
return initiateOAuth2Flow(dataSourceId, sourceConfig);
} else {
return {
success: false,
error: 'Unsupported authentication type: ' + sourceConfig.authType
};
}
} catch (error) {
Logger.log('Error connecting data source ' + dataSourceId + ': ' + error.toString());
return {
success: false,
error: error.toString()
};
}
}
function initiateOAuth2Flow(dataSourceId, sourceConfig) {
try {
// For GA4, use existing authentication flow
if (dataSourceId === 'ga4') {
var authUrl = getAuthUrl();
return {
success: true,
authUrl: authUrl
};
}
// For other data sources, implement specific OAuth flows
switch (dataSourceId) {
case 'google_ads':
return initiateGoogleAdsAuth(sourceConfig);
case 'facebook_ads':
return initiateFacebookAdsAuth(sourceConfig);
case 'spotify':
return initiateSpotifyAuth(sourceConfig);
case 'hubspot':
return initiateHubSpotAuth(sourceConfig);
default:
return {
success: false,
error: 'Authentication not yet implemented for ' + dataSourceId
};
}
} catch (error) {
Logger.log('Error initiating OAuth2 flow for ' + dataSourceId + ': ' + error.toString());
return {
success: false,
error: error.toString()
};
}
}
function disconnectDataSource(dataSourceId) {
try {
// Remove authentication tokens
var keys = [
'AUTH_' + dataSourceId.toUpperCase() + '_CONNECTED',
'AUTH_' + dataSourceId.toUpperCase() + '_TOKEN',
'AUTH_' + dataSourceId.toUpperCase() + '_REFRESH_TOKEN',
'AUTH_' + dataSourceId.toUpperCase() + '_EXPIRES'
];
var properties = PropertiesService.getScriptProperties();
for (var i = 0; i < keys.length; i++) {
properties.deleteProperty(keys[i]);
}
return { success: true };
} catch (error) {
Logger.log('Error disconnecting data source ' + dataSourceId + ': ' + error.toString());
return {
success: false,
error: error.toString()
};
}
}
function openDataSourceSidebar(dataSourceId) {
try {
setSelectedDataSource(dataSourceId);
var config = getDataSourceConfig();
var sourceName = config[dataSourceId] ? config[dataSourceId].name : 'Data Source';
var html = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle(sourceName + ' Connector')
.setWidth(320);
SpreadsheetApp.getUi().showSidebar(html);
return { success: true };
} catch (error) {
Logger.log('Error opening data source sidebar: ' + error.toString());
return {
success: false,
error: error.toString()
};
}
}
function getDataSourceMetadata(dataSourceId) {
try {
switch (dataSourceId) {
case 'ga4':
return {
dimensions: getAvailableDimensions(),
metrics: getAvailableMetrics(),
properties: []
};
case 'google_ads':
return getGoogleAdsMetadata();
case 'facebook_ads':
return getFacebookAdsMetadata();
case 'spotify':
return getSpotifyMetadata();
case 'hubspot':
return getHubSpotMetadata();
default:
return {
dimensions: [],
metrics: [],
properties: []
};
}
} catch (error) {
Logger.log('Error getting metadata for ' + dataSourceId + ': ' + error.toString());
return {
dimensions: [],
metrics: [],
properties: []
};
}
}
// Placeholder implementations for other data sources
function initiateGoogleAdsAuth(sourceConfig) {
// Full implementation is now in GoogleAds.gs
return initiateGoogleAdsAuth(sourceConfig);
}
function initiateFacebookAdsAuth(sourceConfig) {
return {
success: false,
error: 'Facebook Ads authentication coming soon'
};
}
function initiateSpotifyAuth(sourceConfig) {
return {
success: false,
error: 'Spotify authentication coming soon'
};
}
function initiateHubSpotAuth(sourceConfig) {
// Full implementation is now in HubSpot.gs
return initiateHubSpotAuth(sourceConfig);
}
function getHubSpotMetadata() {
// Full implementation is now in HubSpot.gs
return getHubSpotMetadata();
}
function getGoogleAdsMetadata() {
// Full implementation is now in GoogleAds.gs
return getGoogleAdsMetadata();
}
function getFacebookAdsMetadata() {
return {
dimensions: [
{ name: 'date_start', displayName: 'Date Start', category: 'Time', description: 'Start date' },
{ name: 'campaign_name', displayName: 'Campaign Name', category: 'Campaign', description: 'Campaign name' },
{ name: 'adset_name', displayName: 'Ad Set Name', category: 'Ad Set', description: 'Ad set name' }
],
metrics: [
{ name: 'impressions', displayName: 'Impressions', category: 'Performance', description: 'Number of impressions' },
{ name: 'clicks', displayName: 'Clicks', category: 'Performance', description: 'Number of clicks' },
{ name: 'spend', displayName: 'Spend', category: 'Cost', description: 'Amount spent' },
{ name: 'actions', displayName: 'Actions', category: 'Actions', description: 'Number of actions' }
],
properties: []
};
}
function getSpotifyMetadata() {
return {
dimensions: [
{ name: 'date', displayName: 'Date', category: 'Time', description: 'Date' },
{ name: 'track_name', displayName: 'Track Name', category: 'Track', description: 'Track name' },
{ name: 'artist_name', displayName: 'Artist Name', category: 'Artist', description: 'Artist name' }
],
metrics: [
{ name: 'streams', displayName: 'Streams', category: 'Performance', description: 'Number of streams' },
{ name: 'listeners', displayName: 'Listeners', category: 'Performance', description: 'Number of listeners' },
{ name: 'followers', displayName: 'Followers', category: 'Audience', description: 'Number of followers' }
],
properties: []
};
}
function getHubSpotMetadata() {
return {
dimensions: [
{ name: 'createdate', displayName: 'Create Date', category: 'Time', description: 'Date created' },
{ name: 'firstname', displayName: 'First Name', category: 'Contact', description: 'First name' },
{ name: 'lastname', displayName: 'Last Name', category: 'Contact', description: 'Last name' },
{ name: 'company', displayName: 'Company', category: 'Company', description: 'Company name' }
],
metrics: [
{ name: 'num_contacted_notes', displayName: 'Contact Notes', category: 'Activity', description: 'Number of contact notes' },
{ name: 'num_conversion_events', displayName: 'Conversion Events', category: 'Conversions', description: 'Number of conversion events' },
{ name: 'hs_analytics_num_visits', displayName: 'Visits', category: 'Analytics', description: 'Number of visits' }
],
properties: []
};
}
function executeDataSourceReport(dataSourceId, config) {
try {
switch (dataSourceId) {
case 'ga4':
return executeGA4Report(config);
case 'google_ads':
return executeGoogleAdsReport(config);
case 'facebook_ads':
return executeFacebookAdsReport(config);
case 'spotify':
return executeSpotifyReport(config);
case 'hubspot':
return executeHubSpotReport(config);
default:
return {
success: false,
error: 'Report execution not implemented for ' + dataSourceId
};
}
} catch (error) {
Logger.log('Error executing report for ' + dataSourceId + ': ' + error.toString());
return {
success: false,
error: error.toString()
};
}
}
function executeGA4Report(config) {
var dateRange = config.dateRangeComputed || config.dateRange;
return fetchGA4Report(
config.propertyId,
dateRange.start,
dateRange.end,
config.dimensions.join(','),
config.metrics.join(','),
config.limit
);
}
// Placeholder implementations for other data source reports
function executeGoogleAdsReport(config) {
// Full implementation is now in GoogleAds.gs
return executeGoogleAdsReport(config);
}
function executeFacebookAdsReport(config) {
return {
success: false,
error: 'Facebook Ads reports coming soon'
};
}
function executeSpotifyReport(config) {
return {
success: false,
error: 'Spotify reports coming soon'
};
}
function executeHubSpotReport(config) {
// Full implementation is now in HubSpot.gs
return executeHubSpotReport(config);
}
function checkDataSourceAuthentication(dataSourceId) {
try {
if (dataSourceId === 'ga4') {
return {
isConnected: isAuthenticated(),
dataSourceId: dataSourceId
};
}
if (dataSourceId === 'google_ads') {
return {
isConnected: isGoogleAdsAuthenticated(),
dataSourceId: dataSourceId
};
}
if (dataSourceId === 'hubspot') {
return {
isConnected: isHubSpotAuthenticated(),
dataSourceId: dataSourceId
};
}
return {
isConnected: isDataSourceConnected(dataSourceId),
dataSourceId: dataSourceId
};
} catch (error) {
Logger.log('Error checking authentication for ' + dataSourceId + ': ' + error.toString());
return {
isConnected: false,
dataSourceId: dataSourceId,
error: error.toString()
};
}
}