What is the best way to identify a specific AdWords account while utilizing campaignSelector within AdWords scripts?

I have been working on a script that aims to achieve the following tasks:

  1. Go through all the accounts in an MCC and pick out the ones with 'SEM' in their name.
  2. Go through the campaigns in a selected account and choose those that meet specific conditions.
  3. Email a list of these selected campaigns to myself.

The issue I am facing pertains to connecting the account loop with the campaign loop.

Therefore, my question is; How can I specify an AdWords account when utilizing campaignSelector in AdWords scripts?

If I can specify the account for the campaign iteration (instead of the script defaulting to the current account), I can provide an array of my chosen accounts there.

Thank you.

Here is the script as it stands:

//This code is to be inserted in an MCC, it loops through accounts in the MCC based on certain criteria
//then for those selected accounts, it filters through the campaigns that meet specific criteria and adds
//these to a report (the report code has not been added yet)
//The challenge lies in getting the campaignSelector() function to look at the passed account from the accountIterator() function

function main() {

 var mccAccount = AdWordsApp.currentAccount();
var childAccounts = MccApp.accounts();


 function accountIterator() 
  {
   var accountSelector = MccApp.accounts()
   .withCondition("AccountDescriptiveName CONTAINS 'SEM'")
   .withCondition("Status = ENABLED");

   var accountIterator = accountSelector.get();

   while (accountIterator.hasNext())
   {
     var account = accountIterator.next();
     var accountName = account.getName();
     Logger.log(accountName);
     campaignSelector(accountName);  //This might need adjustment
                                     //We need to pass the account name to the campaignSelector function 
                                     //so that it looks at the campaigns in the specified account
   }
  }


 function campaignSelector () 
    {
    //SELECT campaigns we're interested in
    var account = AdWordsApp.currentAccount(); //Might need to use this?
    var campaignSelector = AdWordsApp.campaigns()
    .withCondition("CampaignName CONTAINS 'Shoop'")
    .withCondition("SearchExactMatchImpressionShare < 95")
    .forDateRange("LAST_7_DAYS")
    .withCondition("Status = ENABLED");

    //GET an iterator to list the selected campaigns
    var campaignIterator = campaignSelector.get();

    //ITERATE through all selected campaigns
    while (campaignIterator.hasNext()) 
     {
       var campaign = campaignIterator.next();
       //Include campaign and account info in a report – to be coded separately
    }
  }
}

Answer №1

If you are looking to select a specific account based on certain conditions, you can use the code snippet below before proceeding with your main code. I trust this will be beneficial for you.

var mccAccount = AdWordsApp.currentAccount();

while (accountIterator.hasNext()) {
  var account = accountIterator.next();
  if("condition to retrieve specific account"){
    // Choose the desired client account.
    MccApp.select(account);
  }
}

// Retrieve campaigns under the selected client account
var campaignIterator = AdWordsApp.campaigns().get();

Answer №2

Here is a potential solution to your question:

function initiate() {

  var currentAccount = AdWordsApp.currentAccount();
  var childAccounts = MccApp.accounts();

  var accountList = MccApp.accounts().get();
  while (accountList.hasNext())
  {
    var acc = accountList.next();
    selectCampaign(acc);
  }
}

function selectCampaign(acc) {
  MccApp.select(acc); // opening the acquired account from the previous function

  var accName = acc.getName();

  var campaignFilter = AdWordsApp.campaigns()
  .withCondition("CampaignName CONTAINS 'Shoop'")
  .withCondition("SearchExactMatchImpressionShare < 95")
  .forDateRange("LAST_7_DAYS")
  .withCondition("Status = ENABLED");

  var campaignList = campaignFilter.get();

  while (campaignList.hasNext()) {
    var campaign = campaignList.next();
    // Reporting
  }
}

The main issue in the code provided is the absence of the MccApp.select function.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Instructions on calling a function with AngularJS ng-click in a template rendered by a Grails controller

Is there a way to invoke a function using angularjs ng-click on a template that is rendered from a grails controller? I have tried, but the jQuery function call seems to work fine while the ng-click() function does not. What am I missing here? I'm rea ...

What is the process for including headers while establishing a connection to a websocket?

After configuring a websocket topic using Spring websocket on the server side, we implemented the client side with Stomp.js to subscribe to it. Everything was functioning correctly when connecting directly to the websocket service. However, we decided to i ...

Choosing HTML Elements for Audio Playback in HTML5: A Guide to Selecting Elements Without Using Div IDs

When creating an HTML5 player, I am transitioning "from using divs with id's" to "simple HTML elements". Former Scenario (functional) <audio src="track1.mp3" id="audio"></audio> <controls> <play id="play" style="display:none ...

In Angular, what is the best way to update the quantity of an item in a Firestore database?

Whenever I attempt to modify the quantity of an item in the cart, the quantity does not update in the firestore database. Instead, the console shows an error message: TypeError: Cannot read properties of undefined (reading 'indexOf'). It seems li ...

When jQuery is combined with extending Object.prototype, the result is an error message that states, "c.replace is not

In my open source project, I am utilizing jQuery 1.5 and the following snippet is included in my JavaScript code: /** * Object.isEmpty() * * @returns {Boolean} */ Object.prototype.isEmpty = function () { /** * @deprecated Since Javascript 1.8 ...

How to display JSON containing nested objects in AngularJS using the ng-repeat directive

Hey everyone, I have this JSON external file that I need help with: { "success":true, "errors":[ ], "objects":[ { "cod":"8211300", "descricao":"Serviços advocatícios" }, // more objects here... ] } In ...

Guide to building a react-redux application with a Node server as the backend

Hey there! I'm looking to build a react-redux app with a node server as the backend. Is it feasible for the node server to serve the react-redux app instead of having react-redux run on one port and node on another? Any suggestions on how to kick thi ...

The Ajax success function is failing to trigger after receiving a 200 network response

I have a basic HTML page that loads jquery 3.3.1 and an external script file called script.js. I am using a simple node http-server to serve the static page. The data.json file is also in the same folder as the HTML file. However, even though the network c ...

Serialization of forms consistently yields an empty string

In my view, I have a dropdown menu that triggers the insertion of a partial view into a designated div when an option is selected. The following code snippet demonstrates what the view looks like: <div class="container"> <div class="row"> ...

The term 'MapEditServiceConfig' is being incorrectly utilized as a value in this context, even though it is meant to refer to a type

Why am I receiving an error for MapEditServiceConfig, where it refers to a type? Also, what does MapEditServiceConfig {} represent as an interface, and what is the significance of these brackets? export interface MapEditServiceConfig extends AppCredenti ...

A guide on retrieving data from an API and displaying it using AngularJS

REACT $state.saveData= function(productfilter){ var url = CONFIG.apiUrl + '/product'; window.open(url); window.print(url); }; CSS <button onClick="saveData(productfilter)" type="button">Print</button> ...

Adjust the position of an image to move it closer to the top using CSS

I have a fiddle where I am trying to adjust the position of an image (keyboard) to match a specific design. https://i.sstatic.net/flqCH.png In my fiddle, the keyboard image is slightly lower than the desired position shown in the design. I am looking for ...

What is the best way to initialize a value asynchronously for React context API in the latest version of NextJS, version

Currently, I'm working on implementing the React context API in my NextJS e-commerce application to manage a user's shopping cart. The challenge I'm facing is how to retrieve the cart contents from MongoDB to initiate the cart context. This ...

Using the && operator for conditional rendering in a React return statement

How should I format my return statement in a class component when using the && operator in the condition like this: if (x === null && y === null) If the condition is met, display this HTML: <div className="col-12"> <SomeComponent /> < ...

Learn how to implement the PATCH method specifically for the scenario when the user clicks on the "forgot password"

After clicking on the forgot password link, I want to display the password change form. However, I'm facing issues with calling the API to update the user's password. Below are the files I'm utilizing to develop the API, trigger events, mana ...

I'm encountering an issue with my array in JavaScript while using // @ts-check in VS Code. Why am I receiving an error stating that property 'find' does not exist on my array? (typescript 2.7

** update console.log(Array.isArray(primaryNumberFemales)); // true and I export it with: export { primaryNumberFemales, }; ** end update I possess an array (which is indeed a type of object) that is structured in the following manner: const primar ...

Executing jQuery AJAX requests in a chain with interdependent tasks

I am struggling to grasp the concept of magic deferred objects with jQuery. Consider the following code snippet: function callWebService(uri, filter, callback) { var data = {}; if (filter && filter != '') data['$filter&apos ...

Is there a term in JavaScript that denotes an object that can be serialized into JSON format?

Can you help me find a term for basic objects that accentuates their simplicity? Particularly, objects that do not reference themselves and do not have any methods or bindings (i.e. JSON-serializable). The terms I am currently using are: "flat object" " ...

Utilizing local storage, store and access user's preferred layout options through a click function

Exploring the realm of localstorage for the first time, I am considering its use to store a specific ID or CLASS from my css file. This stored information would then be utilized to render a selected layout (grid/list) in the user's browser upon their ...

What is the procedure for obtaining a Connect server's host name and port number?

Just like the example shown in this Express-related question, I'm wondering if there is a way to programmatically retrieve the host name and port number of a running Connect server? ...