AdWords
1.7K members online now
1.7K members online now
Dive into advanced features like Remarketing, Flexible Bid Strategies, AdWords Editor, and AdWords Scripts
Guide Me
star_border
Reply

Adwords script for enabling and pausing campaigns based on certain criteria in XML feed

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hey guys,

 

I'm pretty new here. Currently what I'm looking for is an Adwords script that would help enable or disable campaigns based on specific criteria it searches within the XML feed.

 

For example;

* Adwords script to search through the XML feed and find specific information such as;

 

1. Client's name &  No. of jobs

2. If any client has 0 jobs, then Adwords script needs to pause all Adwords campaigns containing client's name with 0 jobs in XML feed.

or 

3. If any client has 1+ jobs, then Adwords script needs to enable all campaigns containing the client's name with 1+ jobs in XML feed.

 

what type of Adwords script should I use or start with? or do you have something similar that I could use for this approach?

 

Your help would be much appreciated

 

 

2 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Mefi F
March 2016

Re: Adwords script for enabling and pausing campaigns based on certain criteria in XML feed

Visitor ✭ ✭ ✭
# 9
Visitor ✭ ✭ ✭

Hi @Arik

 

It almost worked, but it didn't trigger any of the actions. So what I did is revised by firstly removing the hats <> within the "<XML URL Here>" as "XML URL Here" to prevent it from being an invalid argument.

 

First change:

// Load an XML file:
var xmlURL = "XML URL Here";
var xmlFile = UrlFetchApp.fetch(xmlURL);

 

Second change:

The next thing I changed was that I replaced the 'Name= to 'Name CONTAINS=, so that prevents it from searching for the exact name, but anything containing the "id".

('Name="| ' + id + ' |"') to
('Name CONTAINS="| ' + id + ' |"')
Here's the full script
************
function main() {

// Load an XML file:
var xmlURL = "XML URL Here";
var xmlFile = UrlFetchApp.fetch(xmlURL);

// Parse the XML file:
var document = XmlService.parse(xmlFile);
var root = document.getRootElement();

// Go through all children of <deepdive_pages>:
var entries = document.getRootElement().getChildren();

for (var i = 0; i < entries.length; i++) {
//for (var i = 0; i < 10; i++) {
var id = entries[i].getChild('company_root_id').getValue();
var jobs = entries[i].getChild('job_count').getValue();

// If company_root_id has 0 jobs
if (jobs == 0){
var campaignIterator = AdWordsApp.campaigns().withCondition('Name CONTAINS "| ' + id + ' |"').get(); // Find all campaigns with the company name
while (campaignIterator.hasNext()) { // Go over all campaings with company id in the name
var campaign = campaignIterator.next();
if (campaign.isEnabled()) { // If campaign is enables, pause it
campaign.pause();
Logger.log("campaign " + campaign.getName() + " was paused.");
}
}
}

// If company_root_id has MORE than 0 jobs
else {
var campaignIterator = AdWordsApp.campaigns().withCondition('Name CONTAINS "| ' + id + ' |"').get(); // Find all campaigns with the company name
while (campaignIterator.hasNext()) { // Go over all campaings with company id in the name
var campaign = campaignIterator.next();
if (campaign.isPaused()) { // If campaign is paused, enable it
campaign.enable();
Logger.log("campaign " + campaign.getName() + " was enabled.");
}
}
} // End If Else
}
}

 

Now the script fully works.

 

It's able to search for any campaigns that has an id: 150 within the name like "Hire Me | 150 | Geo | Broad" and pause or enable depending on the actions it requires.

 

Thank you so much @Arik. This script was very efficient, clean and straight forward. Hopefully this script will help someone else just as it helped me.

 

You're a champion!!!

 

 

View solution in original post

Re: Adwords script for enabling and pausing campaigns based on certain criteria in XML feed

Community Manager
# 2
Community Manager
Hi Mefi,

I think you may have better luck getting an answer on the AdWords Scripts Community here: https://groups.google.com/forum/#!forum/adwords-scripts

Good luck!
Cassie

Re: Adwords script for enabling and pausing campaigns based on certain criteria in XML feed

Top Contributor
# 3
Top Contributor

Hi @Mefi F did you get a response from the scripts group?  The coding you require would be relatively straightforward ...

 

Jon

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Adwords script for enabling and pausing campaigns based on certain criteria in XML feed

Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭
Hi @Jon_Gritton,

I haven't received any response from script group?

Would you have any examples that you could recommend me that I could try out?

Cheers,
M

Re: Adwords script for enabling and pausing campaigns based on certain criteria in XML feed

Top Contributor
# 5
Top Contributor

Hi @Mefi F I've no specific examples, but if you could post a short snippet of your XML data (with false data, of course, to protect privacy), I could provide you with some pointers for reading and acting upon that data.

 

Jon

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Adwords script for enabling and pausing campaigns based on certain criteria in XML feed

Visitor ✭ ✭ ✭
# 6
Visitor ✭ ✭ ✭

Hi @Jon_Gritton,

 

Here you go. I have given you a snippet of a dummy xml feed as requested.

 

What I need is a Adwords script that will do the following criteria and actions.

1. Extract: Data from XML.

2. Criteria: Anything containing the id from "company_root_id" & number of jobs from "job_count".

3. Action: If any "company_root_id" has a "job_count" of "0" pause the Adwords campaign containing the "company_root_id". The naming convention of the campaign would look like this "Hire Me Online | 150 | Broad", the "150" is the "company_root_id". If already paused please ignore.

4. Action: If any "company_root_id" has a "job_count" of "1" or more enable the Adwords campaign. If already enabled please ignore.

5. Schedule: run daily.

 

Here's the XML feed example:

 

<?xml version="1.0"?>

- <deepdive_pages>

   - <deepdive_page>

                <company_root_id>150</company_root_id>

                <name>Hire Me Online</name>

                <apply_hijack>Y</apply_hijack>

                <is_published>1</is_published>

                <is_latest>1</is_latest>

                <job_count>30</job_count>

   </deepdive_page>

 

I hope this all makes sense. Cheers!


@Jon_Gritton wrote:

Hi @Mefi F I've no specific examples, but if you could post a short snippet of your XML data (with false data, of course, to protect privacy), I could provide you with some pointers for reading and acting upon that data.

 

Jon






Re: Adwords script for enabling and pausing campaigns based on certain criteria in XML feed

Top Contributor
# 7
Top Contributor

Hi @Mefi F I'm having some problems parsing the XML cleanly, leave it with me and I'll get back to you.  Poke me in a week or so if I don't reply.

 

Jon

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits

Re: Adwords script for enabling and pausing campaigns based on certain criteria in XML feed

[ Edited ]
Explorer ✭ ☆ ☆
# 8
Explorer ✭ ☆ ☆

Hi Mefi F.


You can try the following:

Please note, these are my assumptions:
1. the xml structure is that  <deepdive_page> is the direct child of <deepdive_pages>

2. the names "company_root_id", "job_count" are fixed names in the xml file, and they are children of <deepdive_page>.

3. an XML file with more than 1 "<deepdive_page>" child (2 in this example) would look like this:

 

<?xml version="1.0"?>
   <deepdive_pages>
      <deepdive_page>
            <company_root_id>150</company_root_id>
            <name>Hire Me Online</name>
            <apply_hijack>Y</apply_hijack>
            <is_published>1</is_published>
            <is_latest>1</is_latest>
            <job_count>0</job_count>
      </deepdive_page>
      <deepdive_page>
            <company_root_id>140</company_root_id>
            <name>Get Me Calls</name>
            <apply_hijack>Y</apply_hijack>
            <is_published>1</is_published>
            <is_latest>1</is_latest>
            <job_count>4</job_count>
      </deepdive_page>
   </deepdive_pages>

 

If the assumptions match your actual xml, you can try this script:

 

function main() {
  
  // Load an XML file:
  var xmlURL = "<PLACE XML URL HERE>";
  var xmlFile = UrlFetchApp.fetch(xmlURL);
  
  // Parse the XML file:
  var document = XmlService.parse(xmlFile);
  var root = document.getRootElement();
  
  // Go through all children of <deepdive_pages>:
  var entries = document.getRootElement().getChildren();
  
  for (var i = 0; i < entries.length; i++) {
    var id = entries[i].getChild('company_root_id').getValue();
    var jobs = entries[i].getChild('job_count').getValue();
    
    // If company_root_id has 0 jobs 
    if (jobs == 0){
      var campaignIterator = AdWordsApp.campaigns().withCondition('Name="| ' + id + ' |"').get(); // Find all campaigns with the company name
      while (campaignIterator.hasNext()) { // Go over all campaings with company id in the name
        var campaign = campaignIterator.next();
        if (campaign.isEnabled()) { // If campaign is enables, pause it
          campaign.pause();
          Logger.log("campaign " + campaign.getName() + " was paused.");
        }
      }
    }
    
    // If company_root_id has MORE than 0 jobs 
    else {
      var campaignIterator = AdWordsApp.campaigns().withCondition('Name="| ' + id + ' |"').get(); // Find all campaigns with the company name
      while (campaignIterator.hasNext()) { // Go over all campaings with company id in the name
        var campaign = campaignIterator.next();
        if (campaign.isPaused()) { // If campaign is paused, enable it
          campaign.enable();
          Logger.log("campaign " + campaign.getName() + " was enabled.");
        }
      }
    }    // End If Else

  }
}



Let me know if it worked for you.

Marked as Best Answer.
Solution
Accepted by topic author Mefi F
March 2016

Re: Adwords script for enabling and pausing campaigns based on certain criteria in XML feed

Visitor ✭ ✭ ✭
# 9
Visitor ✭ ✭ ✭

Hi @Arik

 

It almost worked, but it didn't trigger any of the actions. So what I did is revised by firstly removing the hats <> within the "<XML URL Here>" as "XML URL Here" to prevent it from being an invalid argument.

 

First change:

// Load an XML file:
var xmlURL = "XML URL Here";
var xmlFile = UrlFetchApp.fetch(xmlURL);

 

Second change:

The next thing I changed was that I replaced the 'Name= to 'Name CONTAINS=, so that prevents it from searching for the exact name, but anything containing the "id".

('Name="| ' + id + ' |"') to
('Name CONTAINS="| ' + id + ' |"')
Here's the full script
************
function main() {

// Load an XML file:
var xmlURL = "XML URL Here";
var xmlFile = UrlFetchApp.fetch(xmlURL);

// Parse the XML file:
var document = XmlService.parse(xmlFile);
var root = document.getRootElement();

// Go through all children of <deepdive_pages>:
var entries = document.getRootElement().getChildren();

for (var i = 0; i < entries.length; i++) {
//for (var i = 0; i < 10; i++) {
var id = entries[i].getChild('company_root_id').getValue();
var jobs = entries[i].getChild('job_count').getValue();

// If company_root_id has 0 jobs
if (jobs == 0){
var campaignIterator = AdWordsApp.campaigns().withCondition('Name CONTAINS "| ' + id + ' |"').get(); // Find all campaigns with the company name
while (campaignIterator.hasNext()) { // Go over all campaings with company id in the name
var campaign = campaignIterator.next();
if (campaign.isEnabled()) { // If campaign is enables, pause it
campaign.pause();
Logger.log("campaign " + campaign.getName() + " was paused.");
}
}
}

// If company_root_id has MORE than 0 jobs
else {
var campaignIterator = AdWordsApp.campaigns().withCondition('Name CONTAINS "| ' + id + ' |"').get(); // Find all campaigns with the company name
while (campaignIterator.hasNext()) { // Go over all campaings with company id in the name
var campaign = campaignIterator.next();
if (campaign.isPaused()) { // If campaign is paused, enable it
campaign.enable();
Logger.log("campaign " + campaign.getName() + " was enabled.");
}
}
} // End If Else
}
}

 

Now the script fully works.

 

It's able to search for any campaigns that has an id: 150 within the name like "Hire Me | 150 | Geo | Broad" and pause or enable depending on the actions it requires.

 

Thank you so much @Arik. This script was very efficient, clean and straight forward. Hopefully this script will help someone else just as it helped me.

 

You're a champion!!!

 

 

Re: Adwords script for enabling and pausing campaigns based on certain criteria in XML feed

[ Edited ]
Explorer ✭ ☆ ☆
# 10
Explorer ✭ ☆ ☆

Hi @Mefi F.

Actually the original meaning was NOT to leave the <>, and indeed replace everything between the ' '. Sorry if it wasn't clear.
Second, regarding the "CONTAINS": You're right. When I tested it on an account of mine, I used exact names, so didn't pay attention to it.

Glad I could help out.