diff --git a/php/v4/README.md b/php/v4/README.md new file mode 100644 index 0000000..773cec4 --- /dev/null +++ b/php/v4/README.md @@ -0,0 +1,49 @@ +# DCM/DFA Reporting and Trafficking API PHP Samples + +This is a collection of samples written in PHP which provide a starting place +for your experimentation into the DCM/DFA Reporting and Trafficking API. + +## Prerequisites + + - PHP 5.4+ + - JSON PHP extension + - Composer + +From the example directory, run `composer install` to install all dependencies. + +## Setup Authentication + +This API uses OAuth 2.0. Learn more about Google APIs and OAuth 2.0 here: +https://developers.google.com/accounts/docs/OAuth2 + +Or, if you'd like to dive right in, follow these steps. + - Visit https://console.developers.google.com to register your application. + - From the API Manager -> Overview screen, activate access to "DCM/DFA Reporting and Trafficking API". + - Click on "Credentials" in the left navigation menu + - Click the button labeled "Create credentials" -> "OAuth2 client ID" + - Select "Web Application" as the "Application type" + - Configure javascript origins and redirect URIs + - Authorized Javascript Origins: http://localhost + - Authorized Redirect URIs: http://localhost/path/to/index.php + - Click "Create client ID" + - Click "Download JSON" and save the file as `client_secrets.json` in your + examples directory + +> #### Security alert! + +> Always ensure that your client_secrets.json file is not publicly accessible. +> This file contains credential information which could allow unauthorized access +> to your DFA account. + +## Running the Examples + +I'm assuming you've checked out the code and are reading this from a local +directory. If not check out the code to a local directory. + +1. Open the sample (`http://your/path/index.php`) in your browser + +2. Complete the authorization steps + +3. Select an example and provide the required information + +3. Examine the response, be inspired and start hacking an amazing new app! diff --git a/php/v4/auth/AuthenticateUsingServiceAccount.php b/php/v4/auth/AuthenticateUsingServiceAccount.php new file mode 100644 index 0000000..aeb867a --- /dev/null +++ b/php/v4/auth/AuthenticateUsingServiceAccount.php @@ -0,0 +1,102 @@ + + * + * This optional flag only applies to service accounts which have domain-wide + * delegation enabled and wish to make API requests on behalf of an account + * within that domain. Using this flag will not allow you to impersonate a user + * from a domain that you don't own (e.g., gmail.com). + */ +class AuthenticateUsingServiceAccount +{ + // The OAuth 2.0 scopes to request. + private static $OAUTH_SCOPES = [ + Google_Service_Dfareporting::DFAREPORTING + ]; + + public function run($pathToJsonFile, $email = null) + { + // Create an authenticated client object. + $client = $this->createAuthenticatedClient($pathToJsonFile, $email); + + // Create a Dfareporting service object. + $service = new Google_Service_Dfareporting($client); + + $this->getUserProfiles($service); + } + + private function createAuthenticatedClient($pathToJsonFile, $email) + { + // Create a Google_Client instance. + // + // Note: application name should be replaced with a value that identifies + // your application. Suggested format is "MyCompany-ProductName". + $client = new Google_Client(); + $client->setApplicationName('PHP service account sample'); + $client->setScopes(self::$OAUTH_SCOPES); + + // Load the service account credentials. + $client->setAuthConfig($pathToJsonFile); + + // Configure impersonation (if applicable). + if (!is_null($email)) { + $client->setSubject($email); + } + + return $client; + } + + private function getUserProfiles($service) + { + // Retrieve and print all user profiles for the current authorized user. + $result = $service->userProfiles->listUserProfiles(); + foreach ($result['items'] as $userProfile) { + printf( + "User profile \"%s\" (ID: %d) found for account %d.\n", + $userProfile->getUserName(), + $userProfile->getProfileId(), + $userProfile->getAccountId() + ); + } + } +} + +if ($argc < 2 || $argc >= 4) { + printf( + "Usage: %s /path/to/client_secrets.json [email_to_impersonate]\n", + $argv[0] + ); +} else { + $sample = new AuthenticateUsingServiceAccount(); + + if ($argc == 2) { + $sample->run($argv[1]); + } else { + $sample->run($argv[1], $argv[2]); + } +} diff --git a/php/v4/auth/AuthenticateUsingUserAccount.php b/php/v4/auth/AuthenticateUsingUserAccount.php new file mode 100644 index 0000000..0c3a003 --- /dev/null +++ b/php/v4/auth/AuthenticateUsingUserAccount.php @@ -0,0 +1,110 @@ +createAuthenticatedClient( + $pathToJsonFile, + self::TOKEN_STORE + ); + + // Create a Dfareporting service object. + $service = new Google_Service_Dfareporting($client); + + $this->getUserProfiles($service); + } + + private function createAuthenticatedClient($pathToJsonFile, $tokenStore) + { + // Create a Google_Client instance. + // + // Note: application name should be replaced with a value that identifies + // your application. Suggested format is "MyCompany-ProductName". + $client = new Google_Client(); + $client->setAccessType('offline'); + $client->setApplicationName('PHP installed app sample'); + $client->setRedirectUri(self::OAUTH_REDIRECT_URI); + $client->setScopes(self::$OAUTH_SCOPES); + + // Load the client secrets file. + $client->setAuthConfig($pathToJsonFile); + + // Try to load cached credentials from the token store. Using a token store + // allows auth credentials to be cached, so they survive multiple runs of + // the application. This avoids prompting the user for authorization every + // time the access token expires, by remembering the refresh token. + if (file_exists($tokenStore) && filesize($tokenStore) > 0) { + $client->setAccessToken(file_get_contents($tokenStore)); + } else { + // If no cached credentials were found, authorize and persist + // credentials to the token store. + print 'Open this URL in your browser and authorize the application.'; + printf("\n\n%s\n\n", $client->createAuthUrl()); + print 'Enter the authorization code: '; + $code = trim(fgets(STDIN)); + $client->authenticate($code); + + file_put_contents($tokenStore, json_encode($client->getAccessToken())); + } + + return $client; + } + + private function getUserProfiles($service) + { + // Retrieve and print all user profiles for the current authorized user. + $result = $service->userProfiles->listUserProfiles(); + foreach ($result['items'] as $userProfile) { + printf( + "User profile \"%s\" (ID: %d) found for account %d.\n", + $userProfile->getUserName(), + $userProfile->getProfileId(), + $userProfile->getAccountId() + ); + } + } +} + +if ($argc !== 2) { + printf("Usage: %s /path/to/client_secrets.json\n", $argv[0]); +} else { + $sample = new AuthenticateUsingUserAccount(); + $sample->run($argv[1]); +} diff --git a/php/v4/client_secrets.json b/php/v4/client_secrets.json new file mode 100755 index 0000000..f29d5ac --- /dev/null +++ b/php/v4/client_secrets.json @@ -0,0 +1,7 @@ +{ + "web": { + "client_id": "Enter Client ID", + "client_secret": "Enter Client Secret", + "redirect_uris": ["http://path/to/the/samples"] + } +} \ No newline at end of file diff --git a/php/v4/composer.json b/php/v4/composer.json new file mode 100755 index 0000000..801ab7f --- /dev/null +++ b/php/v4/composer.json @@ -0,0 +1,7 @@ +{ + "require": { + "google/apiclient-services": "dev-master", + "google/apiclient": "^2.0", + "guzzlehttp/psr7": "^1.2" + } +} diff --git a/php/v4/composer.lock b/php/v4/composer.lock new file mode 100644 index 0000000..534bde9 --- /dev/null +++ b/php/v4/composer.lock @@ -0,0 +1,1215 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "57eb9f6c680a4d150ad46173a5412224", + "packages": [ + { + "name": "firebase/php-jwt", + "version": "v6.2.0", + "source": { + "type": "git", + "url": "https://github.com/firebase/php-jwt.git", + "reference": "d28e6df83830252650da4623c78aaaf98fb385f3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/firebase/php-jwt/zipball/d28e6df83830252650da4623c78aaaf98fb385f3", + "reference": "d28e6df83830252650da4623c78aaaf98fb385f3", + "shasum": "" + }, + "require": { + "php": "^7.1||^8.0" + }, + "require-dev": { + "guzzlehttp/guzzle": "^6.5||^7.4", + "phpspec/prophecy-phpunit": "^1.1", + "phpunit/phpunit": "^7.5||^9.5", + "psr/cache": "^1.0||^2.0", + "psr/http-client": "^1.0", + "psr/http-factory": "^1.0" + }, + "suggest": { + "paragonie/sodium_compat": "Support EdDSA (Ed25519) signatures when libsodium is not present" + }, + "type": "library", + "autoload": { + "psr-4": { + "Firebase\\JWT\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Neuman Vong", + "email": "neuman+pear@twilio.com", + "role": "Developer" + }, + { + "name": "Anant Narayanan", + "email": "anant@php.net", + "role": "Developer" + } + ], + "description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.", + "homepage": "https://github.com/firebase/php-jwt", + "keywords": [ + "jwt", + "php" + ], + "support": { + "issues": "https://github.com/firebase/php-jwt/issues", + "source": "https://github.com/firebase/php-jwt/tree/v6.2.0" + }, + "time": "2022-05-13T20:54:50+00:00" + }, + { + "name": "google/apiclient", + "version": "v2.12.6", + "source": { + "type": "git", + "url": "https://github.com/googleapis/google-api-php-client.git", + "reference": "f92aa126903a9e2da5bd41a280d9633cb249e79e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/googleapis/google-api-php-client/zipball/f92aa126903a9e2da5bd41a280d9633cb249e79e", + "reference": "f92aa126903a9e2da5bd41a280d9633cb249e79e", + "shasum": "" + }, + "require": { + "firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0||~6.0", + "google/apiclient-services": "~0.200", + "google/auth": "^1.10", + "guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0", + "guzzlehttp/psr7": "^1.8.4||^2.2.1", + "monolog/monolog": "^1.17||^2.0||^3.0", + "php": "^5.6|^7.0|^8.0", + "phpseclib/phpseclib": "~2.0||^3.0.2" + }, + "require-dev": { + "cache/filesystem-adapter": "^0.3.2|^1.1", + "composer/composer": "^1.10.22", + "phpcompatibility/php-compatibility": "^9.2", + "phpspec/prophecy-phpunit": "^1.1||^2.0", + "phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0", + "squizlabs/php_codesniffer": "^3.0", + "symfony/css-selector": "~2.1", + "symfony/dom-crawler": "~2.1", + "yoast/phpunit-polyfills": "^1.0" + }, + "suggest": { + "cache/filesystem-adapter": "For caching certs and tokens (using Google\\Client::setCache)" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.x-dev" + } + }, + "autoload": { + "files": [ + "src/aliases.php" + ], + "psr-4": { + "Google\\": "src/" + }, + "classmap": [ + "src/aliases.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "description": "Client library for Google APIs", + "homepage": "http://developers.google.com/api-client-library/php", + "keywords": [ + "google" + ], + "support": { + "issues": "https://github.com/googleapis/google-api-php-client/issues", + "source": "https://github.com/googleapis/google-api-php-client/tree/v2.12.6" + }, + "time": "2022-06-06T20:00:19+00:00" + }, + { + "name": "google/apiclient-services", + "version": "v0.253.0", + "source": { + "type": "git", + "url": "https://github.com/googleapis/google-api-php-client-services.git", + "reference": "70c62b17f7821526cb52c6f125254dc51f256109" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/googleapis/google-api-php-client-services/zipball/70c62b17f7821526cb52c6f125254dc51f256109", + "reference": "70c62b17f7821526cb52c6f125254dc51f256109", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "require-dev": { + "phpunit/phpunit": "^5.7||^8.5.13" + }, + "type": "library", + "autoload": { + "files": [ + "autoload.php" + ], + "psr-4": { + "Google\\Service\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "description": "Client library for Google APIs", + "homepage": "http://developers.google.com/api-client-library/php", + "keywords": [ + "google" + ], + "support": { + "issues": "https://github.com/googleapis/google-api-php-client-services/issues", + "source": "https://github.com/googleapis/google-api-php-client-services/tree/v0.253.0" + }, + "time": "2022-06-13T01:06:12+00:00" + }, + { + "name": "google/auth", + "version": "v1.21.1", + "source": { + "type": "git", + "url": "https://github.com/googleapis/google-auth-library-php.git", + "reference": "aa3b9ca29258ac6347ce3c8937a2418c5d78f840" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/googleapis/google-auth-library-php/zipball/aa3b9ca29258ac6347ce3c8937a2418c5d78f840", + "reference": "aa3b9ca29258ac6347ce3c8937a2418c5d78f840", + "shasum": "" + }, + "require": { + "firebase/php-jwt": "^5.5||^6.0", + "guzzlehttp/guzzle": "^6.2.1|^7.0", + "guzzlehttp/psr7": "^1.7|^2.0", + "php": "^7.1||^8.0", + "psr/cache": "^1.0|^2.0|^3.0", + "psr/http-message": "^1.0" + }, + "require-dev": { + "guzzlehttp/promises": "0.1.1|^1.3", + "kelvinmo/simplejwt": "^0.2.5|^0.5.1", + "phpseclib/phpseclib": "^2.0.31", + "phpspec/prophecy-phpunit": "^1.1", + "phpunit/phpunit": "^7.5||^8.5", + "sebastian/comparator": ">=1.2.3", + "squizlabs/php_codesniffer": "^3.5" + }, + "suggest": { + "phpseclib/phpseclib": "May be used in place of OpenSSL for signing strings or for token management. Please require version ^2." + }, + "type": "library", + "autoload": { + "psr-4": { + "Google\\Auth\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "description": "Google Auth Library for PHP", + "homepage": "http://github.com/google/google-auth-library-php", + "keywords": [ + "Authentication", + "google", + "oauth2" + ], + "support": { + "docs": "https://googleapis.github.io/google-auth-library-php/main/", + "issues": "https://github.com/googleapis/google-auth-library-php/issues", + "source": "https://github.com/googleapis/google-auth-library-php/tree/v1.21.1" + }, + "time": "2022-05-16T19:34:15+00:00" + }, + { + "name": "guzzlehttp/guzzle", + "version": "7.4.4", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "e3ff079b22820c2029d4c2a87796b6a0b8716ad8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/e3ff079b22820c2029d4c2a87796b6a0b8716ad8", + "reference": "e3ff079b22820c2029d4c2a87796b6a0b8716ad8", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/promises": "^1.5", + "guzzlehttp/psr7": "^1.8.3 || ^2.1", + "php": "^7.2.5 || ^8.0", + "psr/http-client": "^1.0", + "symfony/deprecation-contracts": "^2.2 || ^3.0" + }, + "provide": { + "psr/http-client-implementation": "1.0" + }, + "require-dev": { + "bamarni/composer-bin-plugin": "^1.4.1", + "ext-curl": "*", + "php-http/client-integration-tests": "^3.0", + "phpunit/phpunit": "^8.5.5 || ^9.3.5", + "psr/log": "^1.1 || ^2.0 || ^3.0" + }, + "suggest": { + "ext-curl": "Required for CURL handler support", + "ext-intl": "Required for Internationalized Domain Name (IDN) support", + "psr/log": "Required for using the Log middleware" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "7.4-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Jeremy Lindblom", + "email": "jeremeamia@gmail.com", + "homepage": "https://github.com/jeremeamia" + }, + { + "name": "George Mponos", + "email": "gmponos@gmail.com", + "homepage": "https://github.com/gmponos" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com", + "homepage": "https://github.com/sagikazarmark" + }, + { + "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", + "homepage": "https://github.com/Tobion" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "psr-18", + "psr-7", + "rest", + "web service" + ], + "support": { + "issues": "https://github.com/guzzle/guzzle/issues", + "source": "https://github.com/guzzle/guzzle/tree/7.4.4" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/guzzle", + "type": "tidelift" + } + ], + "time": "2022-06-09T21:39:15+00:00" + }, + { + "name": "guzzlehttp/promises", + "version": "1.5.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "fe752aedc9fd8fcca3fe7ad05d419d32998a06da" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/fe752aedc9fd8fcca3fe7ad05d419d32998a06da", + "reference": "fe752aedc9fd8fcca3fe7ad05d419d32998a06da", + "shasum": "" + }, + "require": { + "php": ">=5.5" + }, + "require-dev": { + "symfony/phpunit-bridge": "^4.4 || ^5.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.5-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", + "homepage": "https://github.com/Tobion" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "support": { + "issues": "https://github.com/guzzle/promises/issues", + "source": "https://github.com/guzzle/promises/tree/1.5.1" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/promises", + "type": "tidelift" + } + ], + "time": "2021-10-22T20:56:57+00:00" + }, + { + "name": "guzzlehttp/psr7", + "version": "1.8.5", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "337e3ad8e5716c15f9657bd214d16cc5e69df268" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/337e3ad8e5716c15f9657bd214d16cc5e69df268", + "reference": "337e3ad8e5716c15f9657bd214d16cc5e69df268", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0", + "ralouphie/getallheaders": "^2.0.5 || ^3.0.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "ext-zlib": "*", + "phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.14 || ^7.5.20 || ^8.5.8 || ^9.3.10" + }, + "suggest": { + "laminas/laminas-httphandlerrunner": "Emit PSR-7 responses" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.7-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + }, + { + "name": "George Mponos", + "email": "gmponos@gmail.com", + "homepage": "https://github.com/gmponos" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com", + "homepage": "https://github.com/sagikazarmark" + }, + { + "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", + "homepage": "https://github.com/Tobion" + } + ], + "description": "PSR-7 message implementation that also provides common utility methods", + "keywords": [ + "http", + "message", + "psr-7", + "request", + "response", + "stream", + "uri", + "url" + ], + "support": { + "issues": "https://github.com/guzzle/psr7/issues", + "source": "https://github.com/guzzle/psr7/tree/1.8.5" + }, + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/psr7", + "type": "tidelift" + } + ], + "time": "2022-03-20T21:51:18+00:00" + }, + { + "name": "monolog/monolog", + "version": "3.1.0", + "source": { + "type": "git", + "url": "https://github.com/Seldaek/monolog.git", + "reference": "0c375495d40df0207e5833dca333f963b171ff43" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/0c375495d40df0207e5833dca333f963b171ff43", + "reference": "0c375495d40df0207e5833dca333f963b171ff43", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "psr/log": "^2.0 || ^3.0" + }, + "provide": { + "psr/log-implementation": "3.0.0" + }, + "require-dev": { + "aws/aws-sdk-php": "^3.0", + "doctrine/couchdb": "~1.0@dev", + "elasticsearch/elasticsearch": "^7 || ^8", + "ext-json": "*", + "graylog2/gelf-php": "^1.4.2", + "guzzlehttp/guzzle": "^7.4", + "guzzlehttp/psr7": "^2.2", + "mongodb/mongodb": "^1.8", + "php-amqplib/php-amqplib": "~2.4 || ^3", + "php-console/php-console": "^3.1.3", + "phpstan/phpstan": "^1.4", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-strict-rules": "^1.1", + "phpunit/phpunit": "^9.5.16", + "predis/predis": "^1.1", + "ruflin/elastica": "^7", + "symfony/mailer": "^5.4 || ^6", + "symfony/mime": "^5.4 || ^6" + }, + "suggest": { + "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", + "doctrine/couchdb": "Allow sending log messages to a CouchDB server", + "elasticsearch/elasticsearch": "Allow sending log messages to an Elasticsearch server via official client", + "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", + "ext-curl": "Required to send log messages using the IFTTTHandler, the LogglyHandler, the SendGridHandler, the SlackWebhookHandler or the TelegramBotHandler", + "ext-mbstring": "Allow to work properly with unicode symbols", + "ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)", + "ext-openssl": "Required to send log messages using SSL", + "ext-sockets": "Allow sending log messages to a Syslog server (via UDP driver)", + "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server", + "mongodb/mongodb": "Allow sending log messages to a MongoDB server (via library)", + "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib", + "php-console/php-console": "Allow sending log messages to Google Chrome", + "rollbar/rollbar": "Allow sending log messages to Rollbar", + "ruflin/elastica": "Allow sending log messages to an Elastic Search server" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Monolog\\": "src/Monolog" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "https://seld.be" + } + ], + "description": "Sends your logs to files, sockets, inboxes, databases and various web services", + "homepage": "https://github.com/Seldaek/monolog", + "keywords": [ + "log", + "logging", + "psr-3" + ], + "support": { + "issues": "https://github.com/Seldaek/monolog/issues", + "source": "https://github.com/Seldaek/monolog/tree/3.1.0" + }, + "funding": [ + { + "url": "https://github.com/Seldaek", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/monolog/monolog", + "type": "tidelift" + } + ], + "time": "2022-06-09T09:09:00+00:00" + }, + { + "name": "paragonie/constant_time_encoding", + "version": "v2.6.3", + "source": { + "type": "git", + "url": "https://github.com/paragonie/constant_time_encoding.git", + "reference": "58c3f47f650c94ec05a151692652a868995d2938" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/paragonie/constant_time_encoding/zipball/58c3f47f650c94ec05a151692652a868995d2938", + "reference": "58c3f47f650c94ec05a151692652a868995d2938", + "shasum": "" + }, + "require": { + "php": "^7|^8" + }, + "require-dev": { + "phpunit/phpunit": "^6|^7|^8|^9", + "vimeo/psalm": "^1|^2|^3|^4" + }, + "type": "library", + "autoload": { + "psr-4": { + "ParagonIE\\ConstantTime\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Paragon Initiative Enterprises", + "email": "security@paragonie.com", + "homepage": "https://paragonie.com", + "role": "Maintainer" + }, + { + "name": "Steve 'Sc00bz' Thomas", + "email": "steve@tobtu.com", + "homepage": "https://www.tobtu.com", + "role": "Original Developer" + } + ], + "description": "Constant-time Implementations of RFC 4648 Encoding (Base-64, Base-32, Base-16)", + "keywords": [ + "base16", + "base32", + "base32_decode", + "base32_encode", + "base64", + "base64_decode", + "base64_encode", + "bin2hex", + "encoding", + "hex", + "hex2bin", + "rfc4648" + ], + "support": { + "email": "info@paragonie.com", + "issues": "https://github.com/paragonie/constant_time_encoding/issues", + "source": "https://github.com/paragonie/constant_time_encoding" + }, + "time": "2022-06-14T06:56:20+00:00" + }, + { + "name": "paragonie/random_compat", + "version": "v9.99.100", + "source": { + "type": "git", + "url": "https://github.com/paragonie/random_compat.git", + "reference": "996434e5492cb4c3edcb9168db6fbb1359ef965a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/paragonie/random_compat/zipball/996434e5492cb4c3edcb9168db6fbb1359ef965a", + "reference": "996434e5492cb4c3edcb9168db6fbb1359ef965a", + "shasum": "" + }, + "require": { + "php": ">= 7" + }, + "require-dev": { + "phpunit/phpunit": "4.*|5.*", + "vimeo/psalm": "^1" + }, + "suggest": { + "ext-libsodium": "Provides a modern crypto API that can be used to generate random bytes." + }, + "type": "library", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Paragon Initiative Enterprises", + "email": "security@paragonie.com", + "homepage": "https://paragonie.com" + } + ], + "description": "PHP 5.x polyfill for random_bytes() and random_int() from PHP 7", + "keywords": [ + "csprng", + "polyfill", + "pseudorandom", + "random" + ], + "support": { + "email": "info@paragonie.com", + "issues": "https://github.com/paragonie/random_compat/issues", + "source": "https://github.com/paragonie/random_compat" + }, + "time": "2020-10-15T08:29:30+00:00" + }, + { + "name": "phpseclib/phpseclib", + "version": "3.0.14", + "source": { + "type": "git", + "url": "https://github.com/phpseclib/phpseclib.git", + "reference": "2f0b7af658cbea265cbb4a791d6c29a6613f98ef" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/2f0b7af658cbea265cbb4a791d6c29a6613f98ef", + "reference": "2f0b7af658cbea265cbb4a791d6c29a6613f98ef", + "shasum": "" + }, + "require": { + "paragonie/constant_time_encoding": "^1|^2", + "paragonie/random_compat": "^1.4|^2.0|^9.99.99", + "php": ">=5.6.1" + }, + "require-dev": { + "phpunit/phpunit": "*" + }, + "suggest": { + "ext-gmp": "Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations.", + "ext-libsodium": "SSH2/SFTP can make use of some algorithms provided by the libsodium-php extension.", + "ext-mcrypt": "Install the Mcrypt extension in order to speed up a few other cryptographic operations.", + "ext-openssl": "Install the OpenSSL extension in order to speed up a wide variety of cryptographic operations." + }, + "type": "library", + "autoload": { + "files": [ + "phpseclib/bootstrap.php" + ], + "psr-4": { + "phpseclib3\\": "phpseclib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jim Wigginton", + "email": "terrafrost@php.net", + "role": "Lead Developer" + }, + { + "name": "Patrick Monnerat", + "email": "pm@datasphere.ch", + "role": "Developer" + }, + { + "name": "Andreas Fischer", + "email": "bantu@phpbb.com", + "role": "Developer" + }, + { + "name": "Hans-Jürgen Petrich", + "email": "petrich@tronic-media.com", + "role": "Developer" + }, + { + "name": "Graham Campbell", + "email": "graham@alt-three.com", + "role": "Developer" + } + ], + "description": "PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.", + "homepage": "http://phpseclib.sourceforge.net", + "keywords": [ + "BigInteger", + "aes", + "asn.1", + "asn1", + "blowfish", + "crypto", + "cryptography", + "encryption", + "rsa", + "security", + "sftp", + "signature", + "signing", + "ssh", + "twofish", + "x.509", + "x509" + ], + "support": { + "issues": "https://github.com/phpseclib/phpseclib/issues", + "source": "https://github.com/phpseclib/phpseclib/tree/3.0.14" + }, + "funding": [ + { + "url": "https://github.com/terrafrost", + "type": "github" + }, + { + "url": "https://www.patreon.com/phpseclib", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpseclib/phpseclib", + "type": "tidelift" + } + ], + "time": "2022-04-04T05:15:45+00:00" + }, + { + "name": "psr/cache", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/cache.git", + "reference": "aa5030cfa5405eccfdcb1083ce040c2cb8d253bf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/cache/zipball/aa5030cfa5405eccfdcb1083ce040c2cb8d253bf", + "reference": "aa5030cfa5405eccfdcb1083ce040c2cb8d253bf", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Cache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for caching libraries", + "keywords": [ + "cache", + "psr", + "psr-6" + ], + "support": { + "source": "https://github.com/php-fig/cache/tree/3.0.0" + }, + "time": "2021-02-03T23:26:27+00:00" + }, + { + "name": "psr/http-client", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-client.git", + "reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-client/zipball/2dfb5f6c5eff0e91e20e913f8c5452ed95b86621", + "reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621", + "shasum": "" + }, + "require": { + "php": "^7.0 || ^8.0", + "psr/http-message": "^1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Client\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP clients", + "homepage": "https://github.com/php-fig/http-client", + "keywords": [ + "http", + "http-client", + "psr", + "psr-18" + ], + "support": { + "source": "https://github.com/php-fig/http-client/tree/master" + }, + "time": "2020-06-29T06:28:15+00:00" + }, + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "support": { + "source": "https://github.com/php-fig/http-message/tree/master" + }, + "time": "2016-08-06T14:39:51+00:00" + }, + { + "name": "psr/log", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/fe5ea303b0887d5caefd3d431c3e61ad47037001", + "reference": "fe5ea303b0887d5caefd3d431c3e61ad47037001", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "support": { + "source": "https://github.com/php-fig/log/tree/3.0.0" + }, + "time": "2021-07-14T16:46:02+00:00" + }, + { + "name": "ralouphie/getallheaders", + "version": "3.0.3", + "source": { + "type": "git", + "url": "https://github.com/ralouphie/getallheaders.git", + "reference": "120b605dfeb996808c31b6477290a714d356e822" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822", + "reference": "120b605dfeb996808c31b6477290a714d356e822", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "require-dev": { + "php-coveralls/php-coveralls": "^2.1", + "phpunit/phpunit": "^5 || ^6.5" + }, + "type": "library", + "autoload": { + "files": [ + "src/getallheaders.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ralph Khattar", + "email": "ralph.khattar@gmail.com" + } + ], + "description": "A polyfill for getallheaders.", + "support": { + "issues": "https://github.com/ralouphie/getallheaders/issues", + "source": "https://github.com/ralouphie/getallheaders/tree/develop" + }, + "time": "2019-03-08T08:55:37+00:00" + }, + { + "name": "symfony/deprecation-contracts", + "version": "v3.1.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/deprecation-contracts.git", + "reference": "07f1b9cc2ffee6aaafcf4b710fbc38ff736bd918" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/07f1b9cc2ffee6aaafcf4b710fbc38ff736bd918", + "reference": "07f1b9cc2ffee6aaafcf4b710fbc38ff736bd918", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "3.1-dev" + }, + "thanks": { + "name": "symfony/contracts", + "url": "https://github.com/symfony/contracts" + } + }, + "autoload": { + "files": [ + "function.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "A generic function and convention to trigger deprecation notices", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/deprecation-contracts/tree/v3.1.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-02-25T11:15:52+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.3.0" +} diff --git a/php/v4/examples/Ads/CreateRotationGroup.php b/php/v4/examples/Ads/CreateRotationGroup.php new file mode 100755 index 0000000..52b68a4 --- /dev/null +++ b/php/v4/examples/Ads/CreateRotationGroup.php @@ -0,0 +1,140 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'campaign_id', + 'display' => 'Campaign ID', + 'required' => true], + ['name' => 'creative_id', + 'display' => 'Creative ID', + 'required' => true], + ['name' => 'placement_id', + 'display' => 'Placement ID', + 'required' => true], + ['name' => 'ad_name', + 'display' => 'Ad Name', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating rotation group ad with name "%s" for placement ID' + . ' %s

', + $values['ad_name'], + $values['placement_id'] + ); + + // Retrieve the campaign. + $campaign = $this->service->campaigns->get( + $values['user_profile_id'], + $values['campaign_id'] + ); + + // Create a click-through URL. + $url = new Google_Service_Dfareporting_ClickThroughUrl(); + $url->setDefaultLandingPage(true); + + // Create a creative assignment. + $creativeAssignment = + new Google_Service_Dfareporting_CreativeAssignment(); + $creativeAssignment->setActive(true); + $creativeAssignment->setCreativeId($values['creative_id']); + $creativeAssignment->setClickThroughUrl($url); + + // Create a placement assignment. + $placementAssignment = + new Google_Service_Dfareporting_PlacementAssignment(); + $placementAssignment->setActive(true); + $placementAssignment->setPlacementId($values['placement_id']); + + // Create a creative rotation. + $creativeRotation = new Google_Service_Dfareporting_CreativeRotation(); + $creativeRotation->setCreativeAssignments([$creativeAssignment]); + + // Create a delivery schedule. + $deliverySchedule = new Google_Service_Dfareporting_DeliverySchedule(); + $deliverySchedule->setImpressionRatio(1); + $deliverySchedule->SetPriority('AD_PRIORITY_01'); + + $startDate = new DateTime('today'); + $endDate = new DateTime($campaign->getEndDate()); + + // Create a rotation group. + $ad = new Google_Service_Dfareporting_Ad(); + $ad->setActive(true); + $ad->setCampaignId($values['campaign_id']); + $ad->setCreativeRotation($creativeRotation); + $ad->setDeliverySchedule($deliverySchedule); + $ad->setStartTime($startDate->format('Y-m-d') . 'T23:59:59Z'); + $ad->setEndTime($endDate->format('Y-m-d') . 'T00:00:00Z'); + $ad->setName($values['ad_name']); + $ad->setPlacementAssignments([$placementAssignment]); + $ad->setType('AD_SERVING_STANDARD_AD'); + + $result = $this->service->ads->insert($values['user_profile_id'], $ad); + + $this->printResultsTable('Rotation group ad created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Rotation Group Ad'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Ad ID', + 'name' => 'Ad Name']; + } +} diff --git a/php/v4/examples/Ads/GetAds.php b/php/v4/examples/Ads/GetAds.php new file mode 100755 index 0000000..ac71504 --- /dev/null +++ b/php/v4/examples/Ads/GetAds.php @@ -0,0 +1,93 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all ads

'; + + $this->printResultsTableHeader('Ads'); + + $response = null; + $pageToken = null; + + do { + // Create and execute the ads list request. + $response = $this->service->ads->listAds( + $values['user_profile_id'], + ['active' => true, 'pageToken' => $pageToken] + ); + + foreach ($response->getAds() as $ads) { + $this->printResultsTableRow($ads); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getAds()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Ads'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Ad ID', + 'name' => 'Ad Name', + 'advertiserId' => 'Associated Advertiser ID']; + } +} diff --git a/php/v4/examples/Advertisers/AssignAdvertisersToAdvertiserGroup.php b/php/v4/examples/Advertisers/AssignAdvertisersToAdvertiserGroup.php new file mode 100755 index 0000000..5492113 --- /dev/null +++ b/php/v4/examples/Advertisers/AssignAdvertisersToAdvertiserGroup.php @@ -0,0 +1,94 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'advertiser_group_id', + 'display' => 'Advertiser Group ID', + 'required' => true]]; + } + + /** + * {@inheritdoc} + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Assigning advertiser ID %s to advertiser group ID %s

', + $values['advertiser_id'], + $values['advertiser_group_id'] + ); + + $advertiser = new Google_Service_Dfareporting_Advertiser(); + $advertiser->setAdvertiserGroupId($values['advertiser_group_id']); + + $result = $this->service->advertisers->patch( + $values['user_profile_id'], + $values['advertiser_id'], + $advertiser + ); + + $this->printResultsTable('Advertiser', [$result]); + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Assign Advertiser To Advertiser Group'; + } + + /** + * {@inheritdoc} + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Advertiser ID', + 'name' => 'Advertiser Name', + 'advertiserGroupId' => 'Advertiser Group ID']; + } +} diff --git a/php/v4/examples/Advertisers/CreateAdvertiser.php b/php/v4/examples/Advertisers/CreateAdvertiser.php new file mode 100755 index 0000000..1b41101 --- /dev/null +++ b/php/v4/examples/Advertisers/CreateAdvertiser.php @@ -0,0 +1,87 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_name', + 'display' => 'Advertiser Name', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating advertiser with name "%s"

', + $values['advertiser_name'] + ); + + $advertiser = new Google_Service_Dfareporting_Advertiser(); + $advertiser->setName($values['advertiser_name']); + $advertiser->setStatus('APPROVED'); + + $result = $this->service->advertisers->insert( + $values['user_profile_id'], + $advertiser + ); + + $this->printResultsTable('Advertiser created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Advertiser'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Advertiser ID', + 'name' => 'Advertiser Name']; + } +} diff --git a/php/v4/examples/Advertisers/CreateAdvertiserGroup.php b/php/v4/examples/Advertisers/CreateAdvertiserGroup.php new file mode 100755 index 0000000..0889be8 --- /dev/null +++ b/php/v4/examples/Advertisers/CreateAdvertiserGroup.php @@ -0,0 +1,87 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_group_name', + 'display' => 'Advertiser Group Name', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating advertiser group with name "%s"

', + $values['advertiser_group_name'] + ); + + $advertiser = new Google_Service_Dfareporting_AdvertiserGroup(); + $advertiser->setName($values['advertiser_group_name']); + + + $result = $this->service->advertiserGroups->insert( + $values['user_profile_id'], + $advertiser + ); + + $this->printResultsTable('Advertiser group created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Advertiser Group'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Advertiser Group ID', + 'name' => 'Advertiser Group Name']; + } +} diff --git a/php/v4/examples/Advertisers/CreateAdvertiserLandingPage.php b/php/v4/examples/Advertisers/CreateAdvertiserLandingPage.php new file mode 100755 index 0000000..394e2db --- /dev/null +++ b/php/v4/examples/Advertisers/CreateAdvertiserLandingPage.php @@ -0,0 +1,95 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'landing_page_name', + 'display' => 'Landing Page Name', + 'required' => true], + ['name' => 'landing_page_url', + 'display' => 'Landing Page URL', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating advertiser landing page for advertiser %d

', + $values['advertiser_id'] + ); + + $landingPage = new Google_Service_Dfareporting_LandingPage(); + $landingPage->setAdvertiserId($values['advertiser_id']); + $landingPage->setArchived(false); + $landingPage->setName($values['landing_page_name']); + $landingPage->setUrl($values['landing_page_url']); + + $result = $this->service->advertiserLandingPages->insert( + $values['user_profile_id'], + $landingPage + ); + + $this->printResultsTable('Advertiser landing page created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Advertiser Landing Page'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Advertiser Landing Page ID', + 'name' => 'Advertiser Landing Page Name', + 'url' => 'Advertiser Landing Page URL']; + } +} diff --git a/php/v4/examples/Advertisers/GetAdvertiserGroups.php b/php/v4/examples/Advertisers/GetAdvertiserGroups.php new file mode 100755 index 0000000..27e971a --- /dev/null +++ b/php/v4/examples/Advertisers/GetAdvertiserGroups.php @@ -0,0 +1,91 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all advertiser groups

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Advertiser groups'); + + do { + // Create and execute the advertiser groups list request. + $response = $this->service->advertiserGroups->listAdvertiserGroups( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getAdvertiserGroups() as $group) { + $this->printResultsTableRow($group); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getAdvertiserGroups()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Advertiser Groups'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Advertiser Group ID', + 'name' => 'Advertiser Group Name']; + } +} diff --git a/php/v4/examples/Advertisers/GetAdvertiserLandingPages.php b/php/v4/examples/Advertisers/GetAdvertiserLandingPages.php new file mode 100755 index 0000000..b3aa7d9 --- /dev/null +++ b/php/v4/examples/Advertisers/GetAdvertiserLandingPages.php @@ -0,0 +1,101 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Listing all landing pages for advertiser %d

', + $values['advertiser_id'] + ); + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Advertiser landing pages'); + + do { + // Create and execute the advertiser landing page list request. + $response = + $this->service->advertiserLandingPages->listAdvertiserLandingPages( + $values['user_profile_id'], + ['advertiserIds' => [$values['advertiser_id']], + 'pageToken' => $pageToken] + ); + + foreach ($response->getLandingPages() as $landingPage) { + $this->printResultsTableRow($landingPage); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getLandingPages()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Advertiser Landing Pages'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Advertiser Landing Page ID', + 'name' => 'Advertiser Landing Page Name', + 'url' => 'Advertiser Landing Page URL']; + } +} diff --git a/php/v4/examples/Advertisers/GetAdvertisers.php b/php/v4/examples/Advertisers/GetAdvertisers.php new file mode 100755 index 0000000..94de182 --- /dev/null +++ b/php/v4/examples/Advertisers/GetAdvertisers.php @@ -0,0 +1,93 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all advertisers

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Advertisers'); + + do { + // Create and execute the advertisers list request. + $response = $this->service->advertisers->listAdvertisers( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getAdvertisers() as $advertiser) { + $this->printResultsTableRow($advertiser); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getAdvertisers()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Advertisers'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Advertiser ID', + 'name' => 'Advertiser Name', + 'floodlightConfigurationId' => 'Floodlight Configuration ID']; + } +} diff --git a/php/v4/examples/BaseExample.php b/php/v4/examples/BaseExample.php new file mode 100755 index 0000000..0d0d99e --- /dev/null +++ b/php/v4/examples/BaseExample.php @@ -0,0 +1,277 @@ +service = $service; + $this->headers = $this->getResultsTableHeaders(); + } + + /** + * Contains the logic of the example. + */ + abstract protected function run(); + + /** + * Executes the example, checks if the examples requires parameters and + * request them before invoking run. + */ + public function execute() + { + if (count($this->getInputParameters())) { + if ($this->isSubmitComplete()) { + $this->formValues = $this->getFormValues(); + $this->run(); + } else { + $this->renderInputForm(); + } + } else { + $this->run(); + } + } + + /** + * Returns a display name for the example. + * @return string + */ + abstract public static function getName(); + + /** + * Returns the list of input parameters of the example. + * To be overridden by examples that require parameters. + * @return array + */ + protected function getInputParameters() + { + return []; + } + + /** + * Returns the list of headers to be shown in the results table. + * To be overridden by examples that require table headers. + * @return array + */ + protected function getResultsTableHeaders() + { + return []; + } + + /** + * Renders an input form to capture the example parameters. + */ + protected function renderInputForm() + { + $parameters = $this->getInputParameters(); + if (count($parameters)) { + printf('

Enter %s parameters

', $this->getName()); + print '
'; + foreach ($parameters as $parameter) { + $name = $parameter['name']; + $display = $parameter['display']; + + if (isset($_POST[$name])) { + $currentValue = $_POST[$name]; + } else { + $currentValue = ''; + } + + // If this is a file parameter, generate a file input element + if ($parameter['file']) { + $inputType = ' type="file"'; + } + + printf( + '%s: ', + $display, + $name, + $currentValue, + $inputType + ); + + if ($parameter['required']) { + print '*'; + } + + print '
'; + } + print '
*required
'; + print ''; + print '
'; + } + + $this->flush(); + } + + /** + * Checks if the form has been submitted and all required parameters are + * set. + * @return bool + */ + protected function isSubmitComplete() + { + if (!isset($_POST['submit'])) { + return false; + } + + foreach ($this->getInputParameters() as $parameter) { + if ($parameter['required']) { + if ($parameter['file']) { + return !empty($_FILES[$parameter['name']]); + } else { + return !empty($_POST[$parameter['name']]); + } + } + } + + return true; + } + + /** + * Retrieves the submitted form values. + * @return array + */ + protected function getFormValues() + { + $input = []; + + foreach ($this->getInputParameters() as $parameter) { + if ($parameter['file'] && isset($_FILES[$parameter['name']])) { + $input[$parameter['name']] = $_FILES[$parameter['name']]; + } elseif (isset($_POST[$parameter['name']])) { + $input[$parameter['name']] = $_POST[$parameter['name']]; + } + } + + return $input; + } + + /** + * Prints out the header for the results table. + * @param string $title + * A friendly name for the results table + */ + protected function printResultsTableHeader($title) + { + printf('

%s

', $title); + print ''; + + foreach ($this->headers as $name => $value) { + printf('', $value); + } + + print ''; + $this->flush(); + } + + /** + * Prints out a row of data for the results table. + * @param array $row + * Values representing a single row in the results table + */ + protected function printResultsTableRow($row) + { + print ''; + + foreach ($this->headers as $name => $value) { + print ''; + } + + print ''; + $this->flush(); + } + + /** + * Prints out a "No results" row for the results table. + */ + protected function printNoResultsTableRow() + { + print ''; + printf( + '', + count($this->headers) + ); + print ''; + $this->flush(); + } + + /** + * Prints out the footer for the results table. + */ + protected function printResultsTableFooter() + { + print '
%s
'; + print_r($row[$name]); + print '
No results.

'; + $this->flush(); + } + + /** + * Convenience method to print an entire results table at once. + * @param string $title + * A friendly name for the results table + * @param array $rows + * Values representing multiple rows in the results table + */ + protected function printResultsTable($title, $rows) + { + $this->printResultsTableHeader($title); + + if (empty($rows)) { + $this->printNoResultsTableRow(); + } else { + foreach ($rows as $row) { + $this->printResultsTableRow($row); + } + } + + $this->printResultsTableFooter(); + } + + /** + * Flushes the contents of the output buffer. + */ + protected function flush() + { + ob_flush(); + flush(); + } +} diff --git a/php/v4/examples/Campaigns/CreateCampaign.php b/php/v4/examples/Campaigns/CreateCampaign.php new file mode 100755 index 0000000..488e7e7 --- /dev/null +++ b/php/v4/examples/Campaigns/CreateCampaign.php @@ -0,0 +1,99 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'campaign_name', + 'display' => 'Campaign Name', + 'required' => true], + ['name' => 'default_landing_page_id', + 'display' => 'Default Landing Page ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating campaign with name "%s"

', + $values['campaign_name'] + ); + + $startDate = new DateTime('today'); + $endDate = new DateTime('+1 month'); + + $campaign = new Google_Service_Dfareporting_Campaign(); + $campaign->setAdvertiserId($values['advertiser_id']); + $campaign->setDefaultLandingPageId($values['default_landing_page_id']); + $campaign->setName($values['campaign_name']); + $campaign->setStartDate($startDate->format('Y-m-d')); + $campaign->setEndDate($endDate->format('Y-m-d')); + + $result = $this->service->campaigns->insert( + $values['user_profile_id'], + $campaign + ); + + $this->printResultsTable('Campaign created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Campaign'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Campaign ID', + 'name' => 'Campaign Name']; + } +} diff --git a/php/v4/examples/Campaigns/CreateCampaignEventTag.php b/php/v4/examples/Campaigns/CreateCampaignEventTag.php new file mode 100755 index 0000000..7ae12c7 --- /dev/null +++ b/php/v4/examples/Campaigns/CreateCampaignEventTag.php @@ -0,0 +1,95 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'campaign_id', + 'display' => 'Campaign ID', + 'required' => true], + ['name' => 'event_tag_name', + 'display' => 'Event Tag Name', + 'required' => true], + ['name' => 'event_tag_url', + 'display' => 'Event Tag URL', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating campaign event tag with name "%s"

', + $values['event_tag_name'] + ); + + $eventTag = new Google_Service_Dfareporting_EventTag(); + $eventTag->setCampaignId($values['campaign_id']); + $eventTag->setName($values['event_tag_name']); + $eventTag->setStatus('ENABLED'); + $eventTag->setType('CLICK_THROUGH_EVENT_TAG'); + $eventTag->setUrl($values['event_tag_url']); + + $result = $this->service->eventTags->insert( + $values['user_profile_id'], + $eventTag + ); + + $this->printResultsTable('Campaign event tag created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Campaign Event Tag'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Event Tag ID', + 'name' => 'Event Tag Name']; + } +} diff --git a/php/v4/examples/Campaigns/GetCampaigns.php b/php/v4/examples/Campaigns/GetCampaigns.php new file mode 100755 index 0000000..6e71b1f --- /dev/null +++ b/php/v4/examples/Campaigns/GetCampaigns.php @@ -0,0 +1,91 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all campaigns

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Campaigns'); + + do { + // Create and execute the ads list request. + $response = $this->service->campaigns->listCampaigns( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getCampaigns() as $campaigns) { + $this->printResultsTableRow($campaigns); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getCampaigns()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Campaigns'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Campaign ID', + 'name' => 'Campaign Name']; + } +} diff --git a/php/v4/examples/Conversions/InsertOfflineMobileConversion.php b/php/v4/examples/Conversions/InsertOfflineMobileConversion.php new file mode 100755 index 0000000..e828ce6 --- /dev/null +++ b/php/v4/examples/Conversions/InsertOfflineMobileConversion.php @@ -0,0 +1,110 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'mobile_device_id', + 'display' => 'Mobile Device ID', + 'required' => true], + ['name' => 'floodlight_activity_id', + 'display' => 'Floodlight Activity ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Inserting offline conversion for mobile device ID "%s"

', + $values['mobile_device_id'] + ); + + $currentTimeInMicros = time() * 1000 * 1000; + + // Find Floodlight configuration ID based on provided activity ID. + $activity = $this->service->floodlightActivities->get( + $values['user_profile_id'], + $values['floodlight_activity_id'] + ); + $floodlightConfigId = $activity->getFloodlightConfigurationId(); + + $conversion = new Google_Service_Dfareporting_Conversion(); + $conversion->setMobileDeviceId($values['mobile_device_id']); + $conversion->setFloodlightActivityId($values['floodlight_activity_id']); + $conversion->setFloodlightConfigurationId($floodlightConfigId); + $conversion->setOrdinal($currentTimeInMicros); + $conversion->setTimestampMicros($currentTimeInMicros); + + $batch = new Google_Service_Dfareporting_ConversionsBatchInsertRequest(); + $batch->setConversions([$conversion]); + + $result = $this->service->conversions->batchinsert( + $values['user_profile_id'], + $batch + ); + + if (!$result->getHasFailures()) { + printf( + 'Successfully inserted conversion for mobile device ID %s.', + $values['mobile_device_id'] + ); + } else { + printf( + 'Error(s) inserting conversion for mobile device ID %s:

', + $values['mobile_device_id'] + ); + + $status = $result->getStatus()[0]; + foreach ($status->getErrors() as $error) { + printf('[%s] %s
', $error->getCode(), $error->getMessage()); + } + } + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Insert Offline Mobile Conversion'; + } +} diff --git a/php/v4/examples/Conversions/InsertOfflineUserConversion.php b/php/v4/examples/Conversions/InsertOfflineUserConversion.php new file mode 100755 index 0000000..5cb9ac4 --- /dev/null +++ b/php/v4/examples/Conversions/InsertOfflineUserConversion.php @@ -0,0 +1,125 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'encrypted_user_id', + 'display' => 'Encrypted User ID', + 'required' => true], + ['name' => 'encryption_entity_id', + 'display' => 'Encryption Entity ID', + 'required' => true], + ['name' => 'encryption_entity_type', + 'display' => 'Encryption Entity Type', + 'required' => true], + ['name' => 'encryption_source', + 'display' => 'Encryption Source', + 'required' => true], + ['name' => 'floodlight_activity_id', + 'display' => 'Floodlight Activity ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Inserting offline conversion for encrypted user ID "%s"

', + $values['encrypted_user_id'] + ); + + $currentTimeInMicros = time() * 1000 * 1000; + + // Find Floodlight configuration ID based on provided activity ID. + $activity = $this->service->floodlightActivities->get( + $values['user_profile_id'], + $values['floodlight_activity_id'] + ); + $floodlightConfigId = $activity->getFloodlightConfigurationId(); + + $conversion = new Google_Service_Dfareporting_Conversion(); + $conversion->setEncryptedUserId($values['encrypted_user_id']); + $conversion->setFloodlightActivityId($values['floodlight_activity_id']); + $conversion->setFloodlightConfigurationId($floodlightConfigId); + $conversion->setOrdinal($currentTimeInMicros); + $conversion->setTimestampMicros($currentTimeInMicros); + + $encryptionInfo = new Google_Service_Dfareporting_EncryptionInfo(); + $encryptionInfo->setEncryptionEntityId($values['encryption_entity_id']); + $encryptionInfo->setEncryptionEntityType($values['encryption_entity_type']); + $encryptionInfo->setEncryptionSource($values['encryption_source']); + + $batch = new Google_Service_Dfareporting_ConversionsBatchInsertRequest(); + $batch->setConversions([$conversion]); + $batch->setEncryptionInfo($encryptionInfo); + + $result = $this->service->conversions->batchinsert( + $values['user_profile_id'], + $batch + ); + + if (!$result->getHasFailures()) { + printf( + 'Successfully inserted conversion for encrypted user ID %s.', + $values['encrypted_user_id'] + ); + } else { + printf( + 'Error(s) inserting conversion for encrypted user ID %s:

', + $values['encrypted_user_id'] + ); + + $status = $result->getStatus()[0]; + foreach ($status->getErrors() as $error) { + printf('[%s] %s
', $error->getCode(), $error->getMessage()); + } + } + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Insert Offline User Conversion'; + } +} diff --git a/php/v4/examples/Conversions/UpdateOfflineMobileConversion.php b/php/v4/examples/Conversions/UpdateOfflineMobileConversion.php new file mode 100755 index 0000000..d079745 --- /dev/null +++ b/php/v4/examples/Conversions/UpdateOfflineMobileConversion.php @@ -0,0 +1,127 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'mobile_device_id', + 'display' => 'Mobile Device ID', + 'required' => true], + ['name' => 'floodlight_activity_id', + 'display' => 'Floodlight Activity ID', + 'required' => true], + ['name' => 'ordinal', + 'display' => 'Ordinal', + 'required' => true], + ['name' => 'timestamp', + 'display' => 'Timestamp (microseconds)', + 'required' => true], + ['name' => 'new_quantity', + 'display' => 'New Quantity', + 'required' => true], + ['name' => 'new_value', + 'display' => 'New Value', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Updating offline conversion for mobile device ID "%s"

', + $values['mobile_device_id'] + ); + + // Find Floodlight configuration ID based on provided activity ID. + $activity = $this->service->floodlightActivities->get( + $values['user_profile_id'], + $values['floodlight_activity_id'] + ); + $floodlightConfigId = $activity->getFloodlightConfigurationId(); + + // Create a conversion object with values that identify the conversion to + // update. + $conversion = new Google_Service_Dfareporting_Conversion(); + $conversion->setMobileDeviceId($values['mobile_device_id']); + $conversion->setFloodlightActivityId($values['floodlight_activity_id']); + $conversion->setFloodlightConfigurationId($floodlightConfigId); + $conversion->setOrdinal($values['ordinal']); + $conversion->setTimestampMicros($values['timestamp']); + + // Set the fields to be updated. These fields are required; to preserve a + // value from the existing conversion, it must be copied over manually. + $conversion->setQuantity($values['new_quantity']); + $conversion->setValue($values['new_value']); + + $batch = new Google_Service_Dfareporting_ConversionsBatchUpdateRequest(); + $batch->setConversions([$conversion]); + + $result = $this->service->conversions->batchupdate( + $values['user_profile_id'], + $batch + ); + + if (!$result->getHasFailures()) { + printf( + 'Successfully updated conversion for mobile device ID %s.', + $values['mobile_device_id'] + ); + } else { + printf( + 'Error(s) updating conversion for mobile device ID %s:

', + $values['mobile_device_id'] + ); + + $status = $result->getStatus()[0]; + foreach ($status->getErrors() as $error) { + printf('[%s] %s
', $error->getCode(), $error->getMessage()); + } + } + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Update Offline Mobile Conversion'; + } +} diff --git a/php/v4/examples/Conversions/UpdateOfflineUserConversion.php b/php/v4/examples/Conversions/UpdateOfflineUserConversion.php new file mode 100755 index 0000000..0fecd78 --- /dev/null +++ b/php/v4/examples/Conversions/UpdateOfflineUserConversion.php @@ -0,0 +1,142 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'encrypted_user_id', + 'display' => 'Encrypted User ID', + 'required' => true], + ['name' => 'encryption_entity_id', + 'display' => 'Encryption Entity ID', + 'required' => true], + ['name' => 'encryption_entity_type', + 'display' => 'Encryption Entity Type', + 'required' => true], + ['name' => 'encryption_source', + 'display' => 'Encryption Source', + 'required' => true], + ['name' => 'floodlight_activity_id', + 'display' => 'Floodlight Activity ID', + 'required' => true], + ['name' => 'ordinal', + 'display' => 'Ordinal', + 'required' => true], + ['name' => 'timestamp', + 'display' => 'Timestamp (microseconds)', + 'required' => true], + ['name' => 'new_quantity', + 'display' => 'New Quantity', + 'required' => true], + ['name' => 'new_value', + 'display' => 'New Value', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Updating offline conversion for encrypted user ID "%s"

', + $values['encrypted_user_id'] + ); + + // Find Floodlight configuration ID based on provided activity ID. + $activity = $this->service->floodlightActivities->get( + $values['user_profile_id'], + $values['floodlight_activity_id'] + ); + $floodlightConfigId = $activity->getFloodlightConfigurationId(); + + // Create a conversion object with values that identify the conversion to + // update. + $conversion = new Google_Service_Dfareporting_Conversion(); + $conversion->setEncryptedUserId($values['encrypted_user_id']); + $conversion->setFloodlightActivityId($values['floodlight_activity_id']); + $conversion->setFloodlightConfigurationId($floodlightConfigId); + $conversion->setOrdinal($values['ordinal']); + $conversion->setTimestampMicros($values['timestamp']); + + // Set the fields to be updated. These fields are required; to preserve a + // value from the existing conversion, it must be copied over manually. + $conversion->setQuantity($values['new_quantity']); + $conversion->setValue($values['new_value']); + + $encryptionInfo = new Google_Service_Dfareporting_EncryptionInfo(); + $encryptionInfo->setEncryptionEntityId($values['encryption_entity_id']); + $encryptionInfo->setEncryptionEntityType($values['encryption_entity_type']); + $encryptionInfo->setEncryptionSource($values['encryption_source']); + + $batch = new Google_Service_Dfareporting_ConversionsBatchUpdateRequest(); + $batch->setConversions([$conversion]); + $batch->setEncryptionInfo($encryptionInfo); + + $result = $this->service->conversions->batchupdate( + $values['user_profile_id'], + $batch + ); + + if (!$result->getHasFailures()) { + printf( + 'Successfully updated conversion for encrypted user ID %s.', + $values['encrypted_user_id'] + ); + } else { + printf( + 'Error(s) updating conversion for encrypted user ID %s:

', + $values['encrypted_user_id'] + ); + + $status = $result->getStatus()[0]; + foreach ($status->getErrors() as $error) { + printf('[%s] %s
', $error->getCode(), $error->getMessage()); + } + } + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Update Offline User Conversion'; + } +} diff --git a/php/v4/examples/CreativeAssetUtils.php b/php/v4/examples/CreativeAssetUtils.php new file mode 100755 index 0000000..4ee64bb --- /dev/null +++ b/php/v4/examples/CreativeAssetUtils.php @@ -0,0 +1,42 @@ +setName($asset['name']); + $assetId->setType($type); + + $metadata = new Google_Service_Dfareporting_CreativeAssetMetadata(); + $metadata->setAssetIdentifier($assetId); + + $result = $service->creativeAssets->insert( + $userProfileId, + $advertiserId, + $metadata, + ['data' => file_get_contents($asset['tmp_name']), + 'mimeType' => $asset['type'], + 'uploadType' => 'multipart'] + ); + + return $result; +} diff --git a/php/v4/examples/Creatives/AssignCreativeToCampaign.php b/php/v4/examples/Creatives/AssignCreativeToCampaign.php new file mode 100755 index 0000000..5b00606 --- /dev/null +++ b/php/v4/examples/Creatives/AssignCreativeToCampaign.php @@ -0,0 +1,85 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'campaign_id', + 'display' => 'Campaign ID', + 'required' => true], + ['name' => 'creative_id', + 'display' => 'Creative ID', + 'required' => true]]; + } + + /** + * {@inheritdoc} + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Assigning creative ID %s to campaign ID %s

', + $values['creative_id'], + $values['campaign_id'] + ); + + $association = + new Google_Service_Dfareporting_CampaignCreativeAssociation(); + $association->setCreativeId($values['creative_id']); + + $result = $this->service->campaignCreativeAssociations->insert( + $values['user_profile_id'], + $values['campaign_id'], + $association + ); + + printf( + 'Successfully associated creative ID %s with campaign ID %s.', + $result->getCreativeId(), + $values['campaign_id'] + ); + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Assign Creative To Campaign'; + } +} diff --git a/php/v4/examples/Creatives/CreateCreativeField.php b/php/v4/examples/Creatives/CreateCreativeField.php new file mode 100755 index 0000000..c11e551 --- /dev/null +++ b/php/v4/examples/Creatives/CreateCreativeField.php @@ -0,0 +1,90 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'field_name', + 'display' => 'Creative Field Name', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating creative field with name "%s"

', + $values['field_name'] + ); + + $field = new Google_Service_Dfareporting_CreativeField(); + $field->setAdvertiserId($values['advertiser_id']); + $field->setName($values['field_name']); + + $result = $this->service->creativeFields->insert( + $values['user_profile_id'], + $field + ); + + $this->printResultsTable('Creative field created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Creative Field'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative Field ID', + 'name' => 'Creative Field Name']; + } +} diff --git a/php/v4/examples/Creatives/CreateCreativeFieldValue.php b/php/v4/examples/Creatives/CreateCreativeFieldValue.php new file mode 100755 index 0000000..f95586d --- /dev/null +++ b/php/v4/examples/Creatives/CreateCreativeFieldValue.php @@ -0,0 +1,90 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'creative_field_id', + 'display' => 'Creative Field ID', + 'required' => true], + ['name' => 'field_value', + 'display' => 'Creative Field Value', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating creative field value with value "%s"

', + $values['field_value'] + ); + + $fieldValue = new Google_Service_Dfareporting_CreativeFieldValue(); + $fieldValue->setValue($values['field_value']); + + $result = $this->service->creativeFieldValues->insert( + $values['user_profile_id'], + $values['creative_field_id'], + $fieldValue + ); + + $this->printResultsTable('Creative field value created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Creative Field Value'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative Field Value ID', + 'value' => 'Creative Field Value']; + } +} diff --git a/php/v4/examples/Creatives/CreateCreativeGroup.php b/php/v4/examples/Creatives/CreateCreativeGroup.php new file mode 100755 index 0000000..1acea4b --- /dev/null +++ b/php/v4/examples/Creatives/CreateCreativeGroup.php @@ -0,0 +1,96 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'group_name', + 'display' => 'Creative Group Name', + 'required' => true], + ['name' => 'group_number', + 'display' => 'Creative Group Number (1-2)', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating creative group with name "%s"

', + $values['group_name'] + ); + + $group = new Google_Service_Dfareporting_CreativeGroup(); + $group->setAdvertiserId($values['advertiser_id']); + $group->setGroupNumber($values['group_number']); + $group->setName($values['group_name']); + + $result = $this->service->creativeGroups->insert( + $values['user_profile_id'], + $group + ); + + $this->printResultsTable('Creative group created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Creative Group'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative Group ID', + 'name' => 'Creative Group Name', + 'groupNumber' => 'Creative Group Number']; + } +} diff --git a/php/v4/examples/Creatives/CreateDisplayImageGalleryCreative.php b/php/v4/examples/Creatives/CreateDisplayImageGalleryCreative.php new file mode 100755 index 0000000..e92888f --- /dev/null +++ b/php/v4/examples/Creatives/CreateDisplayImageGalleryCreative.php @@ -0,0 +1,151 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'size_id', + 'display' => 'Size ID', + 'required' => true], + ['name' => 'image1_asset_file', + 'display' => 'First Image Asset File', + 'file' => true, + 'required' => true], + ['name' => 'image2_asset_file', + 'display' => 'Second Image Asset File', + 'file' => true, + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating display image gallery creative from "%s" and "%s"

', + $values['image1_asset_file']['name'], + $values['image2_asset_file']['name'] + ); + + $creative = new Google_Service_Dfareporting_Creative(); + $creative->setAdvertiserId($values['advertiser_id']); + $creative->setAutoAdvanceImages(true); + $creative->setName('Test display image gallery creative'); + $creative->setType('DISPLAY_IMAGE_GALLERY'); + + $size = new Google_Service_Dfareporting_Size(); + $size->setId($values['size_id']); + $creative->setSize($size); + + // Upload the first image asset. + $image1 = uploadAsset( + $this->service, + $values['user_profile_id'], + $values['advertiser_id'], + $values['image1_asset_file'], + 'HTML_IMAGE' + ); + + $image1Asset = new Google_Service_Dfareporting_CreativeAsset(); + $image1Asset->setAssetIdentifier($image1->getAssetIdentifier()); + $image1Asset->setRole('PRIMARY'); + + // Upload the second image asset. + $image2 = uploadAsset( + $this->service, + $values['user_profile_id'], + $values['advertiser_id'], + $values['image2_asset_file'], + 'HTML_IMAGE' + ); + + $image2Asset = new Google_Service_Dfareporting_CreativeAsset(); + $image2Asset->setAssetIdentifier($image2->getAssetIdentifier()); + $image2Asset->setRole('PRIMARY'); + + // Add the creative assets. + $creative->setCreativeAssets([$image1Asset, $image2Asset]); + + // Add a click tag for the first image asset. + $clickTag1 = new Google_Service_Dfareporting_ClickTag(); + $clickTag1->setName($image1->getAssetIdentifier()->getName()); + $clickTag1->setEventName($image1->getAssetIdentifier()->getName()); + + // Add a click tag for the second image asset. + $clickTag2 = new Google_Service_Dfareporting_ClickTag(); + $clickTag2->setName($image2->getAssetIdentifier()->getName()); + $clickTag2->setEventName($image2->getAssetIdentifier()->getName()); + + $creative->setClickTags([$clickTag1, $clickTag2]); + + $result = $this->service->creatives->insert( + $values['user_profile_id'], + $creative + ); + + $this->printResultsTable( + 'Display image gallery creative created.', + [$result] + ); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Display Image Gallery Creative'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative ID', + 'name' => 'Creative Name', + 'type' => 'Creative type']; + } +} diff --git a/php/v4/examples/Creatives/CreateDisplayRedirectCreative.php b/php/v4/examples/Creatives/CreateDisplayRedirectCreative.php new file mode 100755 index 0000000..eae882a --- /dev/null +++ b/php/v4/examples/Creatives/CreateDisplayRedirectCreative.php @@ -0,0 +1,100 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'image_url', + 'display' => 'Image File URL', + 'required' => true], + ['name' => 'size_id', + 'display' => 'Size ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating display redirect creative from image URL "%s"

', + $values['image_url'] + ); + + $creative = new Google_Service_Dfareporting_Creative(); + $creative->setAdvertiserId($values['advertiser_id']); + $creative->setName('Test display redirect creative'); + $creative->setRedirectUrl($values['image_url']); + $creative->setType('DISPLAY_REDIRECT'); + + $size = new Google_Service_Dfareporting_Size(); + $size->setId($values['size_id']); + $creative->setSize($size); + + $result = $this->service->creatives->insert( + $values['user_profile_id'], + $creative + ); + + $this->printResultsTable('Display redirect creative created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Display Redirect Creative'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative ID', + 'name' => 'Creative Name', + 'type' => 'Creative type']; + } +} diff --git a/php/v4/examples/Creatives/CreateHTML5DisplayCreative.php b/php/v4/examples/Creatives/CreateHTML5DisplayCreative.php new file mode 100755 index 0000000..402553f --- /dev/null +++ b/php/v4/examples/Creatives/CreateHTML5DisplayCreative.php @@ -0,0 +1,158 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'size_id', + 'display' => 'Size ID', + 'required' => true], + ['name' => 'landing_page_id', + 'display' => 'Landing Page ID', + 'required' => true], + ['name' => 'html_asset_file', + 'display' => 'HTML5 Asset File', + 'file' => true, + 'required' => true], + ['name' => 'image_asset_file', + 'display' => 'Backup Image Asset File', + 'file' => true, + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating HTML5 display creative from HTML5 asset "%s"

', + $values['html_asset_file']['name'] + ); + + $creative = new Google_Service_Dfareporting_Creative(); + $creative->setAdvertiserId($values['advertiser_id']); + $creative->setAutoAdvanceImages(true); + $creative->setName('Test HTML5 display creative'); + $creative->setType('DISPLAY'); + + $size = new Google_Service_Dfareporting_Size(); + $size->setId($values['size_id']); + $creative->setSize($size); + + // Upload the HTML5 asset. + $html = uploadAsset( + $this->service, + $values['user_profile_id'], + $values['advertiser_id'], + $values['html_asset_file'], + 'HTML' + ); + + $htmlAsset = new Google_Service_Dfareporting_CreativeAsset(); + $htmlAsset->setAssetIdentifier($html->getAssetIdentifier()); + $htmlAsset->setRole('PRIMARY'); + + // Upload the backup image asset. + $image = uploadAsset( + $this->service, + $values['user_profile_id'], + $values['advertiser_id'], + $values['image_asset_file'], + 'HTML_IMAGE' + ); + + $imageAsset = new Google_Service_Dfareporting_CreativeAsset(); + $imageAsset->setAssetIdentifier($image->getAssetIdentifier()); + $imageAsset->setRole('BACKUP_IMAGE'); + + // Add the creative assets. + $creative->setCreativeAssets([$htmlAsset, $imageAsset]); + + // Configure the default click-through URL. + $clickThroughUrl = + new Google_Service_Dfareporting_CreativeClickThroughUrl(); + $clickThroughUrl->setLandingPageId($values['landing_page_id']); + + // Configure the backup image. + $creative->setBackupImageClickThroughUrl($clickThroughUrl); + $creative->setBackupImageReportingLabel('backup'); + + $targetWindow = new Google_Service_Dfareporting_TargetWindow(); + $targetWindow->setTargetWindowOption('NEW_WINDOW'); + $creative->setBackupImageTargetWindow($targetWindow); + + // Add a click tag. + $clickTag = new Google_Service_Dfareporting_ClickTag(); + $clickTag->setName('clickTag'); + $clickTag->setEventName('exit'); + $clickTag->setClickThroughUrl($clickThroughUrl); + $creative->setClickTags([$clickTag]); + + $result = $this->service->creatives->insert( + $values['user_profile_id'], + $creative + ); + + $this->printResultsTable('HTML5 display creative created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create HTML5 Display Creative'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative ID', + 'name' => 'Creative Name', + 'type' => 'Creative type']; + } +} diff --git a/php/v4/examples/Creatives/CreateImageDisplayCreative.php b/php/v4/examples/Creatives/CreateImageDisplayCreative.php new file mode 100755 index 0000000..8c1fab9 --- /dev/null +++ b/php/v4/examples/Creatives/CreateImageDisplayCreative.php @@ -0,0 +1,117 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'size_id', + 'display' => 'Size ID', + 'required' => true], + ['name' => 'asset_file', + 'display' => 'Image Asset File', + 'file' => true, + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating image display creative from image asset "%s"

', + $values['asset_file']['name'] + ); + + $creative = new Google_Service_Dfareporting_Creative(); + $creative->setAdvertiserId($values['advertiser_id']); + $creative->setName('Test image display creative'); + $creative->setType('DISPLAY'); + + $size = new Google_Service_Dfareporting_Size(); + $size->setId($values['size_id']); + $creative->setSize($size); + + // Upload the image asset. + $image = uploadAsset( + $this->service, + $values['user_profile_id'], + $values['advertiser_id'], + $values['asset_file'], + 'HTML_IMAGE' + ); + + $asset = new Google_Service_Dfareporting_CreativeAsset(); + $asset->setAssetIdentifier($image->getAssetIdentifier()); + $asset->setRole('PRIMARY'); + + // Add the creative asset. + $creative->setCreativeAssets([$asset]); + + $result = $this->service->creatives->insert( + $values['user_profile_id'], + $creative + ); + + $this->printResultsTable('Image display creative created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Image Display Creative'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative ID', + 'name' => 'Creative Name', + 'type' => 'Creative type']; + } +} diff --git a/php/v4/examples/Creatives/CreateInstreamAudioCreative.php b/php/v4/examples/Creatives/CreateInstreamAudioCreative.php new file mode 100755 index 0000000..04e15c6 --- /dev/null +++ b/php/v4/examples/Creatives/CreateInstreamAudioCreative.php @@ -0,0 +1,114 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'asset_file', + 'display' => 'Audio Asset File', + 'file' => true, + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating in-stream audio creative from audio asset "%s"

', + $values['asset_file']['name'] + ); + + $creative = new Google_Service_Dfareporting_Creative(); + $creative->setAdvertiserId($values['advertiser_id']); + $creative->setName('Test in-stream audio creative'); + $creative->setType('INSTREAM_AUDIO'); + + $size = new Google_Service_Dfareporting_Size(); + $size->setId($values['size_id']); + $creative->setSize($size); + + // Upload the audio asset. + $audio = uploadAsset( + $this->service, + $values['user_profile_id'], + $values['advertiser_id'], + $values['asset_file'], + 'AUDIO' + ); + + $asset = new Google_Service_Dfareporting_CreativeAsset(); + $asset->setAssetIdentifier($audio->getAssetIdentifier()); + $asset->setRole('PARENT_AUDIO'); + + // Add the creative asset. + $creative->setCreativeAssets([$asset]); + + $result = $this->service->creatives->insert( + $values['user_profile_id'], + $creative + ); + + $this->printResultsTable('In-stream audio creative created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create In-stream Audio Creative'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative ID', + 'name' => 'Creative Name', + 'type' => 'Creative type']; + } +} diff --git a/php/v4/examples/Creatives/CreateInstreamVideoCreative.php b/php/v4/examples/Creatives/CreateInstreamVideoCreative.php new file mode 100755 index 0000000..7a77d66 --- /dev/null +++ b/php/v4/examples/Creatives/CreateInstreamVideoCreative.php @@ -0,0 +1,114 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'asset_file', + 'display' => 'Video Asset File', + 'file' => true, + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating in-stream video creative from video asset "%s"

', + $values['asset_file']['name'] + ); + + $creative = new Google_Service_Dfareporting_Creative(); + $creative->setAdvertiserId($values['advertiser_id']); + $creative->setName('Test in-stream video creative'); + $creative->setType('INSTREAM_VIDEO'); + + $size = new Google_Service_Dfareporting_Size(); + $size->setId($values['size_id']); + $creative->setSize($size); + + // Upload the video asset. + $video = uploadAsset( + $this->service, + $values['user_profile_id'], + $values['advertiser_id'], + $values['asset_file'], + 'VIDEO' + ); + + $asset = new Google_Service_Dfareporting_CreativeAsset(); + $asset->setAssetIdentifier($video->getAssetIdentifier()); + $asset->setRole('PARENT_VIDEO'); + + // Add the creative asset. + $creative->setCreativeAssets([$asset]); + + $result = $this->service->creatives->insert( + $values['user_profile_id'], + $creative + ); + + $this->printResultsTable('In-stream video creative created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create In-stream Video Creative'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative ID', + 'name' => 'Creative Name', + 'type' => 'Creative type']; + } +} diff --git a/php/v4/examples/Creatives/CreateTrackingCreative.php b/php/v4/examples/Creatives/CreateTrackingCreative.php new file mode 100755 index 0000000..5773d6c --- /dev/null +++ b/php/v4/examples/Creatives/CreateTrackingCreative.php @@ -0,0 +1,88 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating tracking creative for advertiser ID %s

', + $values['advertiser_id'] + ); + + $creative = new Google_Service_Dfareporting_Creative(); + $creative->setAdvertiserId($values['advertiser_id']); + $creative->setName('Test tracking creative'); + $creative->setType('TRACKING_TEXT'); + + $result = $this->service->creatives->insert( + $values['user_profile_id'], + $creative + ); + + $this->printResultsTable('Tracking creative created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Tracking Creative'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative ID', + 'name' => 'Creative Name', + 'type' => 'Creative type']; + } +} diff --git a/php/v4/examples/Creatives/GetCreativeFieldValues.php b/php/v4/examples/Creatives/GetCreativeFieldValues.php new file mode 100755 index 0000000..c6eed53 --- /dev/null +++ b/php/v4/examples/Creatives/GetCreativeFieldValues.php @@ -0,0 +1,100 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'field_id', + 'display' => 'Creative Field ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Listing all creative field values for creative field ID %s

', + $values['field_id'] + ); + + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Creative Field Values'); + + do { + // Create and execute the creative field values list request. + $response = $this->service->creativeFieldValues->listCreativeFieldValues( + $values['user_profile_id'], + $values['field_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getCreativeFieldValues() as $values) { + $this->printResultsTableRow($values); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getCreativeFieldValues()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Creative Field Values'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative Field Value ID', + 'value' => 'Creative Field Value']; + } +} diff --git a/php/v4/examples/Creatives/GetCreativeFields.php b/php/v4/examples/Creatives/GetCreativeFields.php new file mode 100755 index 0000000..6c8ff8a --- /dev/null +++ b/php/v4/examples/Creatives/GetCreativeFields.php @@ -0,0 +1,91 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all creative fields

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Creative Fields'); + + do { + // Create and execute the creative fields list request. + $response = $this->service->creativeFields->listCreativeFields( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getCreativeFields() as $fields) { + $this->printResultsTableRow($fields); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getCreativeFields()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Creative Fields'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative Field ID', + 'name' => 'Creative Field Name']; + } +} diff --git a/php/v4/examples/Creatives/GetCreativeGroups.php b/php/v4/examples/Creatives/GetCreativeGroups.php new file mode 100755 index 0000000..b2601d1 --- /dev/null +++ b/php/v4/examples/Creatives/GetCreativeGroups.php @@ -0,0 +1,91 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all creative groups

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Creative Groups'); + + do { + // Create and execute the creative fields list request. + $response = $this->service->creativeGroups->listCreativeGroups( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getCreativeGroups() as $groups) { + $this->printResultsTableRow($groups); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getCreativeGroups()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Creative Groups'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative Group ID', + 'name' => 'Creative Group Name']; + } +} diff --git a/php/v4/examples/Creatives/GetCreatives.php b/php/v4/examples/Creatives/GetCreatives.php new file mode 100755 index 0000000..f7fc68a --- /dev/null +++ b/php/v4/examples/Creatives/GetCreatives.php @@ -0,0 +1,101 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Listing all active creatives for advertiser ID %s

', + $values['advertiser_id'] + ); + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Creatives'); + + do { + // Create and execute the creative fields list request. + $response = $this->service->creatives->listCreatives( + $values['user_profile_id'], + ['active' => true, + 'advertiserId' => $values['advertiser_id'], + 'pageToken' => $pageToken] + ); + + foreach ($response->getCreatives() as $creatives) { + $this->printResultsTableRow($creatives); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getCreatives()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Creatives'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Creative ID', + 'name' => 'Creative Name', + 'type' => 'Creative Type']; + } +} diff --git a/php/v4/examples/DimensionValues/GetDimensionValues.php b/php/v4/examples/DimensionValues/GetDimensionValues.php new file mode 100755 index 0000000..2b4b9cc --- /dev/null +++ b/php/v4/examples/DimensionValues/GetDimensionValues.php @@ -0,0 +1,96 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'dimension_name', + 'display' => 'Dimension Name', + 'required' => true], + ['name' => 'start_date', + 'display' => 'Start Date (yyyy-MM-dd)', + 'required' => true], + ['name' => 'end_date', + 'display' => 'End Date (yyyy-MM-dd)', + 'required' => true]]; + } + + /** + * {@inheritdoc} + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + $dimensionValueRequest = + new Google_Service_Dfareporting_DimensionValueRequest(); + $dimensionValueRequest->setDimensionName($values['dimension_name']); + $dimensionValueRequest->setStartDate($values['start_date']); + $dimensionValueRequest->setEndDate($values['end_date']); + + $dimensionValues = $this->service->dimensionValues->query( + $values['user_profile_id'], + $dimensionValueRequest + ); + + printf('

Listing available %s values

', $values['dimension_name']); + + $this->printResultsTable('Dimension Values', $dimensionValues['items']); + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get Dimension Values'; + } + + /** + * {@inheritdoc} + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['value' => 'Dimension Value', + 'id' => 'Dimension ID']; + } +} diff --git a/php/v4/examples/Files/CheckFileStatus.php b/php/v4/examples/Files/CheckFileStatus.php new file mode 100755 index 0000000..cc151bb --- /dev/null +++ b/php/v4/examples/Files/CheckFileStatus.php @@ -0,0 +1,89 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'report_id', + 'display' => 'Report ID', + 'required' => true], + ['name' => 'report_file_id', + 'display' => 'Report File ID', + 'required' => true]]; + } + + /** + * {@inheritdoc} + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Getting status of report file with ID %s

', + $values['report_file_id'] + ); + + $reportFile = $this->service->reports_files->get( + $values['user_profile_id'], + $values['report_id'], + $values['report_file_id'] + ); + + $this->printResultsTable('File status', [$reportFile]); + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Check File Generation Status'; + } + + /** + * {@inheritdoc} + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'File ID', + 'fileName' => 'File Name', + 'reportId' => 'Report ID', + 'format' => 'File Format', + 'status' => 'Status']; + } +} diff --git a/php/v4/examples/Files/GetAllFiles.php b/php/v4/examples/Files/GetAllFiles.php new file mode 100755 index 0000000..fcd6614 --- /dev/null +++ b/php/v4/examples/Files/GetAllFiles.php @@ -0,0 +1,92 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * {@inheritdoc} + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all report files

'; + + $this->printResultsTableHeader('Files'); + + do { + $files = $this->service->files->listFiles( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($files['items'] as $file) { + $url = $file['urls']['browserUrl']; + $file['download'] = 'Link'; + $this->printResultsTableRow($file); + } + + $pageToken = $files['nextPageToken']; + } while (!empty($files['items']) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Files'; + } + + /** + * {@inheritdoc} + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'File ID', + 'fileName' => 'File Name', + 'reportId' => 'Report ID', + 'format' => 'File Format', + 'status' => 'Status', + 'download' => 'Download']; + } +} diff --git a/php/v4/examples/Floodlights/CreateFloodlightActivity.php b/php/v4/examples/Floodlights/CreateFloodlightActivity.php new file mode 100755 index 0000000..3ce173f --- /dev/null +++ b/php/v4/examples/Floodlights/CreateFloodlightActivity.php @@ -0,0 +1,98 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'activity_group_id', + 'display' => 'Floodlight Activity Group ID', + 'required' => true], + ['name' => 'activity_name', + 'display' => 'Floodlight Activity Name', + 'required' => true], + ['name' => 'url', + 'display' => 'Expected URL', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating floodlight activity with name "%s" under group ID' + . ' %s

', + $values['activity_name'], + $values['activity_group_id'] + ); + + $activity = new Google_Service_Dfareporting_FloodlightActivity(); + $activity->setCountingMethod('STANDARD_COUNTING'); + $activity->setExpectedUrl($values['url']); + $activity->setFloodlightActivityGroupId($values['activity_group_id']); + $activity->setFloodlightTagType('GLOBAL_SITE_TAG'); + $activity->setName($values['activity_name']); + + $result = $this->service->floodlightActivities->insert( + $values['user_profile_id'], + $activity + ); + + $this->printResultsTable('Floodlight activity created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Floodlight Activity'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Floodlight Activity ID', + 'name' => 'Floodlight Activity Name']; + } +} diff --git a/php/v4/examples/Floodlights/CreateFloodlightActivityGroup.php b/php/v4/examples/Floodlights/CreateFloodlightActivityGroup.php new file mode 100755 index 0000000..83908aa --- /dev/null +++ b/php/v4/examples/Floodlights/CreateFloodlightActivityGroup.php @@ -0,0 +1,92 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'group_name', + 'display' => 'Floodlight Activity Group Name', + 'required' => true], + ['name' => 'configuration_id', + 'display' => 'Floodlight Configuration (Advertiser) ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating floodlight activity group with name "%s"

', + $values['group_name'] + ); + + $group = new Google_Service_Dfareporting_FloodlightActivityGroup(); + $group->setFloodlightConfigurationId($values['configuration_id']); + $group->setName($values['group_name']); + $group->setType('COUNTER'); + + $result = $this->service->floodlightActivityGroups->insert( + $values['user_profile_id'], + $group + ); + + $this->printResultsTable('Floodlight activity group created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Floodlight Activity Group'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Floodlight Activity Group ID', + 'name' => 'Floodlight Activity Group Name', + 'floodlightConfigurationId' => 'Floodlight Configuration ID']; + } +} diff --git a/php/v4/examples/Floodlights/DownloadFloodlightTags.php b/php/v4/examples/Floodlights/DownloadFloodlightTags.php new file mode 100755 index 0000000..52ba606 --- /dev/null +++ b/php/v4/examples/Floodlights/DownloadFloodlightTags.php @@ -0,0 +1,92 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'activity_id', + 'display' => 'Floodlight Activity ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Downloading floodlight activity tags for activity ID %s

', + $values['activity_id'] + ); + + $result = $this->service->floodlightActivities->generatetag( + $values['user_profile_id'], + ['floodlightActivityId' => $values['activity_id']] + ); + + // Prepare the tag for display + $activityTag = ''; + if (!is_null($result->getGlobalSiteTagGlobalSnippet())) { + // This is a global site tag, display both the global and event snippets. + $activityTag = sprintf( + "Global site tag global snippet:\n\n%s", + $result->getGlobalSiteTagGlobalSnippet() + ); + $activityTag .= sprintf( + "\n\nGlobal site tag event snippet:\n\n%s", + $result->getFloodlightActivityTag() + ); + } else { + // This is an image or iframe tag. + $activityTag = sprintf( + "Floodlight activity tag:\n\n%s", + $result->getFloodlightActivityTag() + ); + } + + // Display the tag + print str_replace(["\r\n", "\n"], '
', htmlentities($activityTag)); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Download Floodlight Activity Tags'; + } +} diff --git a/php/v4/examples/Floodlights/GetFloodlightActivities.php b/php/v4/examples/Floodlights/GetFloodlightActivities.php new file mode 100755 index 0000000..e9753cc --- /dev/null +++ b/php/v4/examples/Floodlights/GetFloodlightActivities.php @@ -0,0 +1,102 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Listing all floodlight activities for advertiser ID %s

', + $values['advertiser_id'] + ); + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Floodlight Activities'); + + do { + // Create and execute the floodlight activities list request. + $response = + $this->service->floodlightActivities->listFloodlightActivities( + $values['user_profile_id'], + ['advertiserId' => $values['advertiser_id'], + 'pageToken' => $pageToken] + ); + + foreach ($response->getFloodlightActivities() as $activities) { + $this->printResultsTableRow($activities); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getFloodlightActivities()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Floodlight Activities'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Floodlight Activity ID', + 'name' => 'Floodlight Activity Name', + 'floodlightConfigurationId' => 'Floodlight Configuration ID']; + } +} diff --git a/php/v4/examples/Floodlights/GetFloodlightActivityGroups.php b/php/v4/examples/Floodlights/GetFloodlightActivityGroups.php new file mode 100755 index 0000000..61e4c60 --- /dev/null +++ b/php/v4/examples/Floodlights/GetFloodlightActivityGroups.php @@ -0,0 +1,103 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Listing all floodlight activity groups for advertiser ID %s

', + $values['advertiser_id'] + ); + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Floodlight Activity Groups'); + + $activityGroupService = $this->service->floodlightActivityGroups; + + do { + // Create and execute the floodlight activity groups list request. + $response = $activityGroupService->listFloodlightActivityGroups( + $values['user_profile_id'], + ['advertiserId' => $values['advertiser_id'], + 'pageToken' => $pageToken] + ); + + foreach ($response->getFloodlightActivityGroups() as $groups) { + $this->printResultsTableRow($groups); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getFloodlightActivityGroups()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Floodlight Activity Groups'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Floodlight Activity Group ID', + 'name' => 'Floodlight Activity Group Name', + 'floodlightConfigurationId' => 'Floodlight Configuration ID']; + } +} diff --git a/php/v4/examples/Misc/GetChangeLogsForAdvertiser.php b/php/v4/examples/Misc/GetChangeLogsForAdvertiser.php new file mode 100755 index 0000000..df5ca72 --- /dev/null +++ b/php/v4/examples/Misc/GetChangeLogsForAdvertiser.php @@ -0,0 +1,104 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Listing all change logs for advertiser ID %s

', + $values['advertiser_id'] + ); + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Change Logs'); + + do { + // Create and execute the change logs list request. + $response = $this->service->changeLogs->listChangeLogs( + $values['user_profile_id'], + ['objectIds' => [$values['advertiser_id']], + 'objectType' => 'OBJECT_ADVERTISER', + 'pageToken' => $pageToken] + ); + + foreach ($response->getChangeLogs() as $log) { + $this->printResultsTableRow($log); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getChangeLogs()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Change Logs For Advertiser'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['action' => 'Action', + 'fieldName' => 'Field', + 'oldValue' => 'Old Value', + 'newValue' => 'New Value']; + } +} diff --git a/php/v4/examples/Misc/GetSites.php b/php/v4/examples/Misc/GetSites.php new file mode 100755 index 0000000..67fd068 --- /dev/null +++ b/php/v4/examples/Misc/GetSites.php @@ -0,0 +1,91 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all sites

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Sites'); + + do { + // Create and execute the sites list request. + $response = $this->service->sites->listSites( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getSites() as $sites) { + $this->printResultsTableRow($sites); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getSites()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Sites'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Site ID', + 'keyName' => 'Site Key Name']; + } +} diff --git a/php/v4/examples/Misc/GetSize.php b/php/v4/examples/Misc/GetSize.php new file mode 100755 index 0000000..18743c4 --- /dev/null +++ b/php/v4/examples/Misc/GetSize.php @@ -0,0 +1,94 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'width', + 'display' => 'Width (px)', + 'required' => true], + ['name' => 'height', + 'display' => 'Height (px)', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Listing sizes matching %sx%s

', + $values['width']. $values['height'] + ); + + $this->printResultsTableHeader('Sizes'); + + // Create and execute the size list request. + $response = $this->service->sizes->listSizes( + $values['user_profile_id'], + ['height' => $values['height'], + 'width' => $values['width']] + ); + + foreach ($response->getSizes() as $sizes) { + $this->printResultsTableRow($sizes); + } + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get Size'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Size ID', + 'width' => 'Width', + 'height' => 'Height']; + } +} diff --git a/php/v4/examples/Placements/CreateContentCategory.php b/php/v4/examples/Placements/CreateContentCategory.php new file mode 100755 index 0000000..20f56ff --- /dev/null +++ b/php/v4/examples/Placements/CreateContentCategory.php @@ -0,0 +1,85 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'category_name', + 'display' => 'Content Category Name', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating content category with name "%s"

', + $values['category_name'] + ); + + $category = new Google_Service_Dfareporting_ContentCategory(); + $category->setName($values['category_name']); + + $result = $this->service->contentCategories->insert( + $values['user_profile_id'], + $category + ); + + $this->printResultsTable('Content category created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Content Category'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Content Category ID', + 'name' => 'Content Category Name']; + } +} diff --git a/php/v4/examples/Placements/CreatePlacement.php b/php/v4/examples/Placements/CreatePlacement.php new file mode 100755 index 0000000..f9a38d5 --- /dev/null +++ b/php/v4/examples/Placements/CreatePlacement.php @@ -0,0 +1,122 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'campaign_id', + 'display' => 'Campaign ID', + 'required' => true], + ['name' => 'site_id', + 'display' => 'Site ID', + 'required' => true], + ['name' => 'size_id', + 'display' => 'Size ID', + 'required' => true], + ['name' => 'placement_name', + 'display' => 'Placement Name', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating placement with name "%s" under campaign ID %s

', + $values['placement_name'], + $values['campaign_id'] + ); + + // Retrieve the campaign. + $campaign = $this->service->campaigns->get( + $values['user_profile_id'], + $values['campaign_id'] + ); + + $placement = new Google_Service_Dfareporting_Placement(); + $placement->setCampaignId($values['campaign_id']); + $placement->setCompatibility('DISPLAY'); + $placement->setName($values['placement_name']); + $placement->setPaymentSource('PLACEMENT_AGENCY_PAID'); + $placement->setSiteId($values['site_id']); + $placement->setTagFormats(['PLACEMENT_TAG_STANDARD']); + + // Set the size of the placement. + $size = new Google_Service_Dfareporting_Size(); + $size->setId($values['size_id']); + $placement->setSize($size); + + // Set the pricing schedule for the placement. + $pricingSchedule = new Google_Service_Dfareporting_PricingSchedule(); + $pricingSchedule->setEndDate($campaign->getEndDate()); + $pricingSchedule->setPricingType('PRICING_TYPE_CPM'); + $pricingSchedule->setStartDate($campaign->getStartDate()); + $placement->setPricingSchedule($pricingSchedule); + + // Insert the placement. + $result = $this->service->placements->insert( + $values['user_profile_id'], + $placement + ); + + $this->printResultsTable('Placement created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Placement'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Placement ID', + 'name' => 'Placement Name']; + } +} diff --git a/php/v4/examples/Placements/CreatePlacementGroup.php b/php/v4/examples/Placements/CreatePlacementGroup.php new file mode 100755 index 0000000..72744de --- /dev/null +++ b/php/v4/examples/Placements/CreatePlacementGroup.php @@ -0,0 +1,111 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'campaign_id', + 'display' => 'Campaign ID', + 'required' => true], + ['name' => 'site_id', + 'display' => 'Site ID', + 'required' => true], + ['name' => 'group_name', + 'display' => 'Placement Group Name', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating placement group with name "%s" under campaign ID %s

', + $values['group_name'], + $values['campaign_id'] + ); + + // Retrieve the campaign. + $campaign = $this->service->campaigns->get( + $values['user_profile_id'], + $values['campaign_id'] + ); + + $group = new Google_Service_Dfareporting_PlacementGroup(); + $group->setCampaignId($values['campaign_id']); + $group->setName($values['group_name']); + $group->setPlacementGroupType('PLACEMENT_PACKAGE'); + $group->setSiteId($values['site_id']); + + // Set the pricing schedule for the placement group. + $pricingSchedule = new Google_Service_Dfareporting_PricingSchedule(); + $pricingSchedule->setEndDate($campaign->getEndDate()); + $pricingSchedule->setPricingType('PRICING_TYPE_CPM'); + $pricingSchedule->setStartDate($campaign->getStartDate()); + $group->setPricingSchedule($pricingSchedule); + + // Insert the placement group. + $result = $this->service->placementGroups->insert( + $values['user_profile_id'], + $group + ); + + $this->printResultsTable('Placement group created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Placement Group'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Placement Group ID', + 'name' => 'Placement Group Name']; + } +} diff --git a/php/v4/examples/Placements/CreatePlacementStrategy.php b/php/v4/examples/Placements/CreatePlacementStrategy.php new file mode 100755 index 0000000..7d632ed --- /dev/null +++ b/php/v4/examples/Placements/CreatePlacementStrategy.php @@ -0,0 +1,85 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'strategy_name', + 'display' => 'Placement Strategy Name', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating placement strategy with name "%s"

', + $values['strategy_name'] + ); + + $strategy = new Google_Service_Dfareporting_PlacementStrategy(); + $strategy->setName($values['strategy_name']); + + $result = $this->service->placementStrategies->insert( + $values['user_profile_id'], + $strategy + ); + + $this->printResultsTable('Placement strategy created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Placement Strategy'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Placement Strategy ID', + 'name' => 'Placement Strategy Name']; + } +} diff --git a/php/v4/examples/Placements/DownloadPlacementTags.php b/php/v4/examples/Placements/DownloadPlacementTags.php new file mode 100755 index 0000000..9ad063f --- /dev/null +++ b/php/v4/examples/Placements/DownloadPlacementTags.php @@ -0,0 +1,106 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'campaign_id', + 'display' => 'Campaign ID', + 'required' => true], + ['name' => 'placement_id', + 'display' => 'Placement ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Downloading placement tags for placement ID %s

', + $values['placement_id'] + ); + + $placementTags = $this->service->placements->generatetags( + $values['user_profile_id'], + ['campaignId' => $values['campaign_id'], + 'placementIds' => [$values['placement_id']], + 'tagFormats' => ['PLACEMENT_TAG_STANDARD', + 'PLACEMENT_TAG_IFRAME_JAVASCRIPT', + 'PLACEMENT_TAG_INTERNAL_REDIRECT'] + ] + ); + + $this->printResultsTableHeader('Placement Tags'); + + foreach ($placementTags['placementTags'] as $placementTag) { + foreach ($placementTag['tagDatas'] as $tagData) { + $result = ['clickTag' => htmlspecialchars($tagData->getClickTag()), + 'format' => $tagData->getFormat(), + 'impressionTag' => + htmlspecialchars($tagData->getImpressionTag())]; + + $this->printResultsTableRow($result); + } + } + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Download Placement Tags'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['format' => 'Format', + 'impressionTag' => 'Impression Tag', + 'clickTag' => 'Click Tag']; + } +} diff --git a/php/v4/examples/Placements/GetContentCategories.php b/php/v4/examples/Placements/GetContentCategories.php new file mode 100755 index 0000000..3c13454 --- /dev/null +++ b/php/v4/examples/Placements/GetContentCategories.php @@ -0,0 +1,91 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all content categories

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Content Categories'); + + do { + // Create and execute the content categories list request. + $response = $this->service->contentCategories->listContentCategories( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getContentCategories() as $categories) { + $this->printResultsTableRow($categories); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getContentCategories()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Content Categories'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Content Category ID', + 'name' => 'Content Category Name']; + } +} diff --git a/php/v4/examples/Placements/GetPlacementStrategies.php b/php/v4/examples/Placements/GetPlacementStrategies.php new file mode 100755 index 0000000..41e5dc4 --- /dev/null +++ b/php/v4/examples/Placements/GetPlacementStrategies.php @@ -0,0 +1,91 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all placement strategies

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Placement Strategies'); + + do { + // Create and execute the placement strategies list request. + $response = $this->service->placementStrategies->listPlacementStrategies( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getPlacementStrategies() as $strategies) { + $this->printResultsTableRow($strategies); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getPlacementStrategies()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Placement Strategies'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Placement Strategy ID', + 'name' => 'Placement Strategy Name']; + } +} diff --git a/php/v4/examples/Placements/GetPlacements.php b/php/v4/examples/Placements/GetPlacements.php new file mode 100755 index 0000000..78e64f4 --- /dev/null +++ b/php/v4/examples/Placements/GetPlacements.php @@ -0,0 +1,92 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all placements

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Placements'); + + do { + // Create and execute the placements list request. + $response = $this->service->placements->listPlacements( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getPlacements() as $placements) { + $this->printResultsTableRow($placements); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getPlacements()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Placements'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Placement ID', + 'name' => 'Placement Name', + 'campaignId' => 'Associated Campaign ID']; + } +} diff --git a/php/v4/examples/RemarketingLists/CreateRemarketingList.php b/php/v4/examples/RemarketingLists/CreateRemarketingList.php new file mode 100755 index 0000000..3f1fc76 --- /dev/null +++ b/php/v4/examples/RemarketingLists/CreateRemarketingList.php @@ -0,0 +1,122 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'floodlight_activity_id', + 'display' => 'Floodlight Activity ID', + 'required' => true], + ['name' => 'remarketing_list_name', + 'display' => 'Remarketing List Name', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating remarketing list with name "%s"

', + $values['remarketing_list_name'] + ); + + // Create the remarketing list. + $list = new Google_Service_Dfareporting_RemarketingList(); + $list->setActive(true); + $list->setAdvertiserId($values['advertiser_id']); + $list->setLifeSpan(30); + $list->setName($values['remarketing_list_name']); + + // Create a list population term. + // This term matches all visitors with a U1 value exactly matching + // "test_value". + $term = new Google_Service_Dfareporting_ListPopulationTerm(); + $term->setOperator('STRING_EQUALS'); + $term->setType('CUSTOM_VARIABLE_TERM'); + $term->setValue('test_value'); + $term->setVariableName('U1'); + + // Add the term to a list population clause. + $clause = new Google_Service_Dfareporting_ListPopulationClause(); + $clause->setTerms([$term]); + + // Add the clause to a list population rule. + // This rule will target all visitors who trigger the specified floodlight + // activity and satisfy the custom rule defined in the list population term. + $rule = new Google_Service_Dfareporting_ListPopulationRule(); + $rule->setFloodlightActivityId($values['floodlight_activity_id']); + $rule->setListPopulationClauses([$clause]); + + $list->setListPopulationRule($rule); + + // Insert the remarketing list. + $result = $this->service->remarketingLists->insert( + $values['user_profile_id'], + $list + ); + + $this->printResultsTable('Remarketing list created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Remarketing List'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Remarketing List ID', + 'name' => 'Remarketing List Name']; + } +} diff --git a/php/v4/examples/RemarketingLists/GetRemarketingLists.php b/php/v4/examples/RemarketingLists/GetRemarketingLists.php new file mode 100755 index 0000000..64bde3c --- /dev/null +++ b/php/v4/examples/RemarketingLists/GetRemarketingLists.php @@ -0,0 +1,104 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Listing all remarketing lists for advertiser ID %s

', + $values['advertiser_id'] + ); + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Remarketing Lists'); + + do { + // Create and execute the remarketing lists list request. + $response = $this->service->remarketingLists->listRemarketingLists( + $values['user_profile_id'], + $values['advertiser_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getRemarketingLists() as $list) { + $this->printResultsTableRow($list); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getRemarketingLists()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Remarketing Lists'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Remarketing List ID', + 'name' => 'Remarketing List Name']; + } +} diff --git a/php/v4/examples/RemarketingLists/ShareRemarketingList.php b/php/v4/examples/RemarketingLists/ShareRemarketingList.php new file mode 100755 index 0000000..6eacf69 --- /dev/null +++ b/php/v4/examples/RemarketingLists/ShareRemarketingList.php @@ -0,0 +1,108 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'list_id', + 'display' => 'Remarketing List ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Sharing remarketing list %s with advertiser ID %s

', + $values['list_id'], + $values['advertiser_id'] + ); + + // Load the existing share info. + $share = $this->service->remarketingListShares->get( + $values['user_profile_id'], + $values['list_id'] + ); + + $advertiserIds = $share['sharedAdvertiserIds']; + if (!isset($advertiserIds)) { + $advertiserIds = []; + } + + if (!in_array($values['advertiser_id'], $advertiserIds)) { + // Add the specified advertiser to the list of shared advertisers. + $advertiserIds[] = $values['advertiser_id']; + $share->setSharedAdvertiserIds($advertiserIds); + + // Update the share info with the newly added advertiser. + $result = $this->service->remarketingListShares->update( + $values['user_profile_id'], + $share + ); + + $result['advertiserIds'] = implode(',', $result['sharedAdvertiserIds']); + $this->printResultsTable('Remarketing list shared.', [$result]); + } else { + print '
Remarketing list is already shared with advertiser.
'; + } + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Share Remarketing List With Advertiser'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['remarketingListId' => 'Remarketing List ID', + 'advertiserIds' => 'Shared Advertiser IDs']; + } +} diff --git a/php/v4/examples/RemarketingLists/TargetAdToRemarketingList.php b/php/v4/examples/RemarketingLists/TargetAdToRemarketingList.php new file mode 100755 index 0000000..083d636 --- /dev/null +++ b/php/v4/examples/RemarketingLists/TargetAdToRemarketingList.php @@ -0,0 +1,117 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'ad_id', + 'display' => 'Ad ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Targeting ad %s to a targetable remarketing list.

', + $values['ad_id'] + ); + + // Load the specified ad. + $ad = $this->service->ads->get( + $values['user_profile_id'], + $values['ad_id'] + ); + + // Find a targetable remarketing list for this ad's advertiser. + $listService = $this->service->targetableRemarketingLists; + $lists = $listService->listTargetableRemarketingLists( + $values['user_profile_id'], + $ad['advertiserId'], + ['maxResults' => 1] + ); + + if (!empty($lists['targetableRemarketingLists'])) { + // Select the first targetable remarketing list that was returned. + $list = $lists['targetableRemarketingLists'][0]; + + // Create a list targeting expression. + $expression = new Google_Service_Dfareporting_ListTargetingExpression(); + $expression->setExpression(strval($list['id'])); + + // Update the ad. + $ad->setRemarketingListExpression($expression); + $result = $this->service->ads->update( + $values['user_profile_id'], + $ad + ); + + $result['expression'] = + $result['remarketing_list_expression']['expression']; + + $this->printResultsTable('Ad targeted to remarketing list.', [$result]); + } else { + print '
Ad has no targetable remarketing lists.
'; + } + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Target Ad to a Remarketing List'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Ad ID', + 'expression' => 'Remarketing List Expression']; + } +} diff --git a/php/v4/examples/Reports/CreateStandardReport.php b/php/v4/examples/Reports/CreateStandardReport.php new file mode 100755 index 0000000..2dc8618 --- /dev/null +++ b/php/v4/examples/Reports/CreateStandardReport.php @@ -0,0 +1,186 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * {@inheritdoc} + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Creating a new standard report

'; + $this->flush(); + + $userProfileId = $values['user_profile_id']; + + // 1. Create a report resource. + $report = $this->createReportResource(); + + // 2. Define the report criteria. + $this->defineReportCriteria($report); + + // 3. (optional) Look up compatible fields. + $this->findCompatibleFields($userProfileId, $report); + + // 4. Add dimension filters to the report criteria. + $this->addDimensionFilters($userProfileId, $report); + + // 5. Save the report resource. + $report = $this->insertReportResource($userProfileId, $report); + + $this->printResultsTable('Standard Report', [$report]); + } + + private function createReportResource() + { + $report = new Google_Service_Dfareporting_Report(); + + // Set the required fields "name" and "type". + $report->setName('Example standard report'); + $report->setType('STANDARD'); + + // Set optional fields. + $report->setFileName('example_report'); + $report->setFormat('CSV'); + + return $report; + } + + private function defineReportCriteria($report) + { + // Define a date range to report on. This example uses explicit start and + // end dates to mimic the "LAST_30_DAYS" relative date range. + $dateRange = new Google_Service_Dfareporting_DateRange(); + $dateRange->setStartDate( + date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') - 30, date('Y'))) + ); + $dateRange->setEndDate(date('Y-m-d')); + + // Create a report criteria. + $dimension = new Google_Service_Dfareporting_SortedDimension(); + $dimension->setName('advertiser'); + + $criteria = new Google_Service_Dfareporting_ReportCriteria(); + $criteria->setDateRange($dateRange); + $criteria->setDimensions([$dimension]); + $criteria->setMetricNames(['clicks', 'impressions']); + + // Add the criteria to the report resource. + $report->setCriteria($criteria); + } + + private function findCompatibleFields($userProfileId, $report) + { + $fields = $this->service->reports_compatibleFields->query( + $userProfileId, + $report + ); + + $reportFields = $fields->getReportCompatibleFields(); + + if (!empty($reportFields->getDimensions())) { + // Add a compatible dimension to the report. + $dimension = $reportFields->getDimensions()[0]; + $sortedDimension = new Google_Service_Dfareporting_SortedDimension(); + $sortedDimension->setName($dimension->getName()); + $report->getCriteria()->setDimensions( + array_merge( + $report->getCriteria()->getDimensions(), + [$sortedDimension] + ) + ); + } elseif (!empty($reportFields->getMetrics())) { + // Add a compatible metric to the report. + $metric = $reportFields->getMetrics()[0]; + $report->getCriteria()->setMetricNames( + array_merge( + $report->getCriteria()->getMetricNames(), + [$metric->getName()] + ) + ); + } + } + + private function addDimensionFilters($userProfileId, $report) + { + // Query advertiser dimension values for report run dates. + $request = new Google_Service_Dfareporting_DimensionValueRequest(); + $request->setStartDate( + $report->getCriteria()->getDateRange()->getStartDate() + ); + $request->setEndDate( + $report->getCriteria()->getDateRange()->getEndDate() + ); + $request->setDimensionName('advertiser'); + + $values = + $this->service->dimensionValues->query($userProfileId, $request); + + if (!empty($values->getItems())) { + // Add a value as a filter to the report criteria. + $report->getCriteria()->setDimensionFilters([$values->getItems()[0]]); + } + } + + private function insertReportResource($userProfileId, $report) + { + $insertedReport = + $this->service->reports->insert($userProfileId, $report); + return $insertedReport; + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Standard Report'; + } + /** + * {@inheritdoc} + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Report ID', + 'name' => 'Report Name']; + } +} diff --git a/php/v4/examples/Reports/DeleteReport.php b/php/v4/examples/Reports/DeleteReport.php new file mode 100755 index 0000000..5265906 --- /dev/null +++ b/php/v4/examples/Reports/DeleteReport.php @@ -0,0 +1,68 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'report_id', + 'display' => 'Report ID', + 'required' => true]]; + } + + /** + * {@inheritdoc} + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf('

Deleting report with ID %s

', $values['report_id']); + + $this->service->reports->delete( + $values['user_profile_id'], + $values['report_id'] + ); + + print '
Success
'; + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Delete Report'; + } +} diff --git a/php/v4/examples/Reports/DownloadReportFile.php b/php/v4/examples/Reports/DownloadReportFile.php new file mode 100755 index 0000000..363568b --- /dev/null +++ b/php/v4/examples/Reports/DownloadReportFile.php @@ -0,0 +1,180 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'report_id', + 'display' => 'Report ID', + 'required' => true], + ['name' => 'file_id', + 'display' => 'Report File ID']]; + } + + /** + * {@inheritdoc} + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + $reportId = $values['report_id']; + $userProfileId = $values['user_profile_id']; + + // 1. Find a file to download. + $fileId = $values['file_id']; + if (empty($fileId)) { + $fileId = $this->findFile($userProfileId, $reportId); + } + + if (!empty($fileId)) { + printf( + '

Retrieving contents of file %d for report %d

', + $fileId, + $reportId + ); + $this->flush(); + + // 2. (optional) Generate browser URL. + $this->generateBrowserUrl($reportId, $fileId); + $this->flush(); + + // 3. Directly download the file. + $this->directDownloadFile($reportId, $fileId); + } else { + printf( + '

No file found for profile ID %d and report ID %d

', + $userProfileId, + $reportId + ); + } + } + + private function findFile($userProfileId, $reportId) + { + $target = null; + $response = null; + $pageToken = null; + + do { + // Create and execute the file list request. + $response = $this->service->reports_files->listReportsFiles( + $userProfileId, + $reportId, + ['pageToken' => $pageToken] + ); + + foreach ($response->getItems() as $file) { + if ($this->isTargetFile($file)) { + $target = $file; + break; + } + } + + $pageToken = $response->getNextPageToken(); + } while (empty($target) && !empty($response->getItems()) && !empty($pageToken)); + + return is_null($target) ? null : $target->getId(); + } + + private function isTargetFile($file) + { + // Provide custom validation logic here. + // For example purposes, any file with REPORT_AVAILABLE status is + // considered valid. + return $file->getStatus() === 'REPORT_AVAILABLE'; + } + + private function generateBrowserUrl($reportId, $fileId) + { + $file = $this->service->files->get($reportId, $fileId); + $browserUrl = $file->getUrls()->getBrowserUrl(); + + printf( + '

Report file has browser URL: %s

', + $browserUrl, + $browserUrl + ); + } + + private function directDownloadFile($reportId, $fileId) + { + // Retrieve the file metadata. + $file = $this->service->files->get($reportId, $fileId); + + if ($file->getStatus() === 'REPORT_AVAILABLE') { + try { + // Prepare a local file to download the report contents to. + $fileName = join( + DIRECTORY_SEPARATOR, + [sys_get_temp_dir(), $this->generateFileName($file)] + ); + $fileResource = fopen($fileName, 'w+'); + $fileStream = \GuzzleHttp\Psr7\stream_for($fileResource); + + // Execute the get request and download the file. + $httpClient = $this->service->getClient()->authorize(); + $result = $httpClient->request( + 'GET', + $file->getUrls()->getApiUrl(), + [\GuzzleHttp\RequestOptions::SINK => $fileStream] + ); + + printf('

Report file saved to: %s

', $fileName); + print '

Report file contents:

'; + print nl2br(file_get_contents($fileName)); + } finally { + $fileStream->close(); + fclose($fileResource); + } + } + } + + private function generateFileName($file) + { + $fileName = $file->getFileName() ?: $file->getId(); + $extension = $file->getFormat() === 'CSV' ? '.csv' : '.xls'; + + return $fileName . $extension; + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Download Report File'; + } +} diff --git a/php/v4/examples/Reports/GenerateReportFile.php b/php/v4/examples/Reports/GenerateReportFile.php new file mode 100755 index 0000000..6ae928d --- /dev/null +++ b/php/v4/examples/Reports/GenerateReportFile.php @@ -0,0 +1,210 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'report_id', + 'display' => 'Report ID']]; + } + + /** + * {@inheritdoc} + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + $userProfileId = $values['user_profile_id']; + + // 1. Find a report to run. + $reportId = $values['report_id']; + if (empty($reportId)) { + $reportId = $this->findReport($userProfileId); + } + + if (!empty($reportId)) { + printf( + '

Generating a report file for report with ID %d

', + $reportId + ); + $this->flush(); + + // 2. Run the report. + $file = $this->runReport($userProfileId, $reportId); + $this->flush(); + + // 3. Wait for the report file to be ready. + $file = $this->waitForReportFile($reportId, $file->getId()); + $this->flush(); + + if (!empty($file)) { + $this->printResultsTable('Report File', [$file]); + } + } else { + printf('

No report found for profile ID %d

', $userProfileId); + } + } + + private function findReport($userProfileId) + { + $target = null; + $response = null; + $pageToken = null; + + do { + // Create and execute the report list request. + $response = $this->service->reports->listReports( + $userProfileId, + ['pageToken' => $pageToken] + ); + + foreach ($response->getItems() as $report) { + if ($this->isTargetReport($report)) { + $target = $report; + break; + } + } + + $pageToken = $response->getNextPageToken(); + } while (empty($target) && !empty($response->getItems()) && !empty($pageToken)); + + return is_null($target) ? null : $target->getId(); + } + + private function isTargetReport($file) + { + // Provide custom validation logic here. + // For example purposes, any report is considered valid. + return true; + } + + private function runReport($userProfileId, $reportId) + { + // Run the report. + $file = $this->service->reports->run($userProfileId, $reportId); + + printf( + 'Running report %d, current file status is %s
', + $reportId, + $file->getStatus() + ); + return $file; + } + + private function waitForReportFile($reportId, $fileId) + { + // Wait for the report file to finish processing. + // An exponential backoff policy is used to limit retries and conserve + // quota. + $sleep = 0; + $startTime = time(); + + do { + $file = $this->service->files->get($reportId, $fileId); + + if ($file->getStatus() === 'REPORT_AVAILABLE') { + printf('File status is %s, ready to download
', $file->getStatus()); + return $file; + } elseif ($file->getStatus() !== 'PROCESSING') { + printf('File status is %s, processing failed
', $file->getStatus()); + return null; + } elseif (time() - $startTime > self::MAX_RETRY_ELAPSED_TIME) { + printf('File processing deadline exceeded
'); + return null; + } + + $sleep = $this->getNextSleepInterval($sleep); + printf( + 'File status is %s, sleeping for %d seconds
', + $file->getStatus(), + $sleep + ); + $this->sleep($sleep); + } while (true); + } + + private function getNextSleepInterval($previousSleepInterval) + { + $minInterval = max(self::MIN_RETRY_INTERVAL, $previousSleepInterval); + $maxInterval = max(self::MIN_RETRY_INTERVAL, $previousSleepInterval * 3); + return min(self::MAX_RETRY_INTERVAL, rand($minInterval, $maxInterval)); + } + + private function sleep($interval) + { + $startTime = time(); + do { + // Flush regularly to prevent timeouts. + $this->flush(); + sleep(1); + + if (time() - $startTime >= $interval) { + return; + } + } while (true); + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Generate Report File'; + } + + /** + * {@inheritdoc} + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'File ID', + 'fileName' => 'File Name', + 'reportId' => 'Report ID', + 'format' => 'File Format', + 'status' => 'Status']; + } +} diff --git a/php/v4/examples/Reports/GetAllReportFiles.php b/php/v4/examples/Reports/GetAllReportFiles.php new file mode 100755 index 0000000..0e8ef43 --- /dev/null +++ b/php/v4/examples/Reports/GetAllReportFiles.php @@ -0,0 +1,101 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'report_id', + 'display' => 'Report ID', + 'required' => true]]; + } + + /** + * {@inheritdoc} + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Listing all files for report with ID %s

', + $values['report_id'] + ); + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Report Files'); + + do { + $response = $this->service->reports_files->listReportsFiles( + $values['user_profile_id'], + $values['report_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getItems() as $file) { + $file['apiUrl'] = $file->getUrls()->getApiUrl(); + $this->printResultsTableRow($file); + } + + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getItems()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Report Files'; + } + + /** + * {@inheritdoc} + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'File ID', + 'fileName' => 'File Name', + 'reportId' => 'Report ID', + 'format' => 'File Format', + 'status' => 'Status', + 'apiUrl' => 'API URL']; + } +} diff --git a/php/v4/examples/Reports/GetAllReports.php b/php/v4/examples/Reports/GetAllReports.php new file mode 100755 index 0000000..ad82e57 --- /dev/null +++ b/php/v4/examples/Reports/GetAllReports.php @@ -0,0 +1,90 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * {@inheritdoc} + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all reports

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Reports'); + do { + $response = $this->service->reports->listReports( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getItems() as $report) { + $this->printResultsTableRow($report); + } + + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getItems()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Reports'; + } + + /** + * {@inheritdoc} + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Report ID', + 'name' => 'Report Name', + 'type' => 'Report Type']; + } +} diff --git a/php/v4/examples/Reports/GetCompatibleFields.php b/php/v4/examples/Reports/GetCompatibleFields.php new file mode 100755 index 0000000..36ef130 --- /dev/null +++ b/php/v4/examples/Reports/GetCompatibleFields.php @@ -0,0 +1,110 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'report_id', + 'display' => 'Report ID', + 'required' => true]]; + } + + /** + * {@inheritdoc} + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Listing compatible fields for report with ID %s

', + $values['report_id'] + ); + + // Retrieve the specified report + $report = $this->service->reports->get( + $values['user_profile_id'], + $values['report_id'] + ); + + // Look up the compatible fields for this report + $fields = $this->service->reports_compatibleFields->query( + $values['user_profile_id'], + $report + ); + + // Print the compatible dimensions + $this->printResultsTable( + 'Dimensions', + $fields->getReportCompatibleFields()->getDimensions() + ); + + // Print the compatible metrics + $this->printResultsTable( + 'Metrics', + $fields->getReportCompatibleFields()->getMetrics() + ); + + // Print the compatible dimension filters + $this->printResultsTable( + 'Dimension Filters', + $fields->getReportCompatibleFields()->getDimensionFilters() + ); + + // Print the compatible pivoted activity metrics + $this->printResultsTable( + 'Pivoted Activity Metrics', + $fields->getReportCompatibleFields()->getPivotedActivityMetrics() + ); + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get Compatible Fields'; + } + + /** + * {@inheritdoc} + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['name' => 'Field Name']; + } +} diff --git a/php/v4/examples/Subaccounts/CreateSubaccount.php b/php/v4/examples/Subaccounts/CreateSubaccount.php new file mode 100755 index 0000000..1ee6220 --- /dev/null +++ b/php/v4/examples/Subaccounts/CreateSubaccount.php @@ -0,0 +1,99 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'account_id', + 'display' => 'Account ID', + 'required' => true], + ['name' => 'permission_one', + 'display' => 'First Permission ID', + 'required' => true], + ['name' => 'permission_two', + 'display' => 'Second Permission ID', + 'required' => true], + ['name' => 'subaccount_name', + 'display' => 'Subaccount Name', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating subaccount with name "%s" for account ID %s

', + $values['subaccount_name'], + $values['account_id'] + ); + + $subaccount = new Google_Service_Dfareporting_Subaccount(); + $subaccount->setName($values['subaccount_name']); + $subaccount->setAvailablePermissionIds( + [$values['permission_one'], $values['permission_two']] + ); + + $result = $this->service->subaccounts->insert( + $values['user_profile_id'], + $subaccount + ); + + $this->printResultsTable('Subaccount created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Subaccount'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Subaccount ID', + 'name' => 'Subaccount Name']; + } +} diff --git a/php/v4/examples/Subaccounts/GetSubaccountPermissions.php b/php/v4/examples/Subaccounts/GetSubaccountPermissions.php new file mode 100755 index 0000000..2eef2af --- /dev/null +++ b/php/v4/examples/Subaccounts/GetSubaccountPermissions.php @@ -0,0 +1,91 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'subaccount_id', + 'display' => 'Subaccount ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + $subaccount = $this->service->subaccounts->get( + $values['user_profile_id'], + $values['subaccount_id'] + ); + + printf( + '

Listing all permissions for subaccount "%s"

', + $subaccount->getName() + ); + + $permissions = + $this->service->userRolePermissions->listUserRolePermissions( + $values['user_profile_id'], + ['ids' => $subaccount->getAvailablePermissionIds()] + ); + + $this->printResultsTable('Subaccount Permissions', $permissions); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get Subaccount Permissions'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Permission ID', + 'name' => 'Permission Name']; + } +} diff --git a/php/v4/examples/Subaccounts/GetSubaccounts.php b/php/v4/examples/Subaccounts/GetSubaccounts.php new file mode 100755 index 0000000..e80e2b4 --- /dev/null +++ b/php/v4/examples/Subaccounts/GetSubaccounts.php @@ -0,0 +1,91 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all subaccounts

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Subaccounts'); + + do { + // Create and execute the subaccounts list request. + $response = $this->service->subaccounts->listSubaccounts( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getSubaccounts() as $subaccount) { + $this->printResultsTableRow($subaccount); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getSubaccounts()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Subaccounts'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Subaccount ID', + 'name' => 'Subaccount Name']; + } +} diff --git a/php/v4/examples/TargetingTemplates/ConfigureDynamicAssetSelection.php b/php/v4/examples/TargetingTemplates/ConfigureDynamicAssetSelection.php new file mode 100755 index 0000000..123b8d1 --- /dev/null +++ b/php/v4/examples/TargetingTemplates/ConfigureDynamicAssetSelection.php @@ -0,0 +1,158 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'creative_id', + 'display' => 'In-stream Video Creative ID', + 'required' => true], + ['name' => 'template_id', + 'display' => 'Targeting Template ID', + 'required' => true], + ['name' => 'asset_file', + 'display' => 'Video Asset File', + 'file' => true, + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Configuring dynamic asset selection for creative ID %s

', + $values['creative_id'] + ); + + // Retrieve the specified creative. + $creative = $this->service->creatives->get( + $values['user_profile_id'], + $values['creative_id'] + ); + if (is_null($creative) || strcmp($creative->getType(), 'INSTREAM_VIDEO') !== 0) { + print 'Invalid creative specified.'; + return; + } + + $assetSelection = $creative->getCreativeAssetSelection(); + if (!$creative->getDynamicAssetSelection()) { + // Locate an existing video asset to use as a default. + // This example uses the first PARENT_VIDEO asset found. + $defaultVideoAssetId = $this->findDefaultVideoAssetId($creative); + if ($defaultVideoAssetId < 0) { + print 'Default video asset could not be found.'; + return; + } + + // Create a new selection using the existing asset as a default. + $assetSelection = + new Google_Service_Dfareporting_CreativeAssetSelection(); + $assetSelection->setDefaultAssetId($defaultVideoAssetId); + $assetSelection->setRules([]); + + // Enable dynamic asset selection for the creative. + $creative->setDynamicAssetSelection(true); + $creative->setCreativeAssetSelection($assetSelection); + } + + // Upload the new video asset and add it to the creative. + $video = uploadAsset( + $this->service, + $values['user_profile_id'], + $creative->getAdvertiserId(), + $values['asset_file'], + 'VIDEO' + ); + $videoAsset = new Google_Service_Dfareporting_CreativeAsset(); + $videoAsset->setAssetIdentifier($video->getAssetIdentifier()); + $videoAsset->setRole('PARENT_VIDEO'); + $creative->setCreativeAssets( + array_merge($creative->getCreativeAssets(), [$videoAsset]) + ); + + // Create a rule targeting the new video asset and add it to the selection. + $rule = new Google_Service_Dfareporting_Rule(); + $rule->setAssetId($video->getId()); + $rule->setName('Test rule for asset ' . $video->getId()); + $rule->setTargetingTemplateId($values['template_id']); + $assetSelection->setRules( + array_merge($assetSelection->getRules(), [$rule]) + ); + + // Update the creative. + $result = $this->service->creatives->update( + $values['user_profile_id'], + $creative + ); + + printf( + 'Dynamic asset selection enabled for creative with ID %d.', + $result->getId() + ); + } + + private function findDefaultVideoAssetId($creative) + { + $assets = $creative->getCreativeAssets(); + $index = array_search( + 'PARENT_VIDEO', + array_map( + function ($asset) { + return $asset->getRole(); + }, + $assets + ) + ); + + return $index !== false ? $assets[$index]->getId() : -1; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Configure Dynamic Asset Targeting'; + } +} diff --git a/php/v4/examples/TargetingTemplates/CreateTargetingTemplate.php b/php/v4/examples/TargetingTemplates/CreateTargetingTemplate.php new file mode 100755 index 0000000..ae706b7 --- /dev/null +++ b/php/v4/examples/TargetingTemplates/CreateTargetingTemplate.php @@ -0,0 +1,100 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'advertiser_id', + 'display' => 'Advertiser ID', + 'required' => true], + ['name' => 'template_name', + 'display' => 'Template Name', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating targeting template "%s" for advertiser ID %s

', + $values['template_name'], + $values['advertiser_id'] + ); + + // Create the targeting template. + $template = new Google_Service_Dfareporting_TargetingTemplate(); + $template->setAdvertiserId($values['advertiser_id']); + $template->setName($values['template_name']); + + // Configure the template to serve ads on Monday, Wednesday, and Friday from + // 9 to 10am and 3 to 5pm. + $targeting = new Google_Service_Dfareporting_DayPartTargeting(); + $targeting->setDaysOfWeek(['MONDAY', 'WEDNESDAY', 'FRIDAY']); + $targeting->setHoursOfDay([9, 15, 16]); + $targeting->setUserLocalTime(true); + $template->setDayPartTargeting($targeting); + + $result = $this->service->targetingTemplates->insert( + $values['user_profile_id'], + $template + ); + + $this->printResultsTable('Targeting template created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create Targeting Template'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Template ID', + 'name' => 'Template Name']; + } +} diff --git a/php/v4/examples/TargetingTemplates/GetTargetingTemplates.php b/php/v4/examples/TargetingTemplates/GetTargetingTemplates.php new file mode 100755 index 0000000..05f85f4 --- /dev/null +++ b/php/v4/examples/TargetingTemplates/GetTargetingTemplates.php @@ -0,0 +1,93 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all targeting templates

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('Targeting Templates'); + + do { + // Create and execute the targeting templates list request. + $response = $this->service->targetingTemplates->listTargetingTemplates( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getTargetingTemplates() as $template) { + $this->printResultsTableRow($template); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getTargetingTemplates()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All Targeting Templates'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'Template ID', + 'name' => 'Template Name', + 'advertiserId' => 'Advertiser ID']; + } +} diff --git a/php/v4/examples/UserProfiles/GetAllUserProfiles.php b/php/v4/examples/UserProfiles/GetAllUserProfiles.php new file mode 100755 index 0000000..9d96a83 --- /dev/null +++ b/php/v4/examples/UserProfiles/GetAllUserProfiles.php @@ -0,0 +1,64 @@ +service->userProfiles->listUserProfiles(); + + print '

Listing of User Profiles associated with your account

'; + + $this->printResultsTable('User Profiles', $result['items']); + } + + /** + * {@inheritdoc} + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All User Profiles'; + } + + /** + * {@inheritdoc} + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['profileId' => 'Profile ID', + 'userName' => 'User Name', + 'accountId' => 'Account ID', + 'accountName' => 'Account Name', + 'subAccountId' => 'Subaccount ID', + 'subAccountName' => 'Subaccount Name']; + } +} diff --git a/php/v4/examples/UserRoles/CreateUserRole.php b/php/v4/examples/UserRoles/CreateUserRole.php new file mode 100755 index 0000000..e9eb7c0 --- /dev/null +++ b/php/v4/examples/UserRoles/CreateUserRole.php @@ -0,0 +1,106 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true], + ['name' => 'parent_user_role_id', + 'display' => 'Parent User Role ID', + 'required' => true], + ['name' => 'subaccount_id', + 'display' => 'Subaccount ID', + 'required' => true], + ['name' => 'permission_one', + 'display' => 'First Permission ID', + 'required' => true], + ['name' => 'permission_two', + 'display' => 'Second Permission ID', + 'required' => true], + ['name' => 'user_role_name', + 'display' => 'User Role Name', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + printf( + '

Creating user role with name "%s" under parent role ID %s

', + $values['user_role_name'], + $values['parent_user_role_id'] + ); + + $userRole = new Google_Service_Dfareporting_UserRole(); + $userRole->setName($values['user_role_name']); + $userRole->setParentUserRoleId($values['parent_user_role_id']); + $userRole->setPermissions( + [$values['permission_one'], $values['permission_two']] + ); + $userRole->setSubaccountId($values['subaccount_id']); + + $result = $this->service->userRoles->insert( + $values['user_profile_id'], + $userRole + ); + + $this->printResultsTable('User role created.', [$result]); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Create User Role'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'User Role ID', + 'name' => 'User Role Name']; + } +} diff --git a/php/v4/examples/UserRoles/GetUserRoles.php b/php/v4/examples/UserRoles/GetUserRoles.php new file mode 100755 index 0000000..d32ef82 --- /dev/null +++ b/php/v4/examples/UserRoles/GetUserRoles.php @@ -0,0 +1,93 @@ + 'user_profile_id', + 'display' => 'User Profile ID', + 'required' => true]]; + } + + /** + * (non-PHPdoc) + * @see BaseExample::run() + */ + public function run() + { + $values = $this->formValues; + + print '

Listing all user roles

'; + + $response = null; + $pageToken = null; + + $this->printResultsTableHeader('User Roles'); + + do { + // Create and execute the subaccounts list request. + $response = $this->service->userRoles->listUserRoles( + $values['user_profile_id'], + ['pageToken' => $pageToken] + ); + + foreach ($response->getUserRoles() as $userRoles) { + $this->printResultsTableRow($userRoles); + } + + // Update the next page token. + $pageToken = $response->getNextPageToken(); + } while (!empty($response->getUserRoles()) && !empty($pageToken)); + + $this->printResultsTableFooter(); + } + + /** + * (non-PHPdoc) + * @see BaseExample::getName() + * @return string + */ + public static function getName() + { + return 'Get All User Roles'; + } + + /** + * (non-PHPdoc) + * @see BaseExample::getResultsTableHeaders() + * @return array + */ + public function getResultsTableHeaders() + { + return ['id' => 'User Role ID', + 'name' => 'User Role Name']; + } +} diff --git a/php/v4/htmlHelper.php b/php/v4/htmlHelper.php new file mode 100755 index 0000000..8bf2486 --- /dev/null +++ b/php/v4/htmlHelper.php @@ -0,0 +1,81 @@ +'; + print ''; + printf('%s', $htmlTitle); + print ''; + print ''; +} + +/** + * Closes the HTML. + */ +function printHtmlFooter() +{ + print ''; + print ''; +} + +/** + * Closes the HTML for samples. + */ +function printSampleHtmlFooter() +{ + print '
Go back to samples list'; + printHtmlFooter(); +} + +/** + * Prints the index with links to the examples. + * @param array $actions supported actions + */ +function printExamplesIndex($actions) +{ + print '

Select a sample from the list

'; + print ''; +} diff --git a/php/v4/index.php b/php/v4/index.php new file mode 100755 index 0000000..d59533f --- /dev/null +++ b/php/v4/index.php @@ -0,0 +1,201 @@ +setApplicationName( + 'DCM/DFA Reporting and Trafficking API PHP Samples' +); +$client->addScope(Google_Service_Dfareporting::DFAREPORTING); +$client->addScope(Google_Service_Dfareporting::DFATRAFFICKING); +$client->addScope(Google_Service_Dfareporting::DDMCONVERSIONS); +$client->setAccessType('offline'); + +if (getenv('GOOGLE_APPLICATION_CREDENTIALS')) { + $client->useApplicationDefaultCredentials(); +} else { + // Be sure to replace the contents of client_secrets.json with your developer + // credentials. + $client->setAuthConfigFile('client_secrets.json'); +} + +// Create service. +$service = new Google_Service_Dfareporting($client); + +// If we're logging out we just need to clear our local access token. +// Note that this only logs you out of the session. If STORE_ON_DISK is +// enabled and you want to remove stored data, delete the file. +if (isset($_REQUEST['logout'])) { + unset($_SESSION['access_token']); +} + +// If we have a code back from the OAuth 2.0 flow, we need to exchange that +// with the authenticate() function. We store the resultant access token +// bundle in the session (and disk, if enabled), and redirect to this page. +if (isset($_GET['code'])) { + $client->authenticate($_GET['code']); + // Note that "getAccessToken" actually retrieves both the access and refresh + // tokens, assuming both are available. + $_SESSION['access_token'] = $client->getAccessToken(); + if (STORE_ON_DISK) { + file_put_contents(TOKEN_FILENAME, json_encode($_SESSION['access_token'])); + } + $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; + header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); + exit; +} + +// If we have an access token, we can make requests, else we generate an +// authentication URL. +if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { + $client->setAccessToken($_SESSION['access_token']); +} elseif (STORE_ON_DISK && file_exists(TOKEN_FILENAME) && + filesize(TOKEN_FILENAME) > 0) { + // Note that "setAccessToken" actually sets both the access and refresh token, + // assuming both were saved. + $client->setAccessToken(file_get_contents(TOKEN_FILENAME)); + $_SESSION['access_token'] = $client->getAccessToken(); +} else { + // If we're doing disk storage, generate a URL that forces user approval. + // This is the only way to guarantee we get back a refresh token. + if (STORE_ON_DISK) { + $client->setApprovalPrompt('force'); + } + $authUrl = $client->createAuthUrl(); +} + +$pageTitle = sprintf( + 'DCM/DFA Reporting and Trafficking API %s PHP usage samples', + $service->version +); +printHtmlHeader($pageTitle); + +if (isset($authUrl)) { + // No access token found, show the link to generate one + printf("Login!", $authUrl); +} else { + print "Logout"; +} + +if ($client->getAccessToken()) { + // If the action is set, dispatch the action if supported + if (isset($_GET['action'])) { + $action = decodeActionString($_GET['action']); + if (!isValidAction($action)) { + die('Unsupported action: ' . $_GET['action'] . "\n"); + } + + displayAction($action); + } else { + // Show the list of links to supported actions. + printExamplesIndex(getSupportedActions()); + printHtmlFooter(); + } + + // Note that we re-store the access_token bundle, just in case anything + // changed during the request - the main thing that might happen here is the + // access token itself is refreshed if the application has offline access. + $_SESSION['access_token'] = $client->getAccessToken(); +} + +/** + * Displays the requested action. + */ +function displayAction($action) +{ + global $service; + + // Render the required action. + include_once 'examples/' . $action[0] . '/' . $action[1] . '.php'; + $class = $action[1]; + $example = new $class($service); + printHtmlHeader($example->getName()); + try { + $example->execute(); + } catch (Google_Exception $ex) { + print_r($ex); + print 'An error as occurred while calling the example:
'; + print $ex->getMessage(); + } + printSampleHtmlFooter(); +} + +/** + * Determines whether the requested action is in our list of supported actions. + */ +function isValidAction($action) +{ + $actions = getSupportedActions(); + + if (array_key_exists($action[0], $actions)) { + $section = $actions[$action[0]]; + if (in_array($action[1], $section)) { + return true; + } + } + + return false; +} + +/** + * Decodes an action string passed as a URL parameter into a section and action + * pair. + */ +function decodeActionString($actionString) +{ + $parts = explode(':', $actionString); + if (count($parts) != 2) { + die('Invalid action specified.'); + } + + return $parts; +} + +/** + * Builds an array containing the supported actions, separated into sections. + */ +function getSupportedActions() +{ + $actions = []; + + foreach (glob('examples/*/*.php') as $file) { + $dir = dirname($file); + $section = substr($dir, strrpos($dir, '/') + 1); + + if (!array_key_exists($section, $actions)) { + $actions[$section] = []; + } + + $actions[$section][] = basename($file, '.php'); + } + + return $actions; +} diff --git a/php/v4/styles/style.css b/php/v4/styles/style.css new file mode 100755 index 0000000..c64bc3b --- /dev/null +++ b/php/v4/styles/style.css @@ -0,0 +1,38 @@ +/* + * Copyright 2015 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +a { + font-size: .9em; + color: black; +} + +table, th, td { + border: 1px solid black; +} + +ul { + list-style: none; +} + +.nav { + padding-left: 0; +} + +.noResults { + font-weight: bold; + text-align: center; +} +