Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. /**
  2. *
  3. * AdWords Script for checking the contents of landing pages.
  4. * Goes to the final URL of keywords or ads, then searches the source code for
  5. * user defined strings.
  6. *
  7. * Version: 1.0
  8. * Google AdWords Script maintained by brainlabsdigital.com
  9. *
  10. **/
  11.  
  12. function main() {
  13.  
  14. // nume proiect Playground - eg. webdgital.ro
  15. const accountName = "sports.woomie.ro";
  16.  
  17. // titlu task
  18. const errorName = "eroare 404";
  19.  
  20. var messagesToCheckFor = ["Pagina pe care o cautati nu exista, a fost mutata sau a fost stearsa."];
  21. // What out of stock messages appear on the source code of your landing pages?
  22. // Enter like ["Out of stock", "<em>0 available</em>"]
  23.  
  24. var trimAtQuestionMark = true;
  25. // Do you want to remove all parameters which occur after the '?' character?
  26. // Enter true or false
  27.  
  28. var type = "keywords";
  29. // Choose "keywords" or "ads"
  30. // Are your final URLs at the keyword or ad level?
  31.  
  32. var recipients = ["tasks-ppc@webdigital.ro"];
  33. // If set, these addresses will be emailed with a list of any bad URLs.
  34. // Enter like ["a@b.com"] or ["a@b.com","c@d.com","e@g.co.uk"]
  35. // Leave as [] to skip.
  36.  
  37.  
  38. // Optional filtering options
  39. // Enter like ["hey", "jude"]
  40. // Leave as [] to skip
  41. var containsArray = ["WD"];
  42. // If set, only campaigns whose names contain these phrases will be checked
  43.  
  44. var excludesArray = [];
  45. // If set, campaigns whose names contain any of these phrases will be ignored.
  46.  
  47. var labelArray = [];
  48. // If set, only keywords / ads with these labels will be checked
  49. // Case sensitive.
  50.  
  51.  
  52. // Status options
  53. // Choose from ["ENABLED"] if you only want enabled entities
  54. // ["PAUSED"] if you only want paused entities
  55. // ["ENABLED","PAUSED"] if you want enabled and paused entities
  56. var campaignStatus = ["ENABLED"];
  57. // The status of the campaigns
  58.  
  59. var adGroupStatus = ["ENABLED"];
  60. // The status of the ad groups
  61.  
  62. var status = ["ENABLED"];
  63. // The status of the keywords / ads
  64.  
  65.  
  66. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  67. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  68. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  69. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  70. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  71. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
  72.  
  73.  
  74.  
  75. var urls = [];
  76. var bad_urls = [];
  77. var urlFetchOptions = {muteHttpExceptions: true};
  78. var countEntities = 0;
  79.  
  80.  
  81. var conditions = [];
  82. if (containsArray.length > 0) {
  83. conditions.push(" where the campaign name contains " + containsArray.join(", "));
  84. }
  85. if (excludesArray.length > 0) {
  86. conditions.push(" where the campaign name excludes " + excludesArray.join(", "));
  87. }
  88. if (labelArray.length > 0) {
  89. conditions.push(" where the " + type + " are labelled " + labelArray.join(", "));
  90. }
  91.  
  92. if (containsArray.length === 0) {
  93. containsArray.push("");
  94. }
  95.  
  96. for(var i = 0; i < containsArray.length; i++){
  97. var string = iteratorConstructor(type, containsArray[i], excludesArray, labelArray, status, campaignStatus, adGroupStatus);
  98. eval(string);
  99. countEntities += iterator.totalNumEntities();
  100. excludesArray.push(containsArray[i]);
  101. while(iterator.hasNext()){
  102. var object = iterator.next();
  103. var url = object.urls().getFinalUrl();
  104.  
  105. if(url == null || url == undefined){
  106. url = object.getDestinationUrl();
  107. }
  108.  
  109. if(url !== null && url !== undefined){
  110. if(trimAtQuestionMark){
  111. url = url.split('?')[0];
  112. }
  113. if(urls.indexOf(url) === -1) {
  114. urls.push(url);
  115. }
  116. }
  117. }
  118. }
  119.  
  120. if (countEntities == 0) {
  121. throw "No " + type + " found" + conditions.join("; and");
  122. }
  123. Logger.log(countEntities + " " + type + " found" + conditions.join("; and"));
  124. Logger.log(urls.length + " unique URLs to check.");
  125.  
  126. for(var x in urls){
  127. var response = UrlFetchApp.fetch(urls[x],urlFetchOptions);
  128. var code = response.getContentText();
  129. for(var y = 0; y < messagesToCheckFor.length; y++){
  130. var message = messagesToCheckFor[y];
  131. if(code.indexOf(message) !== -1){
  132. bad_urls.push(urls[x]);
  133. break;
  134. }
  135. }
  136. }
  137.  
  138. if (bad_urls.length === 0) {
  139. Logger.log("No bad URLs found.");
  140. } else {
  141. Logger.log(bad_urls.length + " found:");
  142. Logger.log(bad_urls.join("\n"));
  143. }
  144.  
  145. if(recipients.length > 0 && bad_urls.length > 0){
  146. var name = AdWordsApp.currentAccount().getName();
  147. var subject = "Task 6, " + accountName + ", " + errorName;
  148. var body = 'The following URLs were found to have one of the following phrases in their web page source code. \n\nPhrases:\n"' + messagesToCheckFor.join('",\n"') + '"\n\nURLs:\n';
  149. body += bad_urls.join("\n");
  150. MailApp.sendEmail(recipients.join(","),subject,body);
  151. Logger.log("Email sent to " + recipients.join(", "));
  152. }
  153.  
  154. function iteratorConstructor(type, containsString, excludesArray, labelArray, status, campaignStatus, adGroupStatus){
  155.  
  156. var string = "var iterator = AdWordsApp."+type+"()";
  157. if (containsString != "") {
  158. string = string + ".withCondition('CampaignName CONTAINS_IGNORE_CASE " + '"' + containsString + '"' + "')";
  159. }
  160. for(var i = 0; i < excludesArray.length; i++){
  161. string = string + ".withCondition('CampaignName DOES_NOT_CONTAIN_IGNORE_CASE " + '"' + excludesArray[i] + '"' + "')";
  162. }
  163. if(labelArray.length > 0){
  164. string = string + ".withCondition('LabelNames CONTAINS_ANY " + '["' + labelArray.join('","') + '"]' + "')";
  165. }
  166.  
  167. string = string + ".withCondition('Status IN [" + status.join(",") + "]')";
  168. string = string + ".withCondition('CampaignStatus IN [" + campaignStatus.join(",") + "]')";
  169. string = string + ".withCondition('AdGroupStatus IN [" + adGroupStatus.join(",") + "]')";
  170. string = string + ".orderBy('Cost DESC').forDateRange('LAST_30_DAYS')";
  171. string = string + ".withLimit(50000)";
  172.  
  173. string = string + ".get();"
  174.  
  175. return string;
  176.  
  177. }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement