-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_ads.js
More file actions
365 lines (314 loc) · 12.1 KB
/
google_ads.js
File metadata and controls
365 lines (314 loc) · 12.1 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
/**
* @fileoverview Module for Google Ads Integration
* @version 1.1 - Added Developer Token support
*/
/**
* Syncs Google Ads data with UI feedback.
*/
function syncGoogleAdsWithUI() {
const ui = SpreadsheetApp.getUi();
const result = syncGoogleAdsCore();
if (result.success) {
const details = result.details || {};
const body = `Accounts: ${result.message} | Campaigns: ${details.campaigns || 0} | Conversions: ${details.conversions || 0} | Audiences: ${details.audiences || 0}\n\n` +
`Total: ${result.records} elements | Time: 0s\n\n` +
`Data written to ADS_CAMPAIGNS, ADS_CONVERSIONS, ADS_AUDIENCES.`;
ui.alert('Google Ads Synchronized', body, ui.ButtonSet.OK);
} else {
const body = `Synchronization failed: ${result.message}\n\n` +
`Action: Ensure your Developer Token is valid and configured in Addocu settings, and that the Google Ads API is enabled in your Google Cloud Project.\n\n` +
`Details: Check LOGS sheet for more information.`;
ui.alert('Google Ads Error', body, ui.ButtonSet.OK);
}
}
/**
* Core function to sync Google Ads data.
* @returns {Object} Result object.
*/
function syncGoogleAdsCore() {
try {
logEvent('GOOGLE_ADS', 'Starting Google Ads audit...', 'INFO');
// 1. Validate Service & Token
const authConfig = getAuthConfig('googleAds'); // Checks for OAuth + Dev Token
// 2. Fetch Accessible Customers
const customers = listAccessibleCustomers(authConfig);
if (!customers || customers.length === 0) {
logEvent('GOOGLE_ADS', 'No accessible Google Ads customers found.');
// Continue to write empty sheets for feedback
}
let allCampaigns = [];
let allConversions = [];
let allAudiences = [];
// 3. Process each customer
// Note: In a real MCC scenario, we should traverse the hierarchy.
// For this MVP, we iterate accessible customers directly.
for (const customerResourceName of customers) {
const customerId = customerResourceName.split('/')[1];
try {
const campaigns = getGoogleAdsCampaigns(customerId, authConfig);
allCampaigns = allCampaigns.concat(campaigns);
const conversions = getGoogleAdsConversionActions(customerId, authConfig);
allConversions = allConversions.concat(conversions);
const audiences = getGoogleAdsAudiences(customerId, authConfig);
allAudiences = allAudiences.concat(audiences);
} catch (e) {
logWarning('GOOGLE_ADS', `Skipping customer ${customerId}: ${e.message}`);
}
}
// 4. Write to Sheets
writeGoogleAdsToSheet(allCampaigns);
writeGoogleAdsConversionsToSheet(allConversions);
writeGoogleAdsAudiencesToSheet(allAudiences);
logEvent('GOOGLE_ADS', `Completed Google Ads audit. Campaigns: ${allCampaigns.length}, Conversions: ${allConversions.length}, Audiences: ${allAudiences.length}`);
return {
success: true,
status: 'SUCCESS',
message: `Processed ${customers.length} accounts`,
records: allCampaigns.length + allConversions.length + allAudiences.length,
details: {
campaigns: allCampaigns.length,
conversions: allConversions.length,
audiences: allAudiences.length
}
};
} catch (error) {
// Use granular error handler for better user messaging
const authError = handleAuthError('Google Ads', error);
if (authError.status === 'AUTH_FAILED') {
logError('GOOGLE_ADS', `Authentication error: ${authError.userMessage}`);
const ADS_ERROR_HEADERS = ['Customer ID', 'Campaign ID', 'Campaign Name', 'Status', 'Advertising Channel', 'Budget Type', 'Budget Amount (Micros)', 'Budget ID', 'Start Date', 'End Date', 'Target CPA (Micros)', 'Target ROAS', 'Strategy Type', 'Sync Date'];
writeDataToSheet('GOOGLE_ADS_CAMPAIGNS', ADS_ERROR_HEADERS, null, 'Google Ads', authError.userMessage);
return {
success: false,
status: 'AUTH_FAILED',
message: authError.userMessage,
records: 0,
actionItems: authError.actionItems
};
}
logError('GOOGLE_ADS', `Fatal error in Google Ads audit: ${error.message}`);
const ADS_ERROR_HEADERS = ['Customer ID', 'Campaign ID', 'Campaign Name', 'Status', 'Advertising Channel', 'Budget Type', 'Budget Amount (Micros)', 'Budget ID', 'Start Date', 'End Date', 'Target CPA (Micros)', 'Target ROAS', 'Strategy Type', 'Sync Date'];
writeDataToSheet('GOOGLE_ADS_CAMPAIGNS', ADS_ERROR_HEADERS, null, 'Google Ads', error.message);
return {
success: false,
status: 'ERROR',
message: error.message,
records: 0
};
}
}
/**
* Lists accessible customers for the user.
* @param {Object} authConfig - Auth configuration.
* @returns {Array<string>} List of customer resource names.
*/
function listAccessibleCustomers(authConfig) {
const url = `${ADDOCU_CONFIG.apis.googleAds.baseUrl}`;
try {
const response = UrlFetchApp.fetch(url, {
method: 'get',
headers: authConfig.headers,
muteHttpExceptions: true
});
const code = response.getResponseCode();
const text = response.getContentText();
if (code !== 200) {
throw new Error(`API Error (${code}): ${text}`);
}
const data = JSON.parse(text);
return data.resourceNames || [];
} catch (e) {
logError('GOOGLE_ADS', `Error listing customers: ${e.message}`);
throw e;
}
}
/**
* Fetches campaigns for a specific customer.
* @param {string} customerId - The customer ID (123-456-7890 format stripped).
* @param {Object} authConfig - Auth configuration.
* @returns {Array<Object>} List of campaigns.
*/
function getGoogleAdsCampaigns(customerId, authConfig) {
// We need to set the login-customer-id header to the target customer
// OR the MCC ID if we were authenticating via MCC.
// For 'listAccessibleCustomers', we don't need it.
// For searching a specific client, we usually need it if we are acting as them.
// However, simple direct access might work if the user is a direct admin.
// We will try adding the login-customer-id header as the customerId itself.
const headers = { ...authConfig.headers, 'login-customer-id': customerId };
const query = `
SELECT
campaign.id,
campaign.name,
campaign.status,
campaign.advertising_channel_type,
campaign.start_date,
campaign.end_date,
customer.descriptive_name,
customer.id
FROM campaign
ORDER BY campaign.id
`;
const url = `https://googleads.googleapis.com/v20/customers/${customerId}/googleAds:search`;
try {
const response = UrlFetchApp.fetch(url, {
method: 'post',
headers: headers,
contentType: 'application/json',
payload: JSON.stringify({ query: query }),
muteHttpExceptions: true
});
const code = response.getResponseCode();
if (code !== 200) {
// Common error: user cannot access this specific sub-account directly
// logging strictly warning to avoid stopping the whole process
logWarning('GOOGLE_ADS', `Cannot access customer ${customerId} details: ${response.getContentText().substring(0, 100)}`);
return [];
}
const data = JSON.parse(response.getContentText());
if (!data.results) return [];
return data.results.map(row => ({
accountId: row.customer.id,
accountName: row.customer.descriptiveName,
campaignId: row.campaign.id,
campaignName: row.campaign.name,
status: row.campaign.status,
type: row.campaign.advertisingChannelType,
startDate: row.campaign.startDate,
endDate: row.campaign.endDate
}));
} catch (e) {
logError('GOOGLE_ADS', `Error fetching campaigns for ${customerId}: ${e.message}`);
return [];
}
}
/**
* Writes Google Ads data to the sheet.
* @param {Array<Object>} campaigns - List of campaigns.
*/
function writeGoogleAdsToSheet(campaigns) {
if (!campaigns || campaigns.length === 0) return;
const headers = [
'Customer ID', 'Campaign ID', 'Campaign Name', 'Status', 'Advertising Channel',
'Budget Type', 'Budget Amount (Micros)', 'Budget ID', 'Start Date', 'End Date',
'Target CPA (Micros)', 'Target ROAS', 'Strategy Type', 'Sync Date'
];
writeDataToSheet('ADS_CAMPAIGNS', headers, campaigns, 'Google Ads');
}
/**
* Fetches conversion actions for a specific customer.
* @param {string} customerId - The customer ID.
* @param {Object} authConfig - Auth configuration.
* @returns {Array<Object>} List of conversion actions.
*/
function getGoogleAdsConversionActions(customerId, authConfig) {
const headers = { ...authConfig.headers, 'login-customer-id': customerId };
const query = `
SELECT
conversion_action.id,
conversion_action.name,
conversion_action.status,
conversion_action.type,
conversion_action.category,
customer.descriptive_name,
customer.id
FROM conversion_action
ORDER BY conversion_action.id
`;
const url = `https://googleads.googleapis.com/v20/customers/${customerId}/googleAds:search`;
try {
const response = UrlFetchApp.fetch(url, {
method: 'post',
headers: headers,
contentType: 'application/json',
payload: JSON.stringify({ query: query }),
muteHttpExceptions: true
});
if (response.getResponseCode() !== 200) return [];
const data = JSON.parse(response.getContentText());
if (!data.results) return [];
return data.results.map(row => ({
accountId: row.customer.id,
accountName: row.customer.descriptiveName,
conversionId: row.conversionAction.id,
conversionName: row.conversionAction.name,
status: row.conversionAction.status,
type: row.conversionAction.type,
category: row.conversionAction.category
}));
} catch (e) {
logWarning('GOOGLE_ADS', `Error fetching conversions for ${customerId}: ${e.message}`);
return [];
}
}
/**
* Fetches user lists (audiences) for a specific customer.
* @param {string} customerId - The customer ID.
* @param {Object} authConfig - Auth configuration.
* @returns {Array<Object>} List of user lists.
*/
function getGoogleAdsAudiences(customerId, authConfig) {
const headers = { ...authConfig.headers, 'login-customer-id': customerId };
const query = `
SELECT
user_list.id,
user_list.name,
user_list.type,
user_list.membership_status,
user_list.size_for_search,
user_list.size_for_display,
customer.descriptive_name,
customer.id
FROM user_list
ORDER BY user_list.id
`;
const url = `https://googleads.googleapis.com/v20/customers/${customerId}/googleAds:search`;
try {
const response = UrlFetchApp.fetch(url, {
method: 'post',
headers: headers,
contentType: 'application/json',
payload: JSON.stringify({ query: query }),
muteHttpExceptions: true
});
if (response.getResponseCode() !== 200) return [];
const data = JSON.parse(response.getContentText());
if (!data.results) return [];
return data.results.map(row => ({
accountId: row.customer.id,
accountName: row.customer.descriptiveName,
listId: row.userList.id,
listName: row.userList.name,
type: row.userList.type,
status: row.userList.membershipStatus,
sizeSearch: row.userList.sizeForSearch,
sizeDisplay: row.userList.sizeForDisplay
}));
} catch (e) {
logWarning('GOOGLE_ADS', `Error fetching audiences for ${customerId}: ${e.message}`);
return [];
}
}
/**
* Writes Google Ads Conversion Actions to the sheet.
* @param {Array<Object>} conversions - List of conversions.
*/
function writeGoogleAdsConversionsToSheet(conversions) {
const headers = [
'Customer ID', 'Conversion ID', 'Name', 'Type', 'Status', 'Category',
'Origin', 'Count Type', 'Value Format', 'View-through Lookback',
'Attribution Model', 'Sync Date'
];
writeDataToSheet('ADS_CONVERSIONS', headers, conversions, 'Google Ads');
}
/**
* Writes Google Ads Audiences (User Lists) to the sheet.
* @param {Array<Object>} audiences - List of audiences.
*/
function writeGoogleAdsAudiencesToSheet(audiences) {
const headers = [
'Customer ID', 'Audience ID', 'Name', 'Resource Name', 'Description',
'Type', 'Size', 'Status', 'Notes', 'Sync Date'
];
writeDataToSheet('ADS_AUDIENCES', headers, audiences, 'Google Ads');
}