-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.gs
More file actions
258 lines (228 loc) · 9.11 KB
/
Main.gs
File metadata and controls
258 lines (228 loc) · 9.11 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
// Main.gs - Updated entry point with multi-data source support
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('📊 Data Connector Hub')
.addItem('Open Data Connector', 'showSidebar')
.addItem('Usage & Billing', 'showUsageDashboard')
.addSeparator()
.addSubMenu(SpreadsheetApp.getUi().createMenu('🔧 Data Sources')
.addItem('Google Analytics 4', 'showGA4Sidebar')
.addItem('Google Ads', 'showGoogleAdsSidebar')
.addItem('Facebook Ads', 'showFacebookAdsSidebar')
.addItem('Spotify', 'showSpotifySidebar')
.addItem('HubSpot', 'showHubSpotSidebar')
.addSeparator()
.addItem('Manage Data Sources', 'showDataSourceManager'))
.addItem('Help & Support', 'showHelpDialog')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle('Data Connector Hub')
.setWidth(320);
SpreadsheetApp.getUi().showSidebar(html);
}
// Individual data source shortcuts
function showGA4Sidebar() {
showDataSourceSidebar('ga4');
}
function showGoogleAdsSidebar() {
showDataSourceSidebar('google_ads');
}
function showFacebookAdsSidebar() {
showDataSourceSidebar('facebook_ads');
}
function showSpotifySidebar() {
showDataSourceSidebar('spotify');
}
function showHubSpotSidebar() {
showDataSourceSidebar('hubspot');
}
function showDataSourceSidebar(dataSourceId) {
var html = HtmlService.createHtmlOutputFromFile('Sidebar')
.setTitle(getDataSourceDisplayName(dataSourceId) + ' Connector')
.setWidth(320);
SpreadsheetApp.getUi().showSidebar(html);
// Store selected data source for sidebar initialization
PropertiesService.getScriptProperties().setProperty('SELECTED_DATA_SOURCE', dataSourceId);
}
function showDataSourceManager() {
var html = HtmlService.createHtmlOutputFromFile('DataSourceManager')
.setTitle('Data Source Manager')
.setWidth(500)
.setHeight(600);
SpreadsheetApp.getUi().showModalDialog(html, 'Data Source Manager');
}
function getDataSourceDisplayName(dataSourceId) {
var names = {
'ga4': 'Google Analytics 4',
'google_ads': 'Google Ads',
'facebook_ads': 'Facebook Ads',
'spotify': 'Spotify',
'hubspot': 'HubSpot'
};
return names[dataSourceId] || 'Unknown Data Source';
}
function getSelectedDataSource() {
return PropertiesService.getScriptProperties().getProperty('SELECTED_DATA_SOURCE') || 'ga4';
}
function setSelectedDataSource(dataSourceId) {
PropertiesService.getScriptProperties().setProperty('SELECTED_DATA_SOURCE', dataSourceId);
return { success: true };
}
// Enhanced usage dashboard with data source breakdown
function showUsageDashboard() {
try {
var usageData = getUserUsageDataBySource();
var usageHtml = createEnhancedUsageDashboardHtml(usageData);
var html = HtmlService.createHtmlOutput(usageHtml)
.setTitle('Usage & Billing')
.setWidth(500)
.setHeight(600);
SpreadsheetApp.getUi().showModalDialog(html, 'Usage & Billing');
} catch (error) {
Logger.log('Error showing usage dashboard: ' + error.toString());
var ui = SpreadsheetApp.getUi();
ui.alert(
'Usage Dashboard Error',
'Unable to load usage data. Please try again later.',
ui.ButtonSet.OK
);
}
}
function createEnhancedUsageDashboardHtml(usageData) {
var remaining = usageData.limit - usageData.count;
var percentage = Math.round((usageData.count / usageData.limit) * 100);
var html = '<!DOCTYPE html><html><head><style>' +
'body { font-family: "Google Sans", sans-serif; padding: 20px; background: #f8f9fa; }' +
'.dashboard-container { background: white; padding: 24px; border-radius: 12px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }' +
'.usage-header { text-align: center; margin-bottom: 24px; }' +
'.usage-title { font-size: 20px; font-weight: 500; margin-bottom: 8px; }' +
'.usage-subtitle { color: #5f6368; font-size: 14px; }' +
'.usage-card { border: 1px solid #dadce0; border-radius: 8px; padding: 16px; margin-bottom: 16px; }' +
'.usage-stat { display: flex; justify-content: space-between; margin-bottom: 8px; }' +
'.stat-label { color: #5f6368; }' +
'.stat-value { font-weight: 500; }' +
'.progress-bar { height: 8px; background: #f1f3f4; border-radius: 4px; overflow: hidden; margin: 12px 0; }' +
'.progress-fill { height: 100%; background: linear-gradient(90deg, #34a853 0%, #4285f4 100%); transition: width 0.3s; }' +
'.data-source-breakdown { margin-top: 16px; }' +
'.breakdown-title { font-size: 14px; font-weight: 500; margin-bottom: 8px; color: #202124; }' +
'.breakdown-item { display: flex; justify-content: space-between; padding: 4px 0; font-size: 13px; }' +
'.breakdown-source { color: #5f6368; }' +
'.breakdown-count { font-weight: 400; }' +
'.upgrade-section { text-align: center; padding: 16px; background: #e8f0fe; border-radius: 8px; margin-top: 16px; }' +
'.upgrade-button { background: #4285f4; color: white; border: none; padding: 10px 20px; border-radius: 6px; cursor: pointer; }' +
'</style></head><body>' +
'<div class="dashboard-container">' +
'<div class="usage-header">' +
'<h2 class="usage-title">Usage & Billing</h2>' +
'<p class="usage-subtitle">Track your monthly sync usage across data sources</p>' +
'</div>' +
'<div class="usage-card">' +
'<div class="usage-stat"><span class="stat-label">Current Plan:</span><span class="stat-value">' + (usageData.plan || 'Free') + '</span></div>' +
'<div class="usage-stat"><span class="stat-label">Syncs Used:</span><span class="stat-value">' + usageData.count + ' / ' + usageData.limit + '</span></div>' +
'<div class="usage-stat"><span class="stat-label">Remaining:</span><span class="stat-value">' + remaining + '</span></div>' +
'<div class="progress-bar"><div class="progress-fill" style="width: ' + percentage + '%"></div></div>';
// Add data source breakdown if available
if (usageData.bySource) {
html += '<div class="data-source-breakdown">' +
'<div class="breakdown-title">Usage by Data Source:</div>';
var sources = Object.keys(usageData.bySource);
for (var i = 0; i < sources.length; i++) {
var source = sources[i];
var count = usageData.bySource[source];
if (count > 0) {
html += '<div class="breakdown-item">' +
'<span class="breakdown-source">' + getDataSourceDisplayName(source) + '</span>' +
'<span class="breakdown-count">' + count + ' syncs</span>' +
'</div>';
}
}
html += '</div>';
}
html += '</div>';
if (usageData.plan === 'free' || !usageData.plan) {
html += '<div class="upgrade-section">' +
'<h3>Upgrade to Pro</h3>' +
'<p>Get unlimited syncs and advanced features across all data sources</p>' +
'<button class="upgrade-button" onclick="initiateUpgrade()">Upgrade Now</button>' +
'</div>';
}
html += '</div><script>' +
'function initiateUpgrade() { google.script.run.initiateUpgradeFlow(); }' +
'</script></body></html>';
return html;
}
function getUserUsageDataBySource() {
try {
var baseUsage = getUserUsageData();
// Get usage breakdown by data source
var breakdown = {
'ga4': getDataSourceUsage('ga4'),
'google_ads': getDataSourceUsage('google_ads'),
'facebook_ads': getDataSourceUsage('facebook_ads'),
'spotify': getDataSourceUsage('spotify'),
'hubspot': getDataSourceUsage('hubspot')
};
return {
count: baseUsage.count,
limit: baseUsage.limit,
plan: baseUsage.plan,
bySource: breakdown
};
} catch (error) {
Logger.log('Error getting usage data by source: ' + error.toString());
return {
count: 0,
limit: 60,
plan: 'free',
bySource: {}
};
}
}
function getDataSourceUsage(dataSourceId) {
try {
var key = 'USAGE_' + dataSourceId.toUpperCase() + '_' + getMonthStart();
var usage = PropertiesService.getScriptProperties().getProperty(key);
return usage ? parseInt(usage) : 0;
} catch (error) {
Logger.log('Error getting usage for ' + dataSourceId + ': ' + error.toString());
return 0;
}
}
function incrementDataSourceUsage(dataSourceId) {
try {
var key = 'USAGE_' + dataSourceId.toUpperCase() + '_' + getMonthStart();
var currentUsage = getDataSourceUsage(dataSourceId);
PropertiesService.getScriptProperties().setProperty(key, (currentUsage + 1).toString());
// Also increment total usage
incrementUsage();
return {
success: true,
newCount: currentUsage + 1
};
} catch (error) {
Logger.log('Error incrementing usage for ' + dataSourceId + ': ' + error.toString());
return {
success: false,
error: error.toString()
};
}
}
function showHelpDialog() {
var ui = SpreadsheetApp.getUi();
ui.alert(
'Data Connector Hub Help',
'Universal data connector for Google Sheets\n\n' +
'• Documentation: https://dflo.io/docs\n' +
'• Support: hello@dflo.io\n' +
'• Feature Requests: https://dflo.io/feedback\n\n' +
'Supported Data Sources:\n' +
'✓ Google Analytics 4\n' +
'✓ Google Ads\n' +
'✓ Facebook Ads\n' +
'✓ Spotify\n' +
'✓ HubSpot',
ui.ButtonSet.OK
);
}