【问题标题】:Use Script and XML Feed to pause Adgroups in Google Adwords使用脚本和 XML Feed 暂停 Google Adwords 中的广告组
【发布时间】:2017-02-21 21:35:30
【问题描述】:

我正在尝试编写一个脚本来暂停基于 XML(Google Merchant XML)中的 AVAILABILITY 字段的广告组。

我有一个商家 XML,并且想使用 XML 中的 ID(该 ID 是在广告组名称中使用的算法)和 AVAILABILITY(有货、缺货、预购)来暂停我的广告系列中的广告组。

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

非常感谢您的帮助!

【问题讨论】:

    标签: javascript xml google-apps-script google-ads-api


    【解决方案1】:

    在这篇文章中得到了 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多