Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gchicoye committed Jun 4, 2018
0 parents commit b4be8cd
Show file tree
Hide file tree
Showing 22 changed files with 2,377 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/config
adsapi_php.ini
/vendor
sftp-config.json

89 changes: 89 additions & 0 deletions app/Dfp/CompanyManager.php
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;
}
}
158 changes: 158 additions & 0 deletions app/Dfp/CreativeManager.php
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;
}
}
46 changes: 46 additions & 0 deletions app/Dfp/DfpManager.php
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;
}

}
Loading

0 comments on commit b4be8cd

Please sign in to comment.