Ad

Push To This Workaround

- 1 answer

I'm trying to refactor a portion of a Google AdWords script I've written in order to not have to repeat an if statement that I'm hoping I can dynamically update via an instance of "this". However, I'm receiving a "Cannot find function push in object firstKeywords. (line 34)" error. When I simply insert "firstKeywords.push", instead of "this.keywordsArray.push" the script works. Was wondering if there's a way to have the "this" instance be considered an array or if there's another work around?

Error: this.keywordsArray.push

Non-working script:

var firstKeywords = [];
var secondKeywords = [];
var thirdKeywords = [];

function main() {
  function testKeywords(adgr, keywordsArray) {
  this.adgr = adgr;
  this.keywordsArray = keywordsArray;
}


testKeywords.prototype.move = function () {
    var campaignIterator = AdWordsApp.campaigns()
        .withCondition("Status = ENABLED")
        .withCondition("Name CONTAINS_IGNORE_CASE 'High'")
        .get();
    while (campaignIterator.hasNext()) {
      var campaign = campaignIterator.next();

  var adGroupIterator = campaign.adGroups()
        .withCondition("Name CONTAINS_IGNORE_CASE 'Blogs'")
        .get();
    while (adGroupIterator.hasNext()) {
      var adGroup = adGroupIterator.next();
      var adGroupName = adGroup.getName();
  var keywordIterator = adGroup.keywords()
        .withCondition("SystemServingStatus = RARELY_SERVED")
        .get();
    while (keywordIterator.hasNext()) {
      var keyword = keywordIterator.next();
      var keywordText = keyword.getText();
      var adgroupArray = this.keywordsArray;
        if (adGroupName === this.adgr) {
          this.keywordsArray.push(keywordText);
          keyword.pause();
        }
    }
    }
    }
}

var test01 = new testKeywords("General Music Blogs", "firstKeywords");
var test02 = new testKeywords("Hip Hop Music Blogs", "secondKeywords");
var test03 = new testKeywords("Indie Music Blogs", "thirdKeywords");

test01.move();
test02.move();
test03.move();
}

The below script works the correct way, but with the if statements repeated.

var firstKeywords = [];
var secondKeywords = [];
var thirdKeywords = [];

function main() {
  function testKeywords() {
  var campaignIterator = AdWordsApp.campaigns()
        .withCondition("Status = ENABLED")
        .withCondition("Name CONTAINS_IGNORE_CASE 'High'")
        .get();
    while (campaignIterator.hasNext()) {
      var campaign = campaignIterator.next();

  var adGroupIterator = campaign.adGroups()
        .withCondition("Name CONTAINS_IGNORE_CASE 'Blogs'")
        .get();
  while (adGroupIterator.hasNext()) {
      var adGroup = adGroupIterator.next();
      var adGroupName = adGroup.getName();
  var keywordIterator = adGroup.keywords()
        .withCondition("SystemServingStatus = RARELY_SERVED")
        .get();
    while (keywordIterator.hasNext()) {
      var keyword = keywordIterator.next();
      var keywordText = keyword.getText()
        if (adGroupName === 'General Music Blogs') {
          firstKeywords.push(keywordText);
          keyword.pause();
        }
        if (adGroupName === 'Hip Hop Music Blogs') {
          secondKeywords.push(keywordText);
          keyword.pause();
        }
        if (adGroupName === 'Indie Music Blogs') {
          thirdKeywords.push(keywordText);
          keyword.pause();
        }
       } 
      }
     }   
 }
}

Thanks in advance.

Ad

Answer

In your testKeyword you are passing string literals and not the object refs.

So change

var test01 = new testKeywords("General Music Blogs", "firstKeywords");
var test02 = new testKeywords("Hip Hop Music Blogs", "secondKeywords");
var test03 = new testKeywords("Indie Music Blogs", "thirdKeywords");

to

var test01 = new testKeywords("General Music Blogs", firstKeywords);
var test02 = new testKeywords("Hip Hop Music Blogs", secondKeywords);
var test03 = new testKeywords("Indie Music Blogs", thirdKeywords);
Ad
source: stackoverflow.com
Ad