AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

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

Script to pause Adggroups based in information from a Merchant XML feed

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi Guys.

I have a merchant XML, and want to use the ID (the id is algo used in the adgroup name) and AVAILABILITY (In Stock, Out of Stock, Preorder) from the XML to pause Adgroups in my campaign.

 

I tried to modify the example using adgroups in place of campaigns, but I think the error is when the script try to read the XML.

In the lines bellow lines I receive the error: "TypeError: Cannot call method "getValue" of null. (line 16)"

 

var id = entries[i].getChild('g:id').getValue();
var availability = entries[i].getChild('g:availability').getValue();

 

My XML is like this:

<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<item>
<title>
<![CDATA[ Far Cry 4: Overrun ]]>
</title>
<link>https://www.nuuvem.com/item/far-cry-4-overrun</link>
<description>
<![CDATA[
Neste novo conteúdo para Far Cry 4, enquanto a guerra sem trégua por Kyrat continua, os jogadores assumirão o papel dos Rakshasa e do Caminho Dourado para manter locais que dão pontos no mapa da batalha.
]]>
</description>
<g:availability>in stock</g:availability>
<g:price currency="BRL">8.99</g:price>
<g:image_link>
http://dskhvldhwok3h.cloudfront.net/image/upload/t_boxshot_big/v1/products/557dbcb269702d0a9c490801/boxshots/ynhjodwlnmur0crkiaxp.jpg
</g:image_link>
<g:product_type>
<![CDATA[ Ação ]]>
</g:product_type>
<g:google_product_category>Software > Video Game Software > Computer Games</g:google_product_category>
<g:condition>new</g:condition>
<g:identifier_exists>FALSE</g:identifier_exists>
<g:id>2668</g:id>
</item>

My script now looks like this:

function main() {
 
 // Load an XML file:
 var xmlURL = "XML_MERCHANT_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('g:id').getValue();
 var availability = entries[i].getChild('g:availability').getValue();
 
 // If company_root_id has 0 jobs 
 if (availability == "out of stock"){
 var adGroupIterator = AdWordsApp.adGroups().withCondition('Name CONTAINS "| ' + id + ' |"').get(); // Find all campaigns with the company name
 while (adGroupIterator.hasNext()) { // Go over all campaings with company id in the name
 var adgroup = adGroupIterator.next();
 if (adgroup.isEnabled()) { // If campaign is enables, pause it
 adgroup.pause();
 Logger.log("adgroup " + adgroup.getName() + " was paused.");
}
 }
}
 
 // If company_root_id has MORE than 0 jobs 
 else {
 var adGroupIterator = AdWordsApp.adGroups().withCondition('Name CONTAINS "| ' + id + ' |"').get(); // Find all campaigns with the company name
 while (adGroupIterator.hasNext()) { // Go over all campaings with company id in the name
 var adgroup = adGroupIterator.next();
 if (adgroup.isPaused()) { // If campaign is paused, enable it
 adgroup.enable();
 Logger.log("adgroup " + adgroup.getName() + " was enabled.");
}
 }
   } // End If Else
 }
}

Many thanks for any help! Smiley Happy

1 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Diego G
February 2017

Re: Script to pause Adggroups based in information from a Merchant XML feed

Top Contributor
# 4
Top Contributor

Hi, Diego.

 

I did not know you did not define the namespace. Here's an adaptation of your code that fetches the id and availability correctly from my feed. My structure is a little different than yours and while I did not have time to upload your feed and test with it, I think it should work with your file as well. Provided, of course, that the XML ends in </channel></rss> (is well formed).

 

 

function main() {
 
 // Load an XML file:
 var xmlURL = "XML_MERCHANT_URL_HERE";
 var xmlFile = UrlFetchApp.fetch(xmlURL);
 
 // Parse the XML file:
 var document = XmlService.parse(xmlFile);
 var ns = XmlService.getNamespace("http://base.google.com/ns/1.0");   
 var rss = document.getRootElement().getChildren(); //root element is <rss>; it's children are actually only one, <channel>
 var entries = rss[0].getChildren('item'); //getting all 'item' children of the first rss element, <channel>
  
 // Go through all children of <deepdive_pages>:
 Logger.log(entries.length);

 for (var i = 0; i < entries.length; i++) {

 var id = entries[i].getChild('id',ns).getText();
   
 var availability = entries[i].getChild('availability',ns).getText();
   
//Logger.log("ID: "+id+", Availability: "+availability);
 
 // If company_root_id has 0 jobs 
 if (availability == "out of stock"){
 var adGroupIterator = AdWordsApp.adGroups().withCondition('Name CONTAINS "| ' + id + ' |"').get(); // Find all campaigns with the company name
 while (adGroupIterator.hasNext()) { // Go over all campaings with company id in the name
 var adgroup = adGroupIterator.next();
 if (adgroup.isEnabled()) { // If campaign is enables, pause it
 adgroup.pause();
 Logger.log("adgroup " + adgroup.getName() + " was paused.");
}
 }
}
 
 // If company_root_id has MORE than 0 jobs 
 else {
 var adGroupIterator = AdWordsApp.adGroups().withCondition('Name CONTAINS "| ' + id + ' |"').get(); // Find all campaigns with the company name
 while (adGroupIterator.hasNext()) { // Go over all campaings with company id in the name
 var adgroup = adGroupIterator.next();
 if (adgroup.isPaused()) { // If campaign is paused, enable it
 adgroup.enable();
 Logger.log("adgroup " + adgroup.getName() + " was enabled.");
}
 }
   } // End If Else
 }
}
Calin Sandici, AdWords Top Contributor | Find me on: Google+ | Twitter | LinkedIn | myBlog
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.

View solution in original post

Re: Script to pause Adggroups based in information from a Merchant XML feed

[ Edited ]
Top Contributor
# 2
Top Contributor

Hello, Diego.

 

Here's a sample of one of my scripts that does something similar. Nothice that I'm using getChildren instead of getChild (there are several children) and also, for elements from Google's namespace (those of the <g:id> form, or similar, prefixed by g: ) you need to specify the namespace as well.

 

Can't tell you exactly which is the whole theory behind it (my XML knowledge has faded a bit recently), but this is how I got the script to work.

 

Snippet follows:

 

var skus = items[i].getChildren('custom_label_3', ns);
var prices = items[i].getChildren('price', ns);
var titles = items[i].getChildren('title');

As you can see, g: price and g:custom_label_3 are called getChildren('price', ns) and getChildren('custom_label_3', ns). Title, on the other hand, which is not prefixed by g: in the XML, is called without the "ns" parameter.

 

Let me know if it works like this. I know I grew a few gray hairs before I managed to make my script work, some time ago.

 

Hope it helps.

 

Calin Sandici, AdWords Top Contributor | Find me on: Google+ | Twitter | LinkedIn | myBlog
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.

Re: Script to pause Adggroups based in information from a Merchant XML feed

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Hi Calin,

 

Thanks for your reply!!

 

I had a new error (my knowledge of XML always had been poor):

In my code I'm using:

 

 

var id = entries[i].getChildren('id', ns);
var availability = entries[i].getChildren('availability', ns);

 

But I have the error:

ReferenceError: "ns" is not defined. (line 16)

Any idea why?

 

Thanks!

Marked as Best Answer.
Solution
Accepted by topic author Diego G
February 2017

Re: Script to pause Adggroups based in information from a Merchant XML feed

Top Contributor
# 4
Top Contributor

Hi, Diego.

 

I did not know you did not define the namespace. Here's an adaptation of your code that fetches the id and availability correctly from my feed. My structure is a little different than yours and while I did not have time to upload your feed and test with it, I think it should work with your file as well. Provided, of course, that the XML ends in </channel></rss> (is well formed).

 

 

function main() {
 
 // Load an XML file:
 var xmlURL = "XML_MERCHANT_URL_HERE";
 var xmlFile = UrlFetchApp.fetch(xmlURL);
 
 // Parse the XML file:
 var document = XmlService.parse(xmlFile);
 var ns = XmlService.getNamespace("http://base.google.com/ns/1.0");   
 var rss = document.getRootElement().getChildren(); //root element is <rss>; it's children are actually only one, <channel>
 var entries = rss[0].getChildren('item'); //getting all 'item' children of the first rss element, <channel>
  
 // Go through all children of <deepdive_pages>:
 Logger.log(entries.length);

 for (var i = 0; i < entries.length; i++) {

 var id = entries[i].getChild('id',ns).getText();
   
 var availability = entries[i].getChild('availability',ns).getText();
   
//Logger.log("ID: "+id+", Availability: "+availability);
 
 // If company_root_id has 0 jobs 
 if (availability == "out of stock"){
 var adGroupIterator = AdWordsApp.adGroups().withCondition('Name CONTAINS "| ' + id + ' |"').get(); // Find all campaigns with the company name
 while (adGroupIterator.hasNext()) { // Go over all campaings with company id in the name
 var adgroup = adGroupIterator.next();
 if (adgroup.isEnabled()) { // If campaign is enables, pause it
 adgroup.pause();
 Logger.log("adgroup " + adgroup.getName() + " was paused.");
}
 }
}
 
 // If company_root_id has MORE than 0 jobs 
 else {
 var adGroupIterator = AdWordsApp.adGroups().withCondition('Name CONTAINS "| ' + id + ' |"').get(); // Find all campaigns with the company name
 while (adGroupIterator.hasNext()) { // Go over all campaings with company id in the name
 var adgroup = adGroupIterator.next();
 if (adgroup.isPaused()) { // If campaign is paused, enable it
 adgroup.enable();
 Logger.log("adgroup " + adgroup.getName() + " was enabled.");
}
 }
   } // End If Else
 }
}
Calin Sandici, AdWords Top Contributor | Find me on: Google+ | Twitter | LinkedIn | myBlog
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.

Re: Script to pause Adggroups based in information from a Merchant XML feed

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

Many thanks Calin.

 

It worked fine. I used your line :

Logger.log("ID: "+id+", Availability: "+availability);

 To check the log and they are OK.

 

With some modification in the script to pause, is working fine in my tests.

Script to pause Adggroups based in information from a Merchant XML feed

Top Contributor
# 6
Top Contributor

Thank God, Diego, 'cause I was running out of ideas Smiley Happy. I'm glad it works.

Calin Sandici, AdWords Top Contributor | Find me on: Google+ | Twitter | LinkedIn | myBlog
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.