Google-Ads

Google Ads, Formerly known as Adwords, is the Google’s advertising platform. You are already familiar with Google Ads if you use it before, or heard about it, but you should also heard like most Google products that it’s available via API and you can integrate it in your web application. The Google Ads API Client library is available via Composer and Packagist and require at least PHP 7.2 or higher, in addition to the PHP extensions gRPC and Protobuf and the developer token.

Getting started with Google Ads API Client library for PHP

To get started you need first to clone the project :

git clone https://github.com/googleads/google-ads-php.git

Then change directory to cd google-ads-php and run in the command prompt : composer install to install the necessary dependencies. Finally setup your OAuth2 credentials. We recommend you using Google Ads API with your own credentials that you can find instructions to create them here.

Copy the sample google_ads_php.ini to your home directory. This library determines the home directory of your computer by using EnvironmentalVariables::getHome(). Then run the AuthenticateInStandaloneApplication example, which will prompt you for your OAuth2 client ID and secret.

$php examples/Authentication/AuthenticateInStandaloneApplication.php

Copy the output from the last step of the example into the google_ads_php.ini file in your home directory. Don’t forget to fill in your developer token too. You can test if your credentials are correct or not but running getCampaigns example :

$php examples/BasicOperations/GetCampaigns.php --customerId <YOUR_CUSTOMER_ID>

You may have a look at examples directory which will dive you into different aspect of the API including : Account Management, Authentication, Billing, Error Handling, Feeds, Recommendations, Reporting, Targeting, Advanced Operations, Basic Operations, Campaign,Management, Extensions, Migration, Planning, Remarketing, Shopping Ads …etc.

Basic usage

To issue requests via the Google Ads API, you first need to create a GoogleAdsClient. For convenience, you can store the required settings in a properties file (google_ads_php.ini) with the following format:

[GOOGLE_ADS]
developerToken = "INSERT_DEVELOPER_TOKEN_HERE"

[OAUTH2]
clientId = "INSERT_OAUTH2_CLIENT_ID_HERE"
clientSecret = "INSERT_OAUTH2_CLIENT_SECRET_HERE"
refreshToken = "INSERT_OAUTH2_REFRESH_TOKEN_HERE"

If you’re authenticating as a manager account, additionally you must specify the manager account ID (with hyphens removed) as the login customer ID:

[GOOGLE_ADS]
loginCustomerId = "INSERT_LOGIN_CUSTOMER_ID_HERE"

This configuration file format is similar to the format used in the AdWords API’s client library for PHP.

If you have a google_ads_php.ini configuration file in the above format in your home directory, you can use the no-arg version of fromFile() as follows:

$googleAdsClient = (new GoogleAdsClientBuilder())
    ->fromFile()
    ->withOAuth2Credential($oAuth2Credential)
    ->build();

where $oAuth2Credential was created by:

$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();

If your configuration file is not in your home directory, you can pass the file location to the fromFile methods as follows:

$oAuth2Credential = (new OAuth2TokenBuilder())
    ->fromFile('/path/to/google_ads_php.ini')
    ->build();
$googleAdsClient = (new GoogleAdsClientBuilder())
    ->fromFile('/path/to/google_ads_php.ini')
    ->withOAuth2Credential($oAuth2Credential)
    ->build();

You can also construct an OAuth2 credential object by specifying the client ID, client secret, and refresh token at runtime, then pass that to the GoogleAdsClientBuilder as follows:

$oAuth2Credential = (new OAuth2TokenBuilder())
    ->withClientId('INSERT_CLIENT_ID')
    ->withClientSecret('INSERT_CLIENT_SECRET')
    ->withRefreshToken('INSERT_REFRESH_TOKEN')
    ->build();
$googleAdsClient = (new GoogleAdsClientBuilder())
    ->withOAuth2Credential($oAuth2Credential)
    ->withDeveloperToken('INSERT_DEVELOPER_TOKEN_HERE')
    ->withLoginCustomerId('INSERT_LOGIN_CUSTOMER_ID_HERE')
    ->build();

Now you got your $googleAdsClient object defined and ready for use. You might find more detailed documentation on the project github repository, but make sure you follow the best practices in implementing any Google Ads project or application, of course read carefully the API terms and conditions, then once you have a working prototype have a look at the performance guide and how you can make it better.

LEAVE A REPLY

Please enter your comment!
Please enter your name here