Skip to content

Instantly share code, notes, and snippets.

@rveitch
Created July 1, 2021 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rveitch/2315edec5b9b24a6322853f05eeb3901 to your computer and use it in GitHub Desktop.
Save rveitch/2315edec5b9b24a6322853f05eeb3901 to your computer and use it in GitHub Desktop.
Google Ads Node Generate Keyword Ideas Exampke
const {
GoogleAdsClient,
GenerateKeywordIdeasRequest,
KeywordSeed,
} = require('google-ads-node'); // v2.0.4
const { StringValue } = require('google-protobuf/google/protobuf/wrappers_pb'); // v3.14.0
(async () => {
// Client
const oGoogleAdsClient = new GoogleAdsClient({
client_id: process.env.GOOGLE_ADS_CLIENT_ID,
client_secret: process.env.GOOGLE_ADS_CLIENT_SECRET,
developer_token: process.env.GOOGLE_ADS_DEVELOPER_TOKEN,
refresh_token: process.env.GOOGLE_ADS_REFRESH_TOKEN,
parseResults: true,
});
const service = oGoogleAdsClient.getService('KeywordPlanIdeaService');
// Keyword Seeds
const keyword = new StringValue();
keyword.setValue('content marketing');
const keywordSeed = new KeywordSeed();
keywordSeed.addKeywords(keyword);
// Request
const request = new GenerateKeywordIdeasRequest();
request.setCustomerId(process.env.GOOGLE_ADS_CUSTOMER_ACCOUNT_ID);
request.setPageSize(10);
request.setKeywordSeed(keywordSeed);
const response = await new Promise((resolve, reject) => {
service.generateKeywordIdeas(request, (err, res) => {
if (err) reject(err);
resolve(res);
});
});
const { resultsList } = response || {};
console.log(JSON.stringify(resultsList, null, 0));
})();
@sghsgm99
Copy link

sghsgm99 commented Feb 25, 2023

Hi
How can I implement this function using google-ads-api?
https://www.npmjs.com/package/google-ads-api

@maulikkanani80
Copy link

maulikkanani80 commented Mar 22, 2023

import { GoogleAdsApi, services } from "google-ads-api";

// Make sure to pass in valid authentication!
const client = new GoogleAdsApi({
  client_id: "<CLIENT_ID>",
  client_secret: "<CLIENT_SECRET>",
  developer_token: "<DEVELOPER_TOKEN>",
});

async function main() {
  const customer = client.Customer({
    customer_id: "<CUSTOMER_ACCOUNT_ID>",
    refresh_token: "<REFRESH_TOKEN>",
  });

  const keywordSeed = new services.KeywordSeed({ keywords: ["Some Keyword"] });

  const generateKeywordIdeaResponse =
    await customer.keywordPlanIdeas.generateKeywordIdeas({
      customer_id: customer.credentials.customer_id,
      page_size: 10,
      keyword_seed: keywordSeed,
    });

  return generateKeywordIdeaResponse;
}

main();

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