使用脚本和XML Feed暂停Google Adwords中的广告组

时间:2017-02-16 12:31:05

标签: javascript xml google-apps-script google-adwords

我正在尝试使用XML(Google Merchant XML)中的字段AVAILABILITY暂停广告组的脚本。

我有商家XML,并希望使用来自XML的ID(广告组名称中使用的ID)和可用性(库存,缺货,预购)来暂停广告系列中的广告组。

我尝试修改示例I had found here,使用广告组代替广告系列,但我认为错误是脚本尝试读取XML时的错误。 在下面的行中我收到错误:“TypeError:无法调用方法”getValue“为null。(第16行)”

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

我的XML是这样的:

<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>

我的脚本现在看起来像这样:

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('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
    }
}

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

在这篇文章中,在Calin的帮助下:https://www.en.advertisercommunity.com/t5/Advanced-Features/Script-to-pause-Adggroups-based-in-information-from-a-Merchant/m-p/1024590#

我解决了我的问题。

为了正确阅读XML,我使用了:

// 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);

在我做之前,我已经宣布了XML命名空间并使用了getchild,但在这种情况下,我需要使用getChildren