Skrypt Google Ads: Geo Location Monitor

Kierowanie geograficzne to jedno z najważniejszych ustawień kampanii Google Ads. Serwując reklamy w nieodpowiednim kraju, możemy całkowicie zmarnować nasz budżet.

Geo Location Monitor to skrypt Google Ads, który sprawdza, czy nasze kampanie są widoczne we właściwym kraju. Jeśli pojawią się również w innych lokalizacjach, to otrzymamy e-mail z informacją na temat kosztów, które w związku z tym ponieśliśmy, liczbą konwersji (o ile takie będą) i listą kampanii do sprawdzenia.

Sytuacja taka występuje najczęściej, gdy zostawimy włączoną opcję kierowania:
„Docieraj do osób znajdujących się w wybranych lokalizacjach lub zainteresowanych nimi” — ustawienie to w niektórych przypadkach może generować spore koszty i wyświetlać reklamy w krajach, w których np. nie prowadzimy sprzedaży.

Inną przyczyną błędnego ustawienia lokalizacji jest usunięcie domyślnego kraju, np. podczas edycji kampanii w zewnętrznych programach typu Excel.
W tej sytuacji system Google zacznie wyświetlać nasze reklamy we wszystkich krajach, co spowoduje bardzo szybkie wyczerpanie budżetu.

Harmonogram: raz dziennie

Konfiguracja:
1. Wprowadź swój e-mail do zmiennej EMAIL
2. W razie potrzeby zmień kraj docelowy (domyślnie jest Polska)


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 EMAILS = ['ENTER YOUR EMAIL HERE'];
var YOUR_COUNTRY = "Poland";
//End of the configuration

//Declare variables
var iterator = -1;
var nCountry = [];
var nCost = [];
var nConversions = [];
var nConversionsValue = [];
var nCampaign = [];
var totalConversions;
var totalCost;

function main() {
    validateEmail(EMAILS[0]);
    fetchTheData();
    cleanTheData();
    if (totalCost > 0) {
        sendEmails();
    }
}

function fetchTheData() {
    var report = AdWordsApp.report("SELECT CampaignName, IsTargetingLocation, LocationType, CountryCriteriaId, Clicks, Impressions, Cost, Conversions, ConversionValue " +
        "FROM GEO_PERFORMANCE_REPORT " +
        "WHERE IsTargetingLocation IN [false, true] AND  LocationType = LOCATION_OF_PRESENCE " +
        "DURING LAST_30_DAYS");

    var rows = report.rows();
    while (rows.hasNext()) {
        iterator++
        var row = rows.next();

        //Check then spend outside of the target country
        if (row['CountryCriteriaId'] !== YOUR_COUNTRY) {
            nCountry[iterator] = row['CountryCriteriaId'];
            nCost[iterator] = parseFloat(row['Cost']);
            nCampaign[iterator] = row['CampaignName'];
            nConversionsValue[iterator] = parseFloat(row['ConversionValue']);
            nConversions[iterator] = parseFloat(row['Conversions']);
        }
    }
}
//Clean the metrics
function cleanTheData() {
    totalConversions = sumStats(nConversions);
    totalCost = sumStats(nCost);
    totalCost = totalCost.toFixed(2);
    nCountry = cleanTheArray(nCountry);
    nCountry = nCountry.filter(function(item, i, ar) {
        return ar.indexOf(item) === i;
    });
    nCampaign = nCampaign.filter(function(item, i, ar) {
        return ar.indexOf(item) === i;
    });
}
//Remove the empty spaces from the arrays
function cleanTheArray(currentArray) {
    var newArray = new Array();
    for (var i = 0; i < currentArray.length; i++) {
        if (currentArray[i]) {
            newArray.push(currentArray[i]);
        }
    }
    return newArray;
}

//Sum the elements
function sumStats(sum) {
    var total = 0;
    for (var i = 0; i < sum.length; i++) {
        if (sum[i]) {
            total += sum[i];
        }
    }
    return total;
}

//Validate the email
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 eneter a valid email address to run the script!');
    }
}

function sendEmails() {
    if (totalConversions) {
        var cpa = totalCost / totalConversions;
        cpa = cpa.toFixed(2);
        Logger.log("In the last 30 days you spend: " + totalCost + " outside of your target country. Which brought you: " + totalConversions + " conversions & CPA: " + cpa);
        MailApp.sendEmail(EMAILS.join(','),
            'Your ads are showing in new countries.',
            'Hi, \n\nIn the last 30 days you spend: ' + totalCost + ' outside of your target country. Which brought you: ' + totalConversions + ' conversions & CPA: ' + cpa +
            '\n\nYour ads were visible in these countries: \n\n' + nCountry.join("\n") +
            '\n\nCheck these campaigns: \n\n' + nCampaign.join("\n"));
    } else {
        Logger.log("In the last 30 days you spend: " + totalCost + " outside of your target country but there was no conversion.");
        MailApp.sendEmail(EMAILS.join(','),
            'Your ads are showing in new countries.',
            'Hi, \n\nIn the last 30 days you spend: ' + totalCost + ' outside of your target country but there was no conversion.' +
            '\n\nYour ads were visible in these countries: \n\n' + nCountry.join("\n") +
            '\n\nCheck these campaigns: \n\n' + nCampaign.join("\n"));
    }
}

Dodaj komentarz

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