google adwords api + getting all keywords - google-ads-api

I'm using the google adwords api, I can retrieve all campaigns, group ads, ads,
but I have no idea on how to retrieve keywords related to an "group ads".
In the google adwords interface, when we select a group ads, we have two tabs, one for ads related to that group ads, and the second for keywords.
but programatily, right now I can only retrieve ads.
I'm using PHP, if some one knew how to do that in php or others programming languages or even a soap call.

To get the details of all the keywords of an adgroup you need the following to get the details of all the keywords.
require_once dirname(dirname(__FILE__)) . '/init.php';
// Enter parameters required by the code example.
$adGroupId = 'Enter your adgroup id';
/**
* Runs the example.
* #param AdWordsUser $user the user to run the example with
* #param string $adGroupId the id of the parent ad group
*/
function GetKeywordsExample(AdWordsUser $user, $adGroupId) {
// Get the service, which loads the required classes.
$adGroupCriterionService =
$user->GetService('AdGroupCriterionService', ADWORDS_VERSION);
// Create selector.
$selector = new Selector();
$selector->fields = array('KeywordText', 'KeywordMatchType', 'Id');
$selector->ordering[] = new OrderBy('KeywordText', 'ASCENDING');
// Create predicates.
$selector->predicates[] = new Predicate('AdGroupId', 'IN', array($adGroupId));
$selector->predicates[] =
new Predicate('CriteriaType', 'IN', array('KEYWORD'));
// Create paging controls.
$selector->paging = new Paging(0, AdWordsConstants::RECOMMENDED_PAGE_SIZE);
do {
// Make the get request.
$page = $adGroupCriterionService->get($selector);
// Display results.
if (isset($page->entries)) {
foreach ($page->entries as $adGroupCriterion) {
printf("Keyword with text '%s', match type '%s', and ID '%s' was "
. "found.\n", $adGroupCriterion->criterion->text,
$adGroupCriterion->criterion->matchType,
$adGroupCriterion->criterion->id);
}
} else {
print "No keywords were found.\n";
}
// Advance the paging index.
$selector->paging->startIndex += AdWordsConstants::RECOMMENDED_PAGE_SIZE;
} while ($page->totalNumEntries > $selector->paging->startIndex);
}
// Don't run the example if the file is being included.
if (__FILE__ != realpath($_SERVER['PHP_SELF'])) {
return;
}
try {
// Get AdWordsUser from credentials in "../auth.ini"
// relative to the AdWordsUser.php file's directory.
$user = new AdWordsUser();
// Log every SOAP XML request and response.
$user->LogAll();
// Run the example.
GetKeywordsExample($user, $adGroupId);
} catch (Exception $e) {
printf("An error has occurred: %s\n", $e->getMessage());
}

In the Adwords API Keywords are dubbed as AdGroup Criteria. You can add or retrieve the keywords for a certain AdGroup by using the AdGroupCriterionService.
If you're using the PHP client library of the Adwords API check out GetAllAdGroupCriteria.php in the example files. (don't forget to enter the AdGroupId you want to get the keywords for first)

Related

how to exclude multiple zip code from adword campaign via script?

I am facing issue in google AdWords.
I want to exclude zip code from all campaign via a script, script applied on account for all campaign
function main() {
excludeLocationTarget();
}
function excludeLocationTarget() {
var campaignIterator = AdsApp.campaigns()
.withCondition('Name = "US-WooCommerce"')
.get();
if (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
// Exclude Tennessee, United States (location id = 21175) See
// https://developers.google.com/adwords/api/docs/appendix/geotargeting
// for list of all supported geo codes.
// You could pass either the location code, or a TargetedLocation or
// ExcludedLocation object from an existing campaign.
var tennessee_id =['21175','21176'];
for(val in tennessee_id){
Logger.log(tennessee_id[val]);
campaign.excludeLocation(tennessee_id[val]);
}
}
}
7/31/2019 11:45:44 PM 21175
7/31/2019 11:45:44 PM Invalid argument: id. Should be of type: number (file Code.gs, line 19)
excludeLocation method expects an integer argument and you're using string. that's why excludeLocation fails.
try
var tennessee_id =[21175,21176]; // numerical values instead of strings
for(val in tennessee_id){
Logger.log(tennessee_id[val]);
campaign.excludeLocation(tennessee_id[val]);
}
or campaign.excludeLocation(parseInt(tennessee_id[val]));

How to use sheet ID in Google Sheets API?

Google Sheets document can contain some sheets. First is default and '0'. Generally for any sheet there is address like this:
https://docs.google.com/spreadsheets/d/(spreadsheetId)/edit#gid=(sheetId)
with both spreadsheetId and sheetId.
But in API documentation there is no mention of how to use sheetId. I can only read and edit default sheet for given spreadsheetId.
If in request from code presented in exemplary link I added sheetId property I got error:
{
message: 'Invalid JSON payload received. Unknown name "sheetId": Cannot bind query parameter. Field \'sheetId\' could not be found in request message.',
domain: 'global',
reason: 'badRequest'
}
How to get access to other sheets than default in Google Sheets API and read or update fields in them?
Sheet name is the easiest way to access a specific sheet. As written here, range parameter can include sheet names like,
Sheet1!A1
If you must use a sheet id instead of sheet name, You can use any of the alternate end points which uses dataFilter, like spreadsheets.values.batchUpdateByDataFilter instead of spreadsheets.values.batchUpdate. You can then use sheetId in request body at data.dataFilter.gridRange.sheetId. An example of using such a filter with sheetId is provided by another answer here by ztrat4dkyle.
However, developer metadata is the preferred method of permanently associating objects(sheets/ranges/columns) to variables, where user modifications are expected on such objects.
Essentially we need to use dataFilters to target a specific sheet by ID.
#TheMaster pointed me in the right direction but I found the answers confusing so I just want to share my working example for Node.js.
Here's how to get the value of cell B2 from a sheet that has ID 0123456789
const getValueFromCellB2 = async () => {
const SPREADSHEET_ID = 'INSERT_SPREADSHEET_ID';
const SHEET_ID = 0123456789;
// TODO: replace above values with real IDs.
const google = await googleConnection();
const sheetData = await google.spreadsheets.values
.batchGetByDataFilter({
spreadsheetId: SPREADSHEET_ID,
resource: {
dataFilters: [
{
gridRange: {
sheetId: SHEET_ID,
startRowIndex: 1,
endRowIndex: 2,
startColumnIndex: 1,
endColumnIndex: 2,
},
},
],
},
})
.then((res) => res.data.valueRanges[0].valueRange.values);
return sheetData[0][0];
}
// There are many ways to auth with Google... Here's one:
const googleConnection = async () => {
const auth = await google.auth.getClient({
keyFilename: path.join(__dirname, '../../secrets.json'),
scopes: 'https://www.googleapis.com/auth/spreadsheets',
});
return google.sheets({version: 'v4', auth});
}
To simply read data we're using batchGetByDataFilter where dataFilters is an array of separate filter objects. The gridRange filter (one of many) allows us to specify a sheetId and range of cells to return.
Here is my working example for "rename sheet in spreadsheet by sheetId" function.
You can use other methods from Google Spreadsheets API Docs in the same way. Hope it will be helpful for somebody
<?php
function getClient() //standard auth function for google sheets API
{
$clientConfigPath = __DIR__ . '/google_credentials/client_secret.json';
$client = new Google_Client();
$client->setApplicationName('Google Sheets API PHP Quickstart');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAuthConfig($clientConfigPath);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = (__DIR__ . '/google_credentials/credentials.json');
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if (!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
function renameSheet(string $sheetId, string $newTitle, string $spreadsheetId)
{
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Sheets($client);
$requests = [
new Google_Service_Sheets_Request([
'updateSheetProperties' => [
'properties' => [
'sheetId' => $sheetId,
'title' => $newTitle,
],
'fields' => 'title'
]
])
];
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => $requests
]);
return $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);
}
UPDATE
If you want to get sheet title by sheetId, you can use following function
function getSpreadsheetInfo($spreadsheetId)
{
$client = getClient();
$service = new Google_Service_Sheets($client);
$response = $service->spreadsheets->get($spreadsheetId);
return $response;
}
function getSheets($spreadsheetId)
{
$spreadsheet_info = getSpreadsheetInfo($spreadsheetId);
$sheets_info = [];
foreach ($spreadsheet_info as $item) {
$sheet_id = $item['properties']['sheetId'];
$sheet_title = $item['properties']['title'];
$sheets_info[$sheet_id] = $sheet_title;
}
return $sheets_info;
}
$sheets_info_array = getSheets($YOUR_SPREADSHEET_ID_HERE);
$sheets_info_array will be equal
array (
"sheet_id1(int)" => 'sheet_title1',
"sheet_id2(int)" => 'sheet_title3',
)
so you can get $your_sheet_id's title as $sheets_info_array[$your_sheet_id]
The initial blank empty tab that is always present when a new Google Sheet is created always has sheetId 0 assigned to it.
Subsequently created sheetIds are randomized ten digit numbers. Only the first tab has sheetId 0. Even if you rename a sheet, it's ID remains constant. IDs are never reused - they remain unique within a given sheet.
Using the Google Drive API, access to a Google Sheet is instantiated using the sheet's Google Drive file ID.
Once you have instantiated access to the particular Google Sheet file, you can then reference each tab within the sheet tab and manipulate information, format, etc within a tab of the sheet, by using the 'sheetId' nomenclature.
Here is a PHP example of renaming a Google Sheet's tab name using sheetId 0.
<?php
/*
* Google Sheets API V4 / Drive API V3, rename existing sheet tab example
*
*/
$fileID = '/* pass your Google Sheet Google Drive file ID here */';
$client = new Google_Client();
$client->useApplicationDefaultCredentials(); // the JSON service account key location as defined in $_SERVER
$client->setApplicationName('API Name');
$client->addScope(Google_Service_Drive::DRIVE);
$client->setAccessType('offline');
$client->setSubject('API Instance Subject');
$sheet = new Google_Service_Sheets($client);
$sheetList = $sheet->spreadsheets->get($fileID);
/*
* iterate through all Google Sheet tabs in this sheet
*/
$homeFlag = FALSE;
foreach($sheetList->getSheets() as $sheetRecord) {
/*
* if match, save $sheetTabID from Google Sheet tab
*/
if ($sheetRecord['properties']['sheetId'] == 0) {
$sheetTabID = $sheetRecord['properties']['sheetId'];
$sheetTabTitle = $sheetRecord['properties']['title'];
$homeFlag = TRUE;
}
}
/*
* if $homeFlag is TRUE, you found your desired tab, so rename tab in Google Sheet
*/
if ($homeFlag) {
$newTabName = 'NotTabZero';
$sheetRenameTab = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array('requests' => array('updateSheetProperties' => array('properties' => array('sheetId' => $sheetTabID, 'title' => $newTabName), 'fields' => 'title'))));
$sheetResult = $sheet->spreadsheets->batchUpdate($sheetID,$sheetRenameTab);
}
?>
More simplier answer is to use the A1 Notation to get what sheet and rows you want
const res = await sheets.spreadsheets.values.get({
spreadsheetId: "placeholder_id_value",
range: "Sheet2!A:A", # This will grab all data out of sheet 2 from column A
})
reference

accessing Twitter API from Google Apps Script

I'm trying to read in a Google sheet my Twitter timeline.
I've copied the following code reported in the GAS documentation about twitter authentication (omitting step 2 since I'm not using the code inside a UI):
function getTwitterService() {
// Create a new service with the given name. The name will be used when
// persisting the authorized token, so ensure it is unique within the
// scope of the property store.
return OAuth1.createService('twitter')
// Set the endpoint URLs.
.setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
.setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
.setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
// Set the consumer key and secret.
.setConsumerKey('mykey')
.setConsumerSecret('mysecret')
// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties());
}
function authCallback(request) {
var twitterService = getTwitterService();
var isAuthorized = twitterService.handleCallback(request);
if (isAuthorized) {
return Logger.log('Success! You can close this tab.');
} else {
return Logger.log('Denied. You can close this tab');
}
}
function makeRequest() {
var twitterService = getTwitterService();
var response = twitterService.fetch('https://api.twitter.com/1.1/statuses/user_timeline.json');
Logger.log(response);
}
but I obtain the message error: Service not authorized. (row 292, file "Service", project "OAuth1").
What's wrong?
I needed to add the following line the first time I execute makeRequest:
var authorizationUrl = twitterService.authorize();
Logger.log(authorizationUrl);
Then, open the url read from the log and authorize the app.
After that, all works fine.

Whats happens with GET Video Adwords Campaigns

recently i started a project to get campaigns from google adwords api and make analytics reports about that info.
I have that issue:
I launch this piece of code to get all campaigns:
public function testGetCampaigns()
{
$user = new \AdWordsUser();
$user->LogAll();
$campaignService = $user->GetService('CampaignService', 'v201603');
// Create selector.go
$selector = new \Selector();
$selector->fields = array('Id', 'Name');
$selector->ordering[] = new \OrderBy('Name', 'ASCENDING');
// Create paging controls.
$selector->paging = new \Paging(0, \AdWordsConstants::RECOMMENDED_PAGE_SIZE);
do {
$page = $campaignService->get($selector);
if (isset($page->entries)) {
foreach ($page->entries as $campaign) {
printf("Campaign with name '%s' and ID '%s' was found.\n",
$campaign->name, $campaign->id);
}
} else {
print "No campaigns were found.\n";
}
$selector->paging->startIndex += \AdWordsConstants::RECOMMENDED_PAGE_SIZE;
} while ($page->totalNumEntries > $selector->paging->startIndex);
}
But the result is not the two campaigns that i created, is just only one.
I have to say that the one that the api dont give to me, is a Video Campaigns, and not a search campaign.
RESULT OF THE CODE:
1 / 1 (100%)Campaign with name 'Testingalot' and ID '469071928' was found.
It is correct because CampaignService does not show video campaigns in listings. See e.g. https://groups.google.com/forum/#!topic/adwords-api/SH7lk_y4GTw

Block IP address from Google adwords with their API

Does anyone know how to block certain IP addresses from our Google adwords account using the Google API?
In this article you can see how to do it manually, but I cannot find a way to do it programmatically.
I know it's late. But I needed it as well.
I found the IpBlock type on Google AdWords here.
Here is some sample code i found, and improved slightly
require_once this code, then make this call for each campaign u want to ban the IP for
YourNameSpace\BlockedIP::add($campaignId, $ip);
voila
<?php
/*
...
*/
namespace YourNameSpace;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSession;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\Common\OAuth2TokenBuilder;
use Google\AdsApi\AdWords\v201802\cm\CampaignCriterionService;
use Google\AdsApi\AdWords\v201802\cm\IpBlock;
use Google\AdsApi\AdWords\v201802\cm\NegativeCampaignCriterion;
use Google\AdsApi\AdWords\v201802\cm\CampaignCriterionOperation;
use Google\AdsApi\AdWords\v201802\cm\Operator;
class BlockedIP {
public static function runExample(AdWordsServices $adWordsServices,
AdWordsSession $session,
$campaignId,
$ip) {
$campaignCriterionService =
$adWordsServices->get($session, CampaignCriterionService::class);
$campaignCriteria = [];
// Add a negative campaign criterion.
$ipBlock = new IpBlock();
$ipBlock->setIpAddress($ip);
$negativeCriterion = new NegativeCampaignCriterion();
$negativeCriterion->setCampaignId($campaignId);
$negativeCriterion->setCriterion($ipBlock);
$operation = new CampaignCriterionOperation();
$operation->setOperator(Operator::ADD);
$operation->setOperand($negativeCriterion);
$operations[] = $operation;
$result = $campaignCriterionService->mutate($operations);
// Print out some information about added campaign criteria.
foreach ($result->getValue() as $campaignCriterion) {
printf(
"Campaign targeting criterion with ID %d and type '%s' was added.\n",
$campaignCriterion->getCriterion()->getId(),
$campaignCriterion->getCriterion()->getType());
}
}
public static function add($campaignId, $ip) {
// Generate a refreshable OAuth2 credential for authentication.
$oAuth2Credential = (new OAuth2TokenBuilder())
->fromFile()
->build();
// Construct an API session configured from a properties file and the OAuth2
// credentials above.
$session = (new AdWordsSessionBuilder())
->fromFile()
->withOAuth2Credential($oAuth2Credential)
->build();
self::runExample(new AdWordsServices(), $session, $campaignId, $ip);
}
}

Resources