Skrypt Google Ads: The worst day

W poprzednim poście opisałem skrypt sprawdzający, czy nasze konta odnotowały rekordowo dobry wynik pod względem konwersji w ciągu ostatnich trzydziestu dni.

Poniższe rozwiązanie jest w pewnym sensie jego kontynuacją.

Skrypt ten weryfikuje, czy któreś z naszych kont uzyskało w dniu poprzednim najsłabszy rezultat pod względem liczby konwersji w ciągu ostatnich dziewięćdziesięciu dni.

Wersja ta bada negatywne efekty z tą różnicą, że sprawdza dłuższy okres (dziewięćdziesiąt dni zamiast trzydziestu).

Warto być na bieżąco zarówno z negatywnymi wynikami na naszych kontach (co jest ważne), jak i z pozytywnymi (co jest najważniejsze).


Uwagi: jest to skrypt w wersji MCC
Harmonogram: raz dziennie

Konfiguracja:
1. Utwórz etykietę i oznacz nią konta, które ma sprawdzać skrypt
2. Wprowadź treść etykiety do zmiennej LABELNAME
3. Wprowadź swój e-mail do zmiennej EMAIL


Po więcej na temat Skryptów zapraszam na grupę FB o automatyzacji:
https://www.facebook.com/groups/skrypty.google.ads


/*
Copyright 2019 Krzysztof Bycina, www.LiveAds.pl
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//Configuration
var EMAIL = 'ENTER YOUR EMAIL HERE';
var LABELNAME = "ENTER YOUR LABEL HERE";
//End of the configuration

var badDayAccounts = [];
var _90daysData = {
    date: [],
    conversions: []
};

var todaysDate = new Date();
todaysDate = new Date(todaysDate.getTime() + todaysDate.getTimezoneOffset() * 60000);
todaysDate = formatDate(todaysDate);

var threeMonthsAgo = new Date();
threeMonthsAgo = new Date(threeMonthsAgo.getTime() + threeMonthsAgo.getTimezoneOffset() * 60000);
threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
threeMonthsAgo = formatDate(threeMonthsAgo);

var theDate = threeMonthsAgo + "," + todaysDate;

function main() {
    validateEmail(EMAIL);
    var accountSelector = AdsManagerApp.accounts()
        .withCondition("LabelNames CONTAINS '" + LABELNAME + "'");
    var accountIterator = accountSelector.get();
    while (accountIterator.hasNext()) {
        var account = accountIterator.next();
        AdsManagerApp.select(account);
        checkLastThreeMonths();
    }
    if (badDayAccounts[0]) {
        sendEmails();
    } else {
        Logger.log("The script didn't find anything.");
    }
}


function checkLastThreeMonths() {
    var iterator = -1;
    _90daysData.conversions = [];
    _90daysData.date = [];
    var report = AdWordsApp.report("SELECT Date, Conversions " +
        "FROM ACCOUNT_PERFORMANCE_REPORT " +
        "DURING " + theDate + " ");
    var rows = report.rows();
    while (rows.hasNext()) {
        iterator++
        var row = rows.next();
        _90daysData.conversions[iterator] = row['Conversions'];
        _90daysData.date[iterator] = row['Date'];
    }
    var minValue = Math.min.apply(null, _90daysData.conversions);
    if (yesterdayConv() < minValue) {
        Logger.log(AdsApp.currentAccount().getName() + " had: " + minValue + " conversions yesterday, and that's the worst result in the last three months!")
        badDayAccounts.push(AdsApp.currentAccount().getName() + " had: " + minValue + " conversions yesterday, and that's the worst result in the last three months!")
    }
}

function arrayMin(array) {
    var min = array[0];
    for (var i = 0; i < array.length; i++) {
        if (min < array[i]) {
            min = min;
        } else if (min > array[i]) {
            min = array[i + 1];
        } else if (min == array[i]) {
            min = min;
        }
    }
    return min;
};

function yesterdayConv() {
    var stats = AdsApp.currentAccount().getStatsFor("YESTERDAY");
    return stats.getConversions();
}

function formatDate(oldFormat) {
    var day = oldFormat.getDate();
    day = (day > 9) ? day : "0" + day;
    var month = oldFormat.getMonth() + 1;
    month = (month > 9) ? month : "0" + month;
    var year = oldFormat.getFullYear();
    return "" + year + "" + month + "" + day;
}

function sendEmails() {
    MailApp.sendEmail(EMAIL,
        "You had the worst day.",
        "Hi, \n\nThose accounts aren't doing well: \n\n" + badDayAccounts.join("\n"));
}

function validateEmail(email) {
    var key = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    if (key.test(email) === false) {
        throw new Error('You must enter a valid email address to run the script.')
    }
}

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *