管理標籤的指令碼範例

下列各節顯示針對標籤執行各種動作的指令碼範例。

為帳戶新增標籤

若要使用標籤,請先將其新增至您的帳戶。 您最多可以為帳戶新增 100,000 個標籤。 若要新增標籤,請使用 createLabel 方法。 該方法採用標籤的名稱、描述和背景顏色作為輸入。

名稱為必要項目,但描述和色彩為選用。 名稱區分大小寫,不能超過 80 個字元,且必須在帳戶內是唯一的。 雖然色彩是可選的,但如果您未指定,服務會為您隨機選擇色彩。 您可以使用 3 位元組十六進位數字指定色彩,格式 #RRGGBB (例如 CB0400) 或 16 種已知色彩名稱的其中之一,例如紅、黃和水。 如果您指定色彩,則必須指定描述,但它可以是空字串。

下列範例顯示呼叫方法的 createLabel 三種方式。

function main() {

    AdsApp.createLabel("foo");
    AdsApp.createLabel("bar", "a descriptive description", "aqua");
    AdsApp.createLabel("foo foo", "", "#FF0000");

}

列出帳戶的標籤

若要取得帳戶中的標籤清單,請使用 labels 方法。 您可以使用標準字串運算子(例如 =、CONTAINS 和 STARTS_WITH)依標籤名稱來篩選清單。

function main() {

    // Gets all the labels in the account
    var accountLabels = AdsApp.labels()
        .get();  
    
    Logger.log(`selector returned ${accountLabels.totalNumEntities()} labels that matched the selector's conditions`);

    // Gets all the labels in the account that contain foo in the name
    accountLabels = AdsApp.labels()
        .withCondition("Name CONTAINS 'foo'")
        .get();
    
    Logger.log(`selector returned ${accountLabels.totalNumEntities()} labels that matched the selector's conditions`);

    // Gets the first 15 labels in the account
    accountLabels = AdsApp.labels()
        .withLimit(15)
        .get();
    
    Logger.log(`selector returned ${accountLabels.totalNumEntities()} labels that matched the selector's conditions`);

    // Gets the first 15 labels in the account that contain foo in the name
    accountLabels = AdsApp.labels()
        .withCondition("Name CONTAINS 'foo'")
        .withLimit(15)
        .get();

    Logger.log(`selector returned ${accountLabels.totalNumEntities()} labels that matched the selector's conditions`);

    while (accountLabels.hasNext()){
        var label = accountLabels.next();
    }

}

將標籤套用至關鍵字

只有當您將標籤套用至關鍵字等實體時,標籤才有用。 若要將標籤套用至關鍵字,請使用關鍵字的 applyLabel 方法。 一個關鍵字最多可以套用 50 個標籤。 此方法會採用標籤名稱做為輸入。 標籤名稱區分大小寫、必須存在於帳戶中,且在關鍵字中必須是唯一的。

下列範例顯示如何將標籤新增至關鍵字、列出關鍵字的標籤,以及從關鍵字移除標籤。

function main() {

    // Get a keyword to add labels to
    var keyword = AdsApp.keywords().withIds(["12345"]).get().next();

    // Print the keyword's existing labels
    var labels = keyword.labels().get();
    printLabels(labels);

    // Add labels to the keyword
    keyword.applyLabel("foo");
    keyword.applyLabel("foo foo");
    keyword.applyLabel("bar");

    // Print the new list of labels
    labels = keyword.labels().get();
    printLabels(labels);

    // Remove labels from the keyword
    keyword.removeLabel("foo");
    keyword.removeLabel("bar");

    // Print the new list of labels
    labels = keyword.labels().get();
    printLabels(labels);

}

function printLabels(labels) {
    Logger.log(`keyword has ${labels.totalNumEntities()} labels`);
    
    while (labels.hasNext()){
        var label = labels.next();

        Logger.log(`name: ${label.getName()}
            description: ${label.getDescription()}
            color: ${label.getColor()}\n\n`);
    }
}

取得包含任何指定標籤的關鍵字

若要取得包含特定標籤名稱的關鍵字,請使用 KeywordSelector 物件的 withCondition () 方法。 使用 LabelNames 欄名稱來依標籤名稱進行篩選。 您可以指定一或多個名稱的陣列。 名稱區分大小寫、必須完全相符,且必須存在於帳戶中。 您可以套用下列運算子:

  • CONTAINS_ALL — 如果關鍵字包含所有指定的標籤,則選取關鍵字
  • CONTAINS_ANY — 如果關鍵字包含任何指定的標籤,則選取關鍵字
  • CONTAINS_NONE - 如果關鍵字不包含任何指定的標籤,則選取關鍵字

請注意,如果帳戶中沒有標籤,選取器會失敗並傳回錯誤。

function main() {

    var keywords = AdsApp.keywords()
        .withCondition('LabelNames CONTAINS_ANY ["foo", "bar"]')
        .get();
    
    while (keywords.hasNext()){
        var keyword = keywords.next();

        var labels = keyword.labels().get();

        while (labels.hasNext()) {
            var label = labels.next();
        }
    }

}