-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnegative-keyword-conflict-resolver.js
More file actions
794 lines (668 loc) · 30.8 KB
/
negative-keyword-conflict-resolver.js
File metadata and controls
794 lines (668 loc) · 30.8 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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/**
* Google Ads Negative Keyword Conflict Resolver
*
* This script identifies and removes negative keywords that conflict with
* positive keywords in your Google Ads account, preventing situations where
* your ads wouldn't show due to contradictory keyword settings.
*
* The script handles conflicts at three levels:
* 1. Ad group level negative keywords
* 2. Campaign level negative keywords
* 3. Shared negative keyword lists
*
* Enhanced to handle more sophisticated conflict detection including partial matches
* and different match types.
*/
// Configuration
const CONFIG = {
// Set to true to see what would be removed without actually removing anything
dryRun: false,
// Advanced conflict detection settings
detectPartialConflicts: true, // Check if negative keywords are contained within positive keywords
// Logging settings
detailedLogging: true, // Set to true for more verbose logging
// Email reporting settings
sendEmail: true,
emailAddress: 'youremail@email.com',
// Date range for analysis (leave empty for all-time)
dateRange: '', // Remove this to avoid the DURING syntax issue
// Performance safeguards
maxKeywordsToProcess: 1000000 // Set a limit to prevent timeouts
};
/**
* Main function that orchestrates the conflict detection and removal process.
*/
function main() {
try {
Logger.log('Starting Negative Keyword Conflict Resolution Script...');
// Validate our conflict detection algorithm first
if (!validateConflictDetection()) {
Logger.log('WARNING: Conflict detection validation failed. Some conflicts may be incorrectly identified.');
// If in dry run mode, exit to prevent incorrect actions
if (CONFIG.dryRun) {
Logger.log('Exiting due to validation failure in dry run mode.');
return;
}
// Otherwise, continue with a warning
Logger.log('Continuing despite validation failure. Use caution when reviewing results.');
}
// Track metrics for reporting
const metrics = {
adGroupConflictsFound: 0,
campaignConflictsFound: 0,
sharedListConflictsFound: 0,
adGroupConflictsRemoved: 0,
campaignConflictsRemoved: 0,
sharedListConflictsRemoved: 0,
falsePositivesAvoided: 0
};
// 1) Build a lookup of all active positive keywords.
Logger.log('Building positive keywords map...');
const positiveKeywordsMap = buildPositiveKeywordsMap();
// 2) Check conflicts at the ad group level.
Logger.log('Checking ad group-level conflicts...');
metrics.adGroupConflictsRemoved = removeAdGroupNegativeConflicts(positiveKeywordsMap, metrics);
// 3) Check conflicts at the campaign level.
Logger.log('Checking campaign-level conflicts...');
metrics.campaignConflictsRemoved = removeCampaignNegativeConflicts(positiveKeywordsMap, metrics);
// 4) Check conflicts at the shared list level.
Logger.log('Checking shared list conflicts...');
metrics.sharedListConflictsRemoved = removeSharedListNegativeConflicts(positiveKeywordsMap, metrics);
// 5) Generate report
const report = generateReport(metrics);
Logger.log(report);
// 6) Email report if configured
if (CONFIG.sendEmail) {
MailApp.sendEmail({
to: CONFIG.emailAddress,
subject: 'Google Ads Negative Keyword Conflict Report',
body: report
});
}
Logger.log('Script completed successfully.');
} catch (error) {
Logger.log('Error executing script: ' + error);
if (CONFIG.sendEmail) {
MailApp.sendEmail({
to: CONFIG.emailAddress,
subject: 'ERROR: Google Ads Negative Keyword Conflict Script',
body: 'The script encountered an error: ' + error
});
}
}
}
/**
* Gathers all enabled (active) positive keywords and organizes them by
* campaign ID and ad group ID. Returns a nested map.
*
* @return {Object} A map organized by campaign ID and ad group ID
*/
function buildPositiveKeywordsMap() {
const map = {
byId: {}, // Organized by campaign/ad group ID
allKeywords: new Set(), // Set of all keyword texts for quick lookup
keywordDetails: [] // Array of all keyword details for advanced conflict checking
};
// Apply date range condition if specified and only include keywords in ENABLED ad groups and campaigns
let condition = "Status = ENABLED AND AdGroupStatus = ENABLED AND CampaignStatus = ENABLED";
if (CONFIG.dateRange) {
condition += " AND metrics.impressions > 0 DURING " + CONFIG.dateRange;
}
// Use the correct pagination approach for Google Ads Scripts
const keywordIterator = AdsApp.keywords()
.withCondition(condition)
.get();
let count = 0;
Logger.log("Processing keywords...");
while (keywordIterator.hasNext()) {
if (count >= CONFIG.maxKeywordsToProcess) {
Logger.log(`Reached maximum keyword processing limit (${CONFIG.maxKeywordsToProcess}). Consider running in smaller batches.`);
break;
}
const keyword = keywordIterator.next();
const campaignId = keyword.getCampaign().getId();
const adGroupId = keyword.getAdGroup().getId();
const campaignName = keyword.getCampaign().getName();
const adGroupName = keyword.getAdGroup().getName();
const text = keyword.getText().toLowerCase();
const matchType = keyword.getMatchType();
// Add to the ID-based map
if (!map.byId[campaignId]) {
map.byId[campaignId] = {
name: campaignName,
adGroups: {}
};
}
if (!map.byId[campaignId].adGroups[adGroupId]) {
map.byId[campaignId].adGroups[adGroupId] = {
name: adGroupName,
keywords: {}
};
}
// Store with match type
map.byId[campaignId].adGroups[adGroupId].keywords[text] = matchType;
// Add to the full set for quick lookup
map.allKeywords.add(text);
// Add detailed info for advanced conflict checking
map.keywordDetails.push({
text: text,
matchType: matchType,
campaignId: campaignId,
campaignName: campaignName,
adGroupId: adGroupId,
adGroupName: adGroupName
});
count++;
// Log progress periodically
if (count % 5000 === 0) {
Logger.log(`Processed ${count} keywords...`);
}
}
Logger.log(`Found ${count} active positive keywords across ${Object.keys(map.byId).length} campaigns`);
return map;
}
/**
* Checks if a negative keyword conflicts with a positive keyword based on text and match types.
* Enhanced with more accurate conflict detection based on how Google Ads actually processes negatives.
*
* @param {string} negativeText The negative keyword text
* @param {string} negativeMatchType The negative keyword match type
* @param {string} positiveText The positive keyword text
* @param {string} positiveMatchType The positive keyword match type
* @return {boolean} Whether a conflict exists
*/
function hasKeywordConflict(negativeText, negativeMatchType, positiveText, positiveMatchType) {
// Normalize inputs
negativeText = negativeText.toLowerCase().trim();
positiveText = positiveText.toLowerCase().trim();
// For BROAD match negatives:
if (negativeMatchType === "BROAD") {
// Split negative into individual words
const negTerms = negativeText.replace(/^\+/, '').split(/\s+/);
// For single-word broad negatives, check if it appears as a complete word in the positive
if (negTerms.length === 1) {
const negTerm = negTerms[0];
// Use word boundary regex to ensure we're matching whole words, not parts of words
const wordBoundaryRegex = new RegExp(`\\b${negTerm}\\b`, 'i');
return wordBoundaryRegex.test(positiveText);
}
// For multi-word broad negatives, all words must appear in the positive (in any order)
const posTerms = positiveText.split(/\s+/);
return negTerms.every(term => {
// Check if this term appears as a complete word in the positive keyword
return posTerms.some(posTerm => posTerm === term);
});
}
// For PHRASE match negatives:
if (negativeMatchType === "PHRASE") {
// Short phrase negatives (1-2 characters) often cause false positives
// They need to appear as standalone words/phrases, not as part of larger words
if (negativeText.length <= 2) {
// Check if the short negative appears as a complete word/phrase
const wordBoundaryRegex = new RegExp(`\\b${negativeText}\\b`, 'i');
return wordBoundaryRegex.test(positiveText);
}
// For longer phrase negatives, check if they appear as a continuous phrase
// Split both into words to ensure we're matching complete words
const negWords = negativeText.split(/\s+/);
const posWords = positiveText.split(/\s+/);
// Look for the sequence of negative words within positive words
for (let i = 0; i <= posWords.length - negWords.length; i++) {
let match = true;
for (let j = 0; j < negWords.length; j++) {
if (posWords[i + j] !== negWords[j]) {
match = false;
break;
}
}
if (match) return true;
}
return false;
}
// For EXACT match negatives:
if (negativeMatchType === "EXACT") {
// For exact match, the negative must match the entire positive keyword exactly
return negativeText === positiveText;
}
return false;
}
/**
* Removes ad group-level negative keywords that conflict with
* the positive keywords in the same ad group.
*
* @param {Object} positiveKeywordsMap The map of positive keywords
* @param {Object} metrics The metrics object for tracking
* @return {number} Number of conflicts removed
*/
function removeAdGroupNegativeConflicts(positiveKeywordsMap, metrics) {
let conflictsRemoved = 0;
// We need to iterate through ad groups to access their negative keywords
// Only include ENABLED ad groups in ENABLED campaigns
const adGroupIterator = AdsApp.adGroups()
.withCondition("Status = ENABLED")
.withCondition("CampaignStatus = ENABLED")
.get();
while (adGroupIterator.hasNext()) {
const adGroup = adGroupIterator.next();
const adGroupId = adGroup.getId();
const campaignId = adGroup.getCampaign().getId();
const adGroupName = adGroup.getName();
const campaignName = adGroup.getCampaign().getName();
// Get negative keywords for this ad group
const negativeKeywordIterator = adGroup.negativeKeywords().get();
while (negativeKeywordIterator.hasNext()) {
const negativeKeyword = negativeKeywordIterator.next();
const negText = negativeKeyword.getText().toLowerCase();
const negMatchType = negativeKeyword.getMatchType();
// Check for conflicts within this ad group
let conflictFound = false;
let conflictReason = '';
// Check if this would have been flagged by the old algorithm
let wouldBeOldConflict = false;
if (positiveKeywordsMap.byId[campaignId] &&
positiveKeywordsMap.byId[campaignId].adGroups[adGroupId]) {
const adGroupKeywords = positiveKeywordsMap.byId[campaignId].adGroups[adGroupId].keywords;
// Check each positive keyword in this ad group for conflicts
for (const posText in adGroupKeywords) {
const posMatchType = adGroupKeywords[posText];
// Check if this would be flagged by the old algorithm
if (negMatchType === "PHRASE" && posText.includes(negText)) {
wouldBeOldConflict = true;
} else if (negMatchType === "BROAD") {
const negTerms = negText.replace(/^\+/, '').split(/\s+/);
const posTerms = posText.split(/\s+/);
if (negTerms.every(term => posTerms.includes(term.replace(/^\+/, '')))) {
wouldBeOldConflict = true;
}
}
// Check with our improved algorithm
if (hasKeywordConflict(negText, negMatchType, posText, posMatchType)) {
conflictFound = true;
conflictReason = `Conflicts with positive keyword '${posText}' (${posMatchType})`;
break;
}
}
}
// If it would have been flagged by old algorithm but not by new one, count as false positive avoided
if (wouldBeOldConflict && !conflictFound) {
metrics.falsePositivesAvoided++;
if (CONFIG.detailedLogging) {
Logger.log(`False positive avoided: '${negText}' (${negMatchType}) in Ad Group '${adGroupName}' would not actually block any keywords`);
}
}
if (conflictFound) {
metrics.adGroupConflictsFound++;
const logMessage = `Ad Group Conflict: '${negText}' (${negMatchType}) in Ad Group '${adGroupName}' (${adGroupId}) - ${conflictReason}`;
Logger.log(logMessage);
if (!CONFIG.dryRun) {
negativeKeyword.remove();
conflictsRemoved++;
Logger.log(` ✓ Removed`);
} else {
Logger.log(` ⚠ Would remove (dry run)`);
}
}
}
}
return conflictsRemoved;
}
/**
* Removes campaign-level negative keywords that conflict with
* any positive keyword in the same campaign.
*
* @param {Object} positiveKeywordsMap The map of positive keywords
* @param {Object} metrics The metrics object for tracking
* @return {number} Number of conflicts removed
*/
function removeCampaignNegativeConflicts(positiveKeywordsMap, metrics) {
let conflictsRemoved = 0;
// We need to iterate through campaigns to access their negative keywords
// Only include ENABLED campaigns
const campaignIterator = AdsApp.campaigns()
.withCondition("Status = ENABLED")
.get();
while (campaignIterator.hasNext()) {
const campaign = campaignIterator.next();
const campaignId = campaign.getId();
const campaignName = campaign.getName();
// Get negative keywords for this campaign
const negativeKeywordIterator = campaign.negativeKeywords().get();
while (negativeKeywordIterator.hasNext()) {
const negativeKeyword = negativeKeywordIterator.next();
const negText = negativeKeyword.getText().toLowerCase();
const negMatchType = negativeKeyword.getMatchType();
// Check for conflicts with any positive keyword in this campaign
let conflictFound = false;
let conflictDetails = '';
if (positiveKeywordsMap.byId[campaignId]) {
// Check each ad group in the campaign
for (const adGroupId in positiveKeywordsMap.byId[campaignId].adGroups) {
const adGroupData = positiveKeywordsMap.byId[campaignId].adGroups[adGroupId];
// Check each positive keyword in this ad group
for (const posText in adGroupData.keywords) {
const posMatchType = adGroupData.keywords[posText];
if (hasKeywordConflict(negText, negMatchType, posText, posMatchType)) {
conflictFound = true;
conflictDetails = `Conflicts with '${posText}' (${posMatchType}) in Ad Group '${adGroupData.name}'`;
break;
}
}
if (conflictFound) break;
}
}
if (conflictFound) {
metrics.campaignConflictsFound++;
const logMessage = `Campaign Conflict: '${negText}' (${negMatchType}) in Campaign '${campaignName}' (${campaignId}) - ${conflictDetails}`;
Logger.log(logMessage);
if (!CONFIG.dryRun) {
negativeKeyword.remove();
conflictsRemoved++;
Logger.log(` ✓ Removed`);
} else {
Logger.log(` ⚠ Would remove (dry run)`);
}
}
}
}
return conflictsRemoved;
}
/**
* Removes negative keywords from shared negative keyword lists that conflict
* with any positive keyword in the account.
*
* @param {Object} positiveKeywordsMap The map of positive keywords
* @param {Object} metrics The metrics object for tracking
* @return {number} Number of conflicts removed
*/
function removeSharedListNegativeConflicts(positiveKeywordsMap, metrics) {
let conflictsRemoved = 0;
// Use the correct function name for shared negative keyword lists
const sharedNegLists = AdsApp.negativeKeywordLists().get();
if (!sharedNegLists.hasNext()) {
Logger.log('No shared negative keyword lists found.');
return conflictsRemoved;
}
while (sharedNegLists.hasNext()) {
const negList = sharedNegLists.next();
const listName = negList.getName();
// Get campaigns that are actually using this shared negative keyword list
const campaignsUsingList = negList.campaigns().get();
const campaignsWithList = new Set();
// Build a set of campaign IDs that are using this list
while (campaignsUsingList.hasNext()) {
const campaign = campaignsUsingList.next();
campaignsWithList.add(campaign.getId());
}
if (campaignsWithList.size === 0) {
Logger.log(`Shared list "${listName}" is not assigned to any campaigns. Skipping.`);
continue;
}
Logger.log(`Checking shared list: ${listName} (assigned to ${campaignsWithList.size} campaigns)`);
// Get negative keywords for this list
const negKeywordIterator = negList.negativeKeywords().get();
while (negKeywordIterator.hasNext()) {
const negKeyword = negKeywordIterator.next();
const negText = negKeyword.getText().toLowerCase();
const negMatchType = negKeyword.getMatchType();
// Check against positive keywords, but only in campaigns that use this list
let conflictFound = false;
let conflictDetails = '';
let wouldBeOldConflict = false;
// First do a quick check against the Set of all keyword texts
const potentialDirectMatch = positiveKeywordsMap.allKeywords.has(negText);
// Check each positive keyword for conflicts, but only in campaigns using this list
for (const kwDetails of positiveKeywordsMap.keywordDetails) {
// Skip if this keyword's campaign is not using the shared list
if (!campaignsWithList.has(kwDetails.campaignId)) {
continue;
}
// Check if this would be flagged by the old algorithm
if (negMatchType === "PHRASE" && kwDetails.text.includes(negText)) {
wouldBeOldConflict = true;
} else if (negMatchType === "BROAD") {
const negTerms = negText.replace(/^\+/, '').split(/\s+/);
const posTerms = kwDetails.text.split(/\s+/);
if (negTerms.every(term => posTerms.includes(term.replace(/^\+/, '')))) {
wouldBeOldConflict = true;
}
}
// Check with our improved algorithm
if (hasKeywordConflict(negText, negMatchType, kwDetails.text, kwDetails.matchType)) {
conflictFound = true;
conflictDetails = `Conflicts with '${kwDetails.text}' (${kwDetails.matchType}) in Campaign '${kwDetails.campaignName}', Ad Group '${kwDetails.adGroupName}'`;
break;
}
}
// If it would have been flagged by old algorithm but not by new one, count as false positive avoided
if (wouldBeOldConflict && !conflictFound) {
metrics.falsePositivesAvoided++;
if (CONFIG.detailedLogging) {
Logger.log(`False positive avoided: '${negText}' (${negMatchType}) in shared list '${listName}' would not actually block any keywords`);
}
}
if (conflictFound) {
metrics.sharedListConflictsFound++;
const logMessage = `Shared List Conflict: '${negText}' (${negMatchType}) in List '${listName}' - ${conflictDetails}`;
Logger.log(logMessage);
if (!CONFIG.dryRun) {
negKeyword.remove();
conflictsRemoved++;
Logger.log(` ✓ Removed`);
} else {
Logger.log(` ⚠ Would remove (dry run)`);
}
}
}
}
return conflictsRemoved;
}
/**
* Generates a summary report of the conflicts found and actions taken.
*
* @param {Object} metrics The metrics object with conflict counts
* @return {string} A formatted report string
*/
function generateReport(metrics) {
const totalConflictsFound =
metrics.adGroupConflictsFound +
metrics.campaignConflictsFound +
metrics.sharedListConflictsFound;
const totalConflictsRemoved =
metrics.adGroupConflictsRemoved +
metrics.campaignConflictsRemoved +
metrics.sharedListConflictsRemoved;
// Determine if we found any conflicts
const noConflictsFound = totalConflictsFound === 0;
// Create a more visually appealing HTML email
let htmlReport = `
<html>
<head>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 800px; margin: 0 auto; }
.header { background-color: #4285f4; color: white; padding: 20px; text-align: center; }
.content { padding: 20px; }
.summary { background-color: #f8f9fa; padding: 15px; border-radius: 5px; margin-bottom: 20px; }
.details { margin-bottom: 20px; }
table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
th { background-color: #f2f2f2; }
.success { color: #0f9d58; font-weight: bold; }
.warning { color: #f4b400; }
.footer { font-size: 12px; color: #666; border-top: 1px solid #eee; padding-top: 10px; }
.no-conflicts { background-color: #e6f4ea; padding: 15px; border-radius: 5px; margin-bottom: 20px; border-left: 4px solid #0f9d58; }
.false-positives { background-color: #e8f5e9; padding: 15px; border-radius: 5px; margin-bottom: 20px; border-left: 4px solid #4caf50; }
</style>
</head>
<body>
<div class="header">
<h1>Google Ads Negative Keyword Conflict Report</h1>
<p>${new Date().toLocaleString()}</p>
</div>
<div class="content">
${noConflictsFound ? `
<div class="no-conflicts">
<h2><span class="success">✓ Great job!</span></h2>
<p>No negative keyword conflicts were found in your account. This means your negative keywords are properly configured and not blocking any of your positive keywords.</p>
<p>Your account is optimized for maximum visibility for the keywords you're actively bidding on.</p>
</div>
` : `
<div class="summary">
<h2>Summary</h2>
<p><strong>Date Range:</strong> ${CONFIG.dateRange || 'All Time'}</p>
<p><strong>Mode:</strong> ${CONFIG.dryRun ? 'DRY RUN (no changes made)' : 'ACTIVE (conflicts removed)'}</p>
<p><strong>Total Conflicts Found:</strong> ${totalConflictsFound}</p>
${!CONFIG.dryRun ? `<p><strong>Total Conflicts Removed:</strong> ${totalConflictsRemoved}</p>` : ''}
</div>
<div class="details">
<h2>Details</h2>
<table>
<tr>
<th>Level</th>
<th>Conflicts Found</th>
${!CONFIG.dryRun ? '<th>Conflicts Removed</th>' : ''}
</tr>
<tr>
<td>Ad Group Level</td>
<td>${metrics.adGroupConflictsFound}</td>
${!CONFIG.dryRun ? `<td>${metrics.adGroupConflictsRemoved}</td>` : ''}
</tr>
<tr>
<td>Campaign Level</td>
<td>${metrics.campaignConflictsFound}</td>
${!CONFIG.dryRun ? `<td>${metrics.campaignConflictsRemoved}</td>` : ''}
</tr>
<tr>
<td>Shared Lists</td>
<td>${metrics.sharedListConflictsFound}</td>
${!CONFIG.dryRun ? `<td>${metrics.sharedListConflictsRemoved}</td>` : ''}
</tr>
</table>
</div>
<div class="impact">
<h2>Impact Analysis</h2>
<p>Removing these negative keyword conflicts will help ensure your ads show for the keywords you're actively bidding on, potentially increasing your impression share and overall campaign performance.</p>
<p>By resolving these conflicts, you've eliminated situations where your ads were being blocked by your own negative keywords, which could lead to:</p>
<ul>
<li><strong>Increased visibility</strong> for your targeted keywords</li>
<li><strong>Better ad spend efficiency</strong> by ensuring budget goes to keywords you want to target</li>
<li><strong>Improved campaign consistency</strong> across your account</li>
</ul>
</div>
`}
${metrics.falsePositivesAvoided > 0 ? `
<div class="false-positives">
<h2>False Positives Avoided: ${metrics.falsePositivesAvoided}</h2>
<p>These are potential conflicts that would have been incorrectly flagged by simpler detection methods.</p>
<p>Examples include phrase match negatives like "mini" not actually blocking "mining" or "a c" not blocking "ammonia compressor".</p>
</div>
` : ''}
<div class="footer">
<p>This report was generated automatically by the Google Ads Negative Keyword Conflict Resolver script.</p>
<p>For questions or support, please contact your account manager.</p>
</div>
</div>
</body>
</html>
`;
// Also create a plain text version for the logs and for email clients that don't support HTML
let textReport = '\n' + '='.repeat(50) + '\n';
textReport += 'NEGATIVE KEYWORD CONFLICT RESOLUTION REPORT\n';
textReport += '='.repeat(50) + '\n\n';
if (noConflictsFound) {
textReport += 'GREAT JOB! NO CONFLICTS FOUND\n';
textReport += 'Your account is optimized with no negative keyword conflicts.\n';
textReport += 'This means your negative keywords are properly configured and not blocking any of your positive keywords.\n\n';
} else {
textReport += 'SUMMARY:\n';
textReport += '- Date Range: ' + (CONFIG.dateRange || 'All Time') + '\n';
textReport += '- Mode: ' + (CONFIG.dryRun ? 'DRY RUN (no changes made)' : 'ACTIVE (conflicts removed)') + '\n';
textReport += '- Total Conflicts Found: ' + totalConflictsFound + '\n';
if (!CONFIG.dryRun) {
textReport += '- Total Conflicts Removed: ' + totalConflictsRemoved + '\n';
}
textReport += '\nDETAILS:\n';
textReport += '- Ad Group Level: ' + metrics.adGroupConflictsFound + ' conflicts found';
if (!CONFIG.dryRun) {
textReport += ', ' + metrics.adGroupConflictsRemoved + ' removed';
}
textReport += '\n';
textReport += '- Campaign Level: ' + metrics.campaignConflictsFound + ' conflicts found';
if (!CONFIG.dryRun) {
textReport += ', ' + metrics.campaignConflictsRemoved + ' removed';
}
textReport += '\n';
textReport += '- Shared Lists: ' + metrics.sharedListConflictsFound + ' conflicts found';
if (!CONFIG.dryRun) {
textReport += ', ' + metrics.sharedListConflictsRemoved + ' removed';
}
textReport += '\n';
textReport += '\nIMPACT:\n';
textReport += '- Removing these conflicts will increase visibility for your targeted keywords\n';
textReport += '- Better ad spend efficiency by ensuring budget goes to keywords you want to target\n';
textReport += '- Improved campaign consistency across your account\n';
}
// Add false positives avoided to the report
if (metrics.falsePositivesAvoided > 0) {
textReport += `\nFALSE POSITIVES AVOIDED: ${metrics.falsePositivesAvoided}\n`;
textReport += 'These are potential conflicts that would have been incorrectly flagged by simpler detection methods.\n';
textReport += 'Examples include phrase match negatives like "mini" not actually blocking "mining" or "a c" not blocking "ammonia compressor".\n';
}
textReport += '\n' + '-'.repeat(50) + '\n';
textReport += 'Script executed on: ' + new Date().toLocaleString() + '\n';
textReport += '='.repeat(50) + '\n';
// For email, use the HTML version - only send once
if (CONFIG.sendEmail) {
MailApp.sendEmail({
to: CONFIG.emailAddress,
subject: noConflictsFound ? 'Google Ads: No Negative Keyword Conflicts Found ✓' : 'Google Ads Negative Keyword Conflict Report',
htmlBody: htmlReport,
body: textReport // Fallback plain text version
});
Logger.log("Email report sent to: " + CONFIG.emailAddress);
}
// For logs, return the text version
return textReport;
}
/**
* Validates the conflict detection algorithm against known examples.
* This helps ensure our conflict detection is accurate.
*/
function validateConflictDetection() {
const testCases = [
// Format: [negative text, negative match type, positive text, expected result, description]
["a 1", "PHRASE", "wsda 1.25", false, "False conflict: 'a 1' in 'wsda 1.25'"],
["r5 ra", "PHRASE", "busch r5 ra 0025", true, "Real conflict: 'r5 ra' in 'busch r5 ra 0025'"],
["ra 0255", "PHRASE", "busch r5 ra 0255 d", true, "Real conflict: 'ra 0255' in 'busch r5 ra 0255 d'"],
["a c", "PHRASE", "liquid ring ammonia compressor", false, "False conflict: 'a c' in 'ammonia compressor'"],
["medical", "BROAD", "medical liquid ring pump", true, "Real conflict: 'medical' in 'medical liquid ring pump'"],
["ed", "PHRASE", "edwards gsx750", false, "False conflict: 'ed' in 'edwards'"],
["oil", "PHRASE", "nash vectra oil seal package", true, "Real conflict: 'oil' in 'nash vectra oil seal package'"],
["mini", "PHRASE", "copper mining liquid ring pump", false, "False conflict: 'mini' in 'mining'"],
["kd", "PHRASE", "kinney kdp", false, "False conflict: 'kd' in 'kdp'"],
["small", "PHRASE", "small capacity liquid ring pump", true, "Real conflict: 'small' in 'small capacity liquid ring pump'"],
["chamber", "PHRASE", "vacuum chamber liquid ring", true, "Real conflict: 'chamber' in 'vacuum chamber liquid ring'"],
["ac", "PHRASE", "nash sc 6 vacuum pump", false, "False conflict: 'ac' in 'nash sc 6 vacuum pump'"],
["busch", "PHRASE", "busch dolphin la", true, "Real conflict: 'busch' in 'busch dolphin la'"],
["rotary vane", "PHRASE", "oil circulated rotary vane pump", true, "Real conflict: 'rotary vane' in 'oil circulated rotary vane pump'"],
["milking", "PHRASE", "milking system liquid ring", true, "Real conflict: 'milking' in 'milking system liquid ring'"],
["rietschle", "PHRASE", "elmo rietschle s", true, "Real conflict: 'rietschle' in 'elmo rietschle s'"]
];
Logger.log("Validating conflict detection algorithm...");
let passedTests = 0;
let failedTests = 0;
for (const test of testCases) {
const [negText, negMatchType, posText, expectedResult, description] = test;
const actualResult = hasKeywordConflict(negText, negMatchType, posText, "BROAD"); // Assuming positive is broad
if (actualResult === expectedResult) {
passedTests++;
if (CONFIG.detailedLogging) {
Logger.log(`✓ PASS: ${description}`);
}
} else {
failedTests++;
Logger.log(`✗ FAIL: ${description} - Expected ${expectedResult} but got ${actualResult}`);
}
}
Logger.log(`Validation complete: ${passedTests} passed, ${failedTests} failed`);
return failedTests === 0;
}