-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b4be8cd
Showing
22 changed files
with
2,377 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/config | ||
adsapi_php.ini | ||
/vendor | ||
sftp-config.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace App\Dfp; | ||
|
||
require("../../vendor/autoload.php"); | ||
|
||
use Google\AdsApi\Common\OAuth2TokenBuilder; | ||
use Google\AdsApi\Dfp\DfpServices; | ||
use Google\AdsApi\Dfp\DfpSession; | ||
use Google\AdsApi\Dfp\DfpSessionBuilder; | ||
use Google\AdsApi\Dfp\v201802\Company; | ||
use Google\AdsApi\Dfp\v201802\CompanyService; | ||
use Google\AdsApi\Dfp\v201802\CompanyType; | ||
use Google\AdsApi\Dfp\Util\v201802\StatementBuilder; | ||
|
||
class CompanyManager extends DfpManager | ||
{ | ||
|
||
public function setUpCompany($companyName) | ||
{ | ||
if(empty(($foo = $this->getCompany($companyName)))) | ||
{ | ||
$foo = $this->createCompany($companyName); | ||
} | ||
return $foo[0]['companyId']; | ||
} | ||
|
||
public function createCompany($companyName) | ||
{ | ||
$output = []; | ||
|
||
$companyService = $this->dfpServices->get($this->session, CompanyService::class); | ||
$company = new Company(); | ||
$company->setName($companyName); | ||
$company->setType(CompanyType::ADVERTISER); | ||
// Create the company on the server. | ||
$data = $companyService->createCompanies([$company]); | ||
// Print out some information for each created company. | ||
foreach ($data as $i => $company) { | ||
$foo = array( | ||
"companyId"=>$company->getId(), | ||
"companyName"=>$company->getName() | ||
); | ||
array_push($output, $foo); | ||
} | ||
return $output; | ||
} | ||
|
||
public function getAllCompanies() | ||
{ | ||
$output = []; | ||
$companyService = $this->dfpServices->get($this->session, CompanyService::class); | ||
$statementBuilder = (new StatementBuilder())->orderBy('id ASC'); | ||
$data = $companyService->getCompaniesByStatement($statementBuilder->toStatement()); | ||
if ($data->getResults() !== null) | ||
{ | ||
foreach ($data->getResults() as $company) { | ||
$foo = array( | ||
"companyId"=>$company->getId(), | ||
"companyName"=>$company->getName() | ||
); | ||
array_push($output, $foo); | ||
} | ||
} | ||
return $output; | ||
} | ||
|
||
public function getCompany($companyName) | ||
{ | ||
$output = []; | ||
$companyService = $this->dfpServices->get($this->session, CompanyService::class); | ||
$statementBuilder = (new StatementBuilder()) | ||
->orderBy('id ASC') | ||
->where('name = :name') | ||
->WithBindVariableValue('name', $companyName); | ||
$data = $companyService->getCompaniesByStatement($statementBuilder->toStatement()); | ||
if ($data->getResults() !== null) | ||
{ | ||
foreach ($data->getResults() as $company) { | ||
$foo = array( | ||
"companyId"=>$company->getId(), | ||
"companyName"=>$company->getName() | ||
); | ||
array_push($output, $foo); | ||
} | ||
} | ||
return $output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
<?php | ||
|
||
namespace App\Dfp; | ||
|
||
require("../../vendor/autoload.php"); | ||
|
||
use Google\AdsApi\Common\OAuth2TokenBuilder; | ||
use Google\AdsApi\Dfp\DfpServices; | ||
use Google\AdsApi\Dfp\DfpSession; | ||
use Google\AdsApi\Dfp\DfpSessionBuilder; | ||
use Google\AdsApi\Dfp\Util\v201802\StatementBuilder; | ||
use Google\AdsApi\Dfp\v201802\CreativeAsset; | ||
use Google\AdsApi\Dfp\v201802\CreativeService; | ||
use Google\AdsApi\Dfp\v201802\ThirdPartyCreative; | ||
use Google\AdsApi\Dfp\v201802\Size; | ||
|
||
class CreativeManager extends DfpManager | ||
{ | ||
protected $ssp; | ||
protected $advertiserId; | ||
|
||
public function setSsp($ssp) | ||
{ | ||
$this->ssp = $ssp; | ||
return $this; | ||
} | ||
|
||
public function setAdvertiserId($advertiserId) | ||
{ | ||
$this->advertiserId = $advertiserId; | ||
return $this; | ||
} | ||
|
||
public function setUpCreatives() | ||
{ | ||
|
||
$output = []; | ||
//Create a creativeName List | ||
$creativeNameList = []; | ||
for ($i=1;$i <= 10; $i++) { | ||
array_push($creativeNameList, ucfirst($this->ssp)."_Prebid_Creative_$i"); | ||
} | ||
|
||
foreach($creativeNameList as $creativeName) | ||
{ | ||
if(empty(($foo = $this->getCreative($creativeName)))) | ||
{ | ||
$foo = $this->createCreative($creativeName, $this->createSnippet(), $this->advertiserId); | ||
|
||
} | ||
array_push($output, $foo[0]); | ||
} | ||
return $output; | ||
} | ||
|
||
public function getAllCreatives() | ||
{ | ||
$output = []; | ||
$creativeService = $this->dfpServices->get($this->session, CreativeService::class); | ||
$pageSize = StatementBuilder::SUGGESTED_PAGE_LIMIT; | ||
$statementBuilder = (new StatementBuilder())->orderBy('id ASC') | ||
->limit($pageSize); | ||
|
||
$totalResultSetSize = 0; | ||
do { | ||
$data = $creativeService->getCreativesByStatement($statementBuilder->toStatement()); | ||
if($data->getResults() == null) | ||
{ | ||
return $output; | ||
} | ||
foreach ($data->getResults() as $creative) { | ||
var_dump($creative); | ||
exit; | ||
$foo = array( | ||
"creativeId" => $creative->getId(), | ||
"creativeName" => $creative->getName(), | ||
); | ||
|
||
array_push($output, $foo); | ||
$statementBuilder->increaseOffsetBy($pageSize); | ||
} | ||
} while ($statementBuilder->getOffset() < $totalResultSetSize); | ||
|
||
return $output; | ||
} | ||
|
||
public function getCreative($creativeName) | ||
{ | ||
$output = []; | ||
$creativeService = $this->dfpServices->get($this->session, CreativeService::class); | ||
$statementBuilder = (new StatementBuilder()) | ||
->orderBy('id ASC') | ||
->where('name = :name') | ||
->WithBindVariableValue('name', $creativeName); | ||
$data = $creativeService->getCreativesByStatement($statementBuilder->toStatement()); | ||
if ($data->getResults() !== null) | ||
{ | ||
foreach ($data->getResults() as $creative) { | ||
$foo = array( | ||
"creativeId"=>$creative->getId(), | ||
"creativeName"=>$creative->getName() | ||
); | ||
array_push($output, $foo); | ||
} | ||
} | ||
return $output; | ||
} | ||
|
||
|
||
public function createCreative($creativeName, $snippet, $advertiserId) | ||
{ | ||
$output = []; | ||
$creativeService = $this->dfpServices->get($this->session, CreativeService::class); | ||
$size = new Size(); | ||
$size->setWidth(1); | ||
$size->setHeight(1); | ||
$size->setIsAspectRatio(false); | ||
|
||
$creative = new ThirdPartyCreative(); | ||
|
||
$creative->setName($creativeName) | ||
->setAdvertiserId($advertiserId) | ||
->setIsSafeFrameCompatible(false) | ||
->setSnippet($snippet) | ||
->setSize($size); | ||
|
||
// Create the order on the server. | ||
$results = $creativeService->createCreatives([$creative]); | ||
foreach ($results as $creative) { | ||
$foo = array( | ||
"creativeId"=>$creative->getId(), | ||
"creativeName"=>$creative->getName() | ||
); | ||
array_push($output, $foo); | ||
} | ||
return $output; | ||
} | ||
|
||
private function createSnippet() | ||
{ | ||
$key =substr("hb_adid_".$this->ssp,0,20); | ||
$snippet = "<script>\n"; | ||
$snippet .= "var w = window;\n"; | ||
$snippet .= "for (i = 0; i < 10; i++) {\n"; | ||
$snippet .= "\tw = w.parent;\n"; | ||
$snippet .= "\tif (w.pbjs) {\n"; | ||
$snippet .= "\t\ttry {\n"; | ||
$snippet .= "\t\t\tw.pbjs.renderAd(document, '%%PATTERN:".$key."%%');\n"; | ||
$snippet .= "\t\t\tbreak;\n"; | ||
$snippet .= "\t\t} catch (e) {\n"; | ||
$snippet .= "\t\t\tcontinue;\n"; | ||
$snippet .= "\t\t}\n"; | ||
$snippet .= "\t}\n"; | ||
$snippet .= "}\n"; | ||
$snippet .= "</script>\n"; | ||
return $snippet; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Dfp; | ||
|
||
require("../../vendor/autoload.php"); | ||
|
||
use Google\AdsApi\Common\OAuth2TokenBuilder; | ||
use Google\AdsApi\Dfp\DfpServices; | ||
use Google\AdsApi\Dfp\DfpSession; | ||
use Google\AdsApi\Dfp\DfpSessionBuilder; | ||
use Google\AdsApi\Dfp\v201802\Order; | ||
use Google\AdsApi\Dfp\v201802\OrderService; | ||
use Google\AdsApi\Dfp\v201802\Company; | ||
use Google\AdsApi\Dfp\v201802\CompanyService; | ||
use Google\AdsApi\Dfp\v201802\CompanyType; | ||
|
||
class DfpManager | ||
{ | ||
protected $dfpServices; | ||
protected $session; | ||
|
||
public function __construct() | ||
{ | ||
$oAuth2Credential = (new OAuth2TokenBuilder()) | ||
->fromFile() | ||
->build(); | ||
|
||
$this->session = (new DfpSessionBuilder()) | ||
->fromFile() | ||
->withOAuth2Credential($oAuth2Credential) | ||
->build(); | ||
|
||
$this->dfpServices = new DfpServices(); | ||
} | ||
|
||
public function getDfpServices() | ||
{ | ||
return $this->dfpServices; | ||
} | ||
|
||
public function getSession() | ||
{ | ||
return $this->session; | ||
} | ||
|
||
} |
Oops, something went wrong.