Under development! Use with caution.
npm install googleads-node-lib
Start by following the instructions at https://developers.google.com/adwords/api/docs/guides/start. You will need client ID, a client secret, and an authorization code. You will get the client ID and the client secret from the Google Developers Console. To get an authorization code (filling in client_id and selecting account), open this URL in your browser:
https://accounts.google.com/o/oauth2/auth?client_id=<CLIENT_ID>&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fadwords&redirect_uri=urn:ietf:wg:oauth:2.0:oob&access_type=offline&approval_prompt=auto
Use this code to get tokens (filling in your Google AdWords credentials):
curl \
-d code=<AUTHORIZATION_CODE> \
-d client_id=<CLIENT_ID> \
-d client_secret=<CLIENT_SECRET> \
-d redirect_uri=urn:ietf:wg:oauth:2.0:oob \
-d grant_type=authorization_code https://accounts.google.com/o/oauth2/token
This yields something like:
{
"access_token" : <ACCESS_TOKEN>,
"token_type" : "Bearer",
"expires_in" : 3599,
"refresh_token" : <REFRESH_TOKEN>
}
Use the access_token
to make requests. The access_token
will expire. Use the refresh_token
to get a new access token. The refresh_token
will not expire. The refresh_token
is a stored credential.
This library's services get Google AdWords credentials from the following sources in priority order:
var AdWords = require('googleads-node-lib');
var Service = new AdWords.ManagedCustomerService({
ADWORDS_CLIENT_ID: 'your client id',
ADWORDS_CLIENT_CUSTOMER_ID: 'your client customer id',
ADWORDS_DEVELOPER_TOKEN: 'your developer token'
ADWORDS_REFRESH_TOKEN: 'your refresh token',
ADWORDS_SECRET: 'your secret',
ADWORDS_USER_AGENT: 'your user agent',
});
.env
file. An example .env
file is provided in .env-example
.Failure to provide credentials will cause the library to throw a configuration error.
Here are some examples. Documentation should improve, but in the mean time, the best places to look are the integration tests and the gulp tasks.
Setting up the AdGroupService:
var AdWords = require('googleads-node-lib');
var service = new AdWords.AdGroupService();
var clientCustomerId = 'the client customer ID you are interested in';
Getting AdGroups:
var selector = new AdWords.Selector.model({
fields: service.selectable,
ordering: [{field: 'Name', sortOrder: 'ASCENDING'}],
paging: {startIndex: 0, numberResults: 100}
});
service.get(clientCustomerId, selector, function(err, results) {
if (err) console.log(err);
else console.log(JSON.stringify(results, null, 2));
});
Setting up the AccountLabelService:
var AdWords = require('googleads-node-lib');
var service = new AdWords.AccountLabelService();
var clientCustomerId = 'the client customer ID you are interested in';
Getting account labels:
var selector = new AdWords.Selector.model({
fields: service.selectable,
ordering: [{field: 'Name', sortOrder: 'ASCENDING'}],
paging: {startIndex: 0, numberResults: 100}
});
service.get(clientCustomerId, selector, function(err, results) {
if (err) console.log(err);
else console.log(JSON.stringify(results, null, 2));
});
Adding an account label:
var operand = new service.Model({
name: 'the name of the account label'
});
service.mutateAdd(
clientCustomerId,
operand,
function(err, results) {
if (err) console.log(err);
else console.log(JSON.stringify(results, null, 2));
}
);
Removing an account label:
var operand = new service.Model({
id: 'the id of the account label'
});
service.mutateRemove(
clientCustomerId,
operand,
function(err, results) {
if (err) console.log(err);
else console.log(JSON.stringify(results, null, 2));
}
);
Changing the name of an account label:
var operand = new service.Model({
id: 'the id of the account label',
name: 'the new name of the account label'
});
service.mutateSet(
clientCustomerId,
operand,
function(err, results) {
if (err) console.log(err);
else console.log(JSON.stringify(results, null, 2));
}
);
Setting up the CampaignService:
var AdWords = require('googleads-node-lib');
var service = new AdWords.CampaignService();
var clientCustomerId = 'the client customer ID you are interested in';
Getting your campaigns:
var selector = new AdWords.Selector.model({
dateRange: {min: '19700101', max: '20380101'},
fields: service.selectable,
ordering: [{field: 'Name', sortOrder: 'ASCENDING'}],
paging: {startIndex: 0, numberResults: 100},
predicates: []
});
service.get(clientCustomerId, selector, function(err, results) {
if (err) console.log(err);
else console.log(JSON.stringify(results, null, 2));
});
Setting up the ManagedCustomerService:
var AdWords = require('googleads-node-lib');
var service = new AdWords.ManagedCustomerService();
var clientCustomerId = 'the client customer ID you are interested in';
Getting your managed customers:
var selector = new AdWords.Selector.model({
dateRange: {min: '19700101', max: '20380101'},
fields: service.selectable,
ordering: [{field: 'Name', sortOrder: 'ASCENDING'}],
paging: {startIndex: 0, numberResults: 100},
predicates: []
});
service.get(clientCustomerId, selector, function(err, results) {
if (err) console.log(err);
else console.log(JSON.stringify(results, null, 2));
});
Adding a managed customer:
var operand = new service.Model({
name: 'the name of the customer',
currencyCode: 'USD',
dateTimeZone: 'America/Chicago'
});
service.mutateAdd(
clientCustomerId,
operand,
function(err, results) {
if (err) console.log(err);
else console.log(JSON.stringify(results, null, 2));
}
);
Hiding a managed customer:
var operand = new service.ManagedCustomerLink({
clientCustomerId: clientCustomerId,
isHidden: true,
managerCustomerId: managerCustomerId
});
service.mutateLinkSet(
clientCustomerId,
operand,
function(err, results) {
if (err) console.log(err);
else console.log(JSON.stringify(results, null, 2));
cb(err);
}
);
CampaignPerformanceReport
AccountPerformanceReport
and AdGroupPerformanceReport
ReportDefinitionService
AccountLabelService
upgradeUrl
to AdGroupAdService
verbose
Paging
and DateRange
typesTrafficEstimatorService
TargetingIdeaService
ProductBiddingCategoryData
to ConstantDataService
ConstantDataService
mutateAdd
to BudgetService
findByCustomerId
to ManagedCustomerService
ManagedCustomer
AdGroupCriterionService
AdGroupAdService
AdGroupService
BiddingStrategyService
BudgetService
CampaignService
CampaignCriterionService
mutateRemove
for servicesmutateSet
for servicesAccountLabelService
mutateLink
for ManagedCustomerService
mutateAdd
for ManagedCustomerService