The Google Natural Language API helps you understand unstructured texts. You can pass a string (for example, a review, a social media post, or a transcription of an audio recording) to the Natural Language API, and it will define entities (for example, a person, places, products, events), mood (whether customers are happy with your brand or angry at it), and syntax (parts of speech).

Google Natural Language can parse strings in multiple languages, and it also has a REST API, so you can easily use it in your Google Apps\Google Ads script projects.

To get started, go to the API section in the Google Developer Console and enable the Natural Language API. Then create an API key that you will use to access the API from the script.

Here, for example, is the mood analysis function:

function analyzeSentiment(lang, str) {
 
    var api_key = '************************';
    var requestUrl = `https://language.googleapis.com/v1/documents:analyzeSentiment?key=${api_key}`;

    var data = {
        document: {
            language: lang,
            type: 'PLAIN_TEXT',
            content: str
        },
        encodingType: 'UTF8'
    };
    var options = {
        method: 'POST',
        contentType: 'application/json',
        payload: JSON.stringify(data)
    };

    var response = UrlFetchApp.fetch(requestUrl, options);
    var data = JSON.parse(response);

    return data;
}

Example response for this function:

{
    "documentSentiment": {
        "magnitude": 0.8,
        "score": 0.4
    },
    "language": "en-US",
    "sentences": [{
        "text": {
            "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ornare, mi mollis porttitor pretium, dui diam mattis lorem, sed imperdiet...",
            "beginOffset": 0
        },
        "sentiment": {
            "magnitude": 0.8,
            "score": 0.8
        }
    }]
}

You can also use this function to evaluate potential negative keywords in contextual advertising. For example, for the word “scam”, the following response will be returned:

{
    "documentSentiment": {
        "magnitude": 0.8,
        "score": -0.8
    },
    "language": "en",
    "sentences": [{
        "text": {
            "content": "scam",
            "beginOffset": 0
        },
        "sentiment": {
            "magnitude": 0.8,
            "score": -0.8
        }
    }]
}

You can create a script that analyzes new words that appear in search queries, and with a certain threshold of negative rating, it would recommend negative-colored words to be added as negative words.