npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@htdangkhoa/google-ads

v0.18.0

Published

Google Ads API client for Node.js

Downloads

498

Readme

Usage

Authentication

import { UserRefreshClient, JWT } from 'google-auth-library';

// for web application
const authClient = new UserRefreshClient({
  clientId: '<CLIENT_ID>',
  clientSecret: '<CLIENT_SECRET>',
  refreshToken: '<REFRESH_TOKEN>',
});

// or use JWT for service account
const authClient = new JWT({
  keyFile: '<KEY_FILE>',
  subject: '<GOOGLE_ADS_EMAIL>',
  scopes: ['https://www.googleapis.com/auth/adwords'],
});

Customer

import { Customer } from '@htdangkhoa/google-ads';

const service = new Customer({
  auth: authClient,
  developer_token: '<DEVELOPER_TOKEN>',
});

const { resource_names: customers } = await service.listAccessibleCustomers();

// ...

Search

import { GoogleAds } from '@htdangkhoa/google-ads';

const service = new GoogleAds(
  {
    auth: authClient,
    developer_token: '<DEVELOPER_TOKEN>',
  },
  {
    customer_id: '<CUSTOMER_ID>',
  },
);

const customerClients = await service.search({
  query: `
    SELECT
      customer_client.descriptive_name,
      customer_client.resource_name,
      customer_client.client_customer,
      customer_client.level,
      customer_client.manager
    FROM customer_client
  `,
});

// ...

Search stream

import { GoogleAds } from '@htdangkhoa/google-ads';

const service = new GoogleAds(
  {
    auth: authClient,
    developer_token: '<DEVELOPER_TOKEN>',
  }, {
    customer_id: '<MANAGER_ID>',
  }
);

const response = service
  .setCustomerId('<CUSTOMER_ID>') // you can switch customer id
  .setLoginCustomerId('<MANAGER_ID>')
  .searchStream({
    query: `
      SELECT
        campaign.id,
        campaign.name,
        campaign.status
      FROM campaign
    `,
  });

for await (const { results } of response) {
  // ...
}

Campaign

import { GoogleAds } from '@htdangkhoa/google-ads';

const service = new GoogleAds(
  {
    auth: authClient,
    developer_token: '<DEVELOPER_TOKEN>',
  },
  {
    customer_id: '<CUSTOMER_CLIENT_ID>',
    login_customer_id: '<CUSTOMER_ID>',
  },
);

const campaigns = await service.search({
  query: `
    SELECT
      campaign.id,
      campaign.name,
      campaign.status
    FROM campaign
  `,
});

// ...

Mutate

import { GoogleAds } from '@htdangkhoa/google-ads';

const service = new GoogleAds(
  {
    auth: authClient,
    developer_token: '<DEVELOPER_TOKEN>',
  },
  {
    customer_id: '<CUSTOMER_CLIENT_ID>',
    login_customer_id: '<CUSTOMER_ID>',
  },
);

const response = await service.mutate({
  mutate_operations: [
    {
      campaign_operation: {
        create: {
          // ...
        },
        update: {
          // ...
        },
        remove: '<CAMPAIGN_RESOURCE_NAME>',
      },
    },
  ],
  partial_failure: true,
});

// ...

Query Builder

import { QueryBuilder } from '@htdangkhoa/google-ads';

const query = new QueryBuilder()
  .select(
    'campaign.id',
    'campaign.name',
    'segments.device',
    'metrics.clicks',
  )
  .from('campaign')
  .where(
    {
      attribute: 'metrics.impressions',
      operator: Operators.GREATER_THAN,
      value: "0",
    },
    {
      attribute: 'segments.device',
      operator: Operators.EQUALS,
      value: "MOBILE",
    },
    {
      attribute: 'segments.date',
      operator: Operators.DURING,
      value: Functions.LAST_30_DAYS,
    },
  )
  .orderBy(
    {
      attribute: 'metrics.clicks',
      direction: Order.DESC,
    },
  )
  .limit(10)
  .build();

const response = await service.search({ query });

Logging

Requests are logged with a one line summary and the full request/response body and headers.

| Log type | Log name | Success level | Failure level | |----------|---------------------------------|---------------|---------------| | SUMMARY | Google::Ads::GoogleAds::Summary | INFO | WARN | | DETAIL | Google::Ads::GoogleAds::Detail | DEBUG | INFO |

Basic

import { GoogleAds } from '@htdangkhoa/google-ads';

const service = new GoogleAds(
  {
    auth: authClient,
    developer_token: '<DEVELOPER_TOKEN>',
    logging: true,
  },
  {
    customer_id: '<CUSTOMER_ID>',
  },
);

Specific Log Type (SUMMARY or DETAIL)

import { GoogleAds } from '@htdangkhoa/google-ads';

const service = new GoogleAds(
  {
    auth: authClient,
    developer_token: '<DEVELOPER_TOKEN>',
    logging: {
      summary: true,
      detail: false,
    },
  },
  {
    customer_id: '<CUSTOMER_ID>',
  },
);

gRPC Client Options

The ServiceOptions is extended from @grpc/grpc-js ClientOptions, so you can pass any options you want to the client.

import { GoogleAds } from '@htdangkhoa/google-ads';

const service = new GoogleAds(
  {
    auth: authClient,
    developer_token: '<DEVELOPER_TOKEN>',
    logging: {
      summary: true,
      detail: false,
    },
    interceptors: [
      // your interceptors
    ],
  },
  {
    customer_id: '<CUSTOMER_ID>',
  },
);

Interceptors

See more at Node.js gRPC Library and some examples here.

Development

Prerequisites

  • Protocol Buffer Compiler (protoc) version 3.0.0 or greater. The latest version can be downloaded from here
  • Node.js version 16 or greater (LTS recommended) and npm version 8 or greater. The latest version of Node.js can be downloaded from here

Building

  1. Install dependencies

    yarn install
  2. Pull in the new protos and compile them

    yarn generate <GOOGLE_ADS_API_VERSION>
    
    # example
    yarn generate v18
  3. Make sure the version number in the src folder is correct (it should match the version number you passed to the generate command)

  4. Run tests to make sure everything worked (you may need to update the version numbers here)

    yarn test
  5. Build the library

    yarn build
  6. Make a pull request, get it approved and merged into main

License

The code in this project is released under the MIT License.