-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.gs
More file actions
111 lines (95 loc) · 3.08 KB
/
Config.gs
File metadata and controls
111 lines (95 loc) · 3.08 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
// Config.gs - Centralized configuration for dflo.io backend integration
function getConfig() {
return {
// Backend API Configuration
API_BASE_URL: PropertiesService.getScriptProperties().getProperty('API_BASE_URL') || 'https://api.dflo.io',
API_KEY: PropertiesService.getScriptProperties().getProperty('API_KEY') || null,
// OAuth Configuration
OAUTH2_CLIENT_ID: PropertiesService.getScriptProperties().getProperty('OAUTH2_CLIENT_ID'),
OAUTH2_CLIENT_SECRET: PropertiesService.getScriptProperties().getProperty('OAUTH2_CLIENT_SECRET') || null,
// Usage Limits
FREE_SYNC_LIMIT: 60,
// GA4 OAuth Scopes
OAUTH_SCOPES: [
'https://www.googleapis.com/auth/analytics.readonly',
'https://www.googleapis.com/auth/userinfo.email'
],
// Add-on Information
ADDON_NAME: 'Google Analytics into Sheets by dflo',
ADDON_ID: 'google-analytics-into-sheets-dflo',
VERSION: '1.0.0'
};
}
// Setup configuration (call once during deployment)
function setupConfig(apiBaseUrl, apiKey) {
try {
var properties = PropertiesService.getScriptProperties();
properties.setProperties({
'API_BASE_URL': apiBaseUrl,
'API_KEY': apiKey
});
return {
success: true,
message: 'Configuration saved successfully'
};
} catch (error) {
Logger.log('Error setting configuration: ' + error.toString());
return {
success: false,
error: error.toString()
};
}
}
// ES5-compatible helper functions
function makeApiRequest(endpoint, options) {
var config = getConfig();
var url = config.API_BASE_URL + endpoint;
options = options || {};
options.method = options.method || 'GET';
options.headers = options.headers || {};
// Add API key authentication
if (config.API_KEY) {
options.headers['X-API-Key'] = config.API_KEY;
}
// Add user authentication if available
try {
var userEmail = getUserEmail();
if (userEmail) {
options.headers['X-User-Email'] = userEmail;
}
} catch (e) {
// User not authenticated, continue without user header
}
options.headers['Content-Type'] = 'application/json';
options.muteHttpExceptions = true;
if (options.payload && typeof options.payload === 'object') {
options.payload = JSON.stringify(options.payload);
}
try {
var response = UrlFetchApp.fetch(url, options);
var responseData = {
code: response.getResponseCode(),
body: response.getContentText(),
success: response.getResponseCode() >= 200 && response.getResponseCode() < 300
};
try {
responseData.json = JSON.parse(responseData.body);
} catch (parseError) {
responseData.json = null;
}
return responseData;
} catch (error) {
Logger.log('API request error: ' + error.toString());
return {
success: false,
error: error.toString(),
code: 0
};
}
}
// Get current month start (for usage tracking)
function getMonthStart() {
var now = new Date();
var monthStart = new Date(now.getFullYear(), now.getMonth(), 1);
return monthStart.toISOString().split('T')[0];
}