Google Ads Script That Adds Youtube exclusions based on criteria

106 views
Skip to first unread message

John

unread,
Sep 26, 2024, 8:28:46 PM9/26/24
to Google Ads API and AdWords API Forum
Hello,

Is it possible to have google automatically add YouTube placement exclusions based on certain criteria in the channel names?

I am trying to make a script that excludes channels with characters in their title that do not match latin or germanic.

I am also trying to have it filter our children's keywords.

It keeps breaking with an error like this:

Ga: 'ad_group_id' is not a valid field in AD_PERFORMANCE_REPORT. Please check your spelling and casing. at main (Code:22:27)
I have tried various placement group fields from google's API doc.

Here is my code currently:

function main() {
    const keywords = ['minecraft', 'skibidi', 'cocomelon', 'peppa pig', 'roblox'];
    const channels = [];
    const dateRangeStart = getDateNDaysAgo(30);
    const dateRangeEnd = getFormattedDate(new Date());

    // Query for YouTube channel placements over the last 30 days
    const query = `
        SELECT
            ad_group_id,
            ad_group_name,
            creative_id,
            creative_name,
            placement_url
        FROM
            AD_PERFORMANCE_REPORT
        WHERE
            placement_type = 'YOUTUBE_CHANNEL'
            AND segments.date >= '${dateRangeStart}'
            AND segments.date <= '${dateRangeEnd}'`;

    const report = AdsApp.report(query);
    const rows = report.rows();

    while (rows.hasNext()) {
        const row = rows.next();
        const channelName = row.creative_name;
        const placementUrl = row.placement_url;

        // Check if the channel name contains non-Latin or non-Germanic characters
        if (containsNonLatinCharacters(channelName) || containsKeywords(channelName, keywords)) {
            channels.push({
                name: channelName,
                url: placementUrl
            });
        }
    }

    // Output the results
    if (channels.length > 0) {
        Logger.log('YouTube Channels Meeting Criteria:');
        channels.forEach(channel => {
            Logger.log(`Name: ${channel.name}, URL: ${channel.url}`);
        });
    } else {
        Logger.log('No YouTube channels found matching the criteria.');
    }
}

// Function to get the date N days ago in YYYY-MM-DD format
function getDateNDaysAgo(n) {
    const date = new Date();
    date.setDate(date.getDate() - n);
    return getFormattedDate(date);
}

// Function to format a date in YYYY-MM-DD format
function getFormattedDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
}

// Function to check if a string contains non-Latin or non-Germanic characters
function containsNonLatinCharacters(str) {
    // Regular expression for non-Latin characters
    const nonLatinRegex = /[^\u0000-\u007F]+/;
    return nonLatinRegex.test(str);
}

// Function to check if the channel name contains any specified keywords
function containsKeywords(str, keywords) {
    return keywords.some(keyword => str.toLowerCase().includes(keyword.toLowerCase()));
}



Google Ads API Forum Advisor

unread,
Sep 27, 2024, 3:59:27 AM9/27/24
to bois...@gmail.com, adwor...@googlegroups.com

Hi,

Thank you for reaching out to the Google Ads API support team.

Upon checking, it appears that your concern is more on Google Ads Scripts rather than anything specific to the Google Ads API. With that, I would recommend you to post your query in the Google Ads Scripts Forum for further assistance.

Please note that our team can only assist with technical queries or concerns related to the Google Ads API.

 
This message is in relation to case "ref:!00D1U01174p.!5004Q02vGavt:ref" (ADR-00269789)

Thanks,
 
Google Logo Google Ads API Team


Reply all
Reply to author
Forward
0 new messages