Skip to content

Instantly share code, notes, and snippets.

@daniil4udo
Last active June 7, 2019 17:00
Show Gist options
  • Save daniil4udo/a93d00f0c6ca902634438a4aaf24c7f6 to your computer and use it in GitHub Desktop.
Save daniil4udo/a93d00f0c6ca902634438a4aaf24c7f6 to your computer and use it in GitHub Desktop.
Holding the position or setting to hold the desired position in AdWords

Holding the position or setting to hold the desired position in AdWords

Very often, when conducting contextual advertising, there is a question of choosing or keeping a particular position of an ad.

function main() {
    // anchor array
    // If one is just a shortcut type "Shortcut1".
    // If an array then ["Shortcut1", "Shortcut2", ..., "ShortcutN"]
    var LabelNames = ["ck"];
    // the maximum allowed price per click
    var maxPrice = 16;
    // the minimum allowed price
    var minPrice = 0.3;
    //the lower boundary of the baseband of AveragePosition
    var customLowerAveragePosition = 2;
    //top-level limit of the baseband of AveragePosition
    var customUpperAveragePosition = 2.5;
    //per cent for the baseband
    var customPercent = 0;
    //(%)a step for the percentages of other ranges
    var stepPercent = 5;
    //the least possible value is AveragePosition
    var minAveragePosition = 1;
    //the highest possible mean of AveragePosition
    var maxAveragePosition = 10;
    //diapason
    var diapason = 0.5;


    var tempPercent = customPercent;
    var down_max = customLowerAveragePosition;
    var down_min = down_max - diapason;

    var up_min = customUpperAveragePosition;
    var up_max = up_min + diapason;

    var to_up = maxAveragePosition - customUpperAveragePosition;

    var labelName = "";
    if (LabelNames.constructor === Array) {
        labelName = "LabelNames CONTAINS_ANY ['" + LabelNames.join("','") + "']";
    } else {
        labelName = "LabelNames = '" + LabelNames + "'";
    }
    Logger.log("Condition for labels: " + labelName);

    var selectedKeyword = 0;
    while (down_max > minAveragePosition) {
        Logger.log("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        tempPercent = tempPercent - stepPercent;
        var keywordIterator = AdWordsApp.keywords()
            .withCondition(labelName)
            .withCondition("AveragePosition < " + Number(down_max) + " AND AveragePosition > " + Number(down_min - 0.01))
            .withCondition("Impressions > 0")
            .forDateRange("YESTERDAY")
            .get();
        Logger.log(" AveragePosition < " + Number(down_max) + " AND AveragePosition > " + Number(down_min - 0.01));
        while (keywordIterator.hasNext()) {
            selectedKeyword = selectedKeyword + 1;
            Logger.log("   ---------------");
            var keyword = keywordIterator.next();
            Logger.log("   " + selectedKeyword + "   ----------- keyword: " + keyword.getText());
            Logger.log("   old price " + keyword.getMaxCpc());
            var tempPrice = keyword.getMaxCpc() + (keyword.getMaxCpc() * tempPercent) / 100;
            if (tempPrice > maxPrice) {
                keyword.setMaxCpc(maxPrice);
            } else if (tempPrice < minPrice) {
                keyword.setMaxCpc(minPrice);
            } else {
                keyword.setMaxCpc(tempPrice);
            }
            Logger.log("new price " + keyword.getMaxCpc());
            Logger.log("add percent " + tempPercent);
        }
        down_max = down_min - 0.01;
        down_min = down_max - diapason;
    }

    tempPercent = customPercent;

    while (up_min < maxAveragePosition) {
        Logger.log("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        tempPercent = tempPercent + stepPercent;
        var keywordIterator = AdWordsApp.keywords()
            .withCondition(labelName)
            .withCondition("AveragePosition < " + Number(up_max) + " AND AveragePosition > " + Number(up_min - 0.01))
            .withCondition("Impressions > 0")
            .forDateRange("YESTERDAY")
            .get();
        Logger.log(" AveragePosition < " + Number(up_max) + " AND AveragePosition > " + Number(up_min - 0.01));
        while (keywordIterator.hasNext()) {
            selectedKeyword = selectedKeyword + 1;
            Logger.log("   ---------------");
            var keyword = keywordIterator.next();
            Logger.log("   " + selectedKeyword + "   ------------------keyword " + keyword.getText());
            Logger.log("   old price " + keyword.getMaxCpc());
            var tempPrice = keyword.getMaxCpc() + (keyword.getMaxCpc() * tempPercent) / 100;
            if (tempPrice > maxPrice) {
                keyword.setMaxCpc(maxPrice);
            } else if (tempPrice < minPrice) {
                keyword.setMaxCpc(minPrice);
            } else {
                keyword.setMaxCpc(tempPrice);
            }
            Logger.log("   new price " + keyword.getMaxCpc());
            Logger.log("   add percent " + tempPercent);
        }
        up_min = up_max;
        up_max = up_max + diapason;
    }
}

Setting up the script

  • You need to set the shortcut to the keywords you want.

  • Specify the maximum and minimum bid within which the script will assign bids.

  • Select the desired range of positions in which your ad must hold.

  • Specify the period for which you want to take position information (YESTERDAY or TODAY point 2 times in the middle of the script)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment