This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #320 from facebook/analytics_ads
Configuration for ADS, Analytics and style on Transformation
- Loading branch information
Showing
5 changed files
with
303 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/Facebook/InstantArticles/Transformer/Settings/AdSettings.php
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,77 @@ | ||
<?php | ||
/** | ||
* Copyright (c) 2016-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
namespace Facebook\InstantArticles\Transformer\Settings; | ||
|
||
use Facebook\InstantArticles\Validators\Type; | ||
use Facebook\InstantArticles\Elements\Ad; | ||
|
||
class AdSettings | ||
{ | ||
/** | ||
* @var string $audienceNetworkPlacementId AN representational ID | ||
*/ | ||
private $audienceNetworkPlacementId; | ||
|
||
/** | ||
* @var string $rawHTML The raw HTML content for ADS to be inserted into Instant Articles | ||
*/ | ||
private $rawHTML; | ||
|
||
public function __construct($audienceNetworkPlacementId = "", $rawHTML = "") | ||
{ | ||
if (Type::enforce($audienceNetworkPlacementId, Type::STRING) && | ||
!Type::isTextEmpty($audienceNetworkPlacementId)) { | ||
$this->audienceNetworkPlacementId = $audienceNetworkPlacementId; | ||
} | ||
if (Type::enforce($rawHTML, Type::STRING) && | ||
!Type::isTextEmpty($rawHTML)) { | ||
$this->rawHTML = $rawHTML; | ||
} | ||
} | ||
|
||
/** | ||
* @return string Returns the previous set raw HTML content; | ||
*/ | ||
public function getRawHTML() | ||
{ | ||
return $this->rawHTML; | ||
} | ||
|
||
/** | ||
* @return string Returns the previous set AN ID. | ||
*/ | ||
public function getAudienceNetworkPlacementId() | ||
{ | ||
return $this->audienceNetworkPlacementId; | ||
} | ||
|
||
public function getAdElement() | ||
{ | ||
$ad = null; | ||
|
||
if (!Type::isTextEmpty($this->getAudienceNetworkPlacementId()) || | ||
(!Type::isTextEmpty($this->getRawHTML()))) { | ||
|
||
$ad = Ad::create(); | ||
if (!Type::isTextEmpty($this->getAudienceNetworkPlacementId())) { | ||
$ad->withSource( | ||
'https://www.facebook.com/adnw_request?placement='. | ||
$this->getAudienceNetworkPlacementId() | ||
); | ||
} | ||
if (!Type::isTextEmpty($this->getRawHTML())) { | ||
$ad->withHTML( | ||
$this->getRawHTML() | ||
); | ||
} | ||
} | ||
|
||
return $ad; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/Facebook/InstantArticles/Transformer/Settings/AnalyticsSettings.php
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,94 @@ | ||
<?php | ||
/** | ||
* Copyright (c) 2016-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
namespace Facebook\InstantArticles\Transformer\Settings; | ||
|
||
use Facebook\InstantArticles\Validators\Type; | ||
use Facebook\InstantArticles\Elements\Analytics; | ||
|
||
class AnalyticsSettings | ||
{ | ||
/** | ||
* @var string $fbPixelId Facebook pixel representational ID | ||
*/ | ||
private $fbPixelId; | ||
|
||
/** | ||
* @var string $rawHTML The raw HTML content for ADS to be inserted into Instant Articles | ||
*/ | ||
private $rawHTML; | ||
|
||
public function __construct($fbPixelId = "", $rawHTML = "") | ||
{ | ||
if (Type::enforce($fbPixelId, Type::STRING) && | ||
!Type::isTextEmpty($fbPixelId)) { | ||
$this->fbPixelId = $fbPixelId; | ||
} | ||
if (Type::enforce($rawHTML, Type::STRING) && | ||
!Type::isTextEmpty($rawHTML)) { | ||
$this->rawHTML = $rawHTML; | ||
} | ||
} | ||
|
||
/** | ||
* @return string Returns the previous set raw HTML content; | ||
*/ | ||
public function getRawHTML() | ||
{ | ||
return $this->rawHTML; | ||
} | ||
|
||
/** | ||
* @return string Returns the previous set AN ID. | ||
*/ | ||
public function getFbPixelId() | ||
{ | ||
return $this->fbPixelId; | ||
} | ||
|
||
public function getFbPixelScript() | ||
{ | ||
$pixelCode = ""; | ||
if ($this->fbPixelId && !Type::isTextEmpty($this->fbPixelId)) { | ||
$pixelCode = $pixelCode. | ||
"<!-- Facebook Pixel Code --> ". | ||
"<script> ". | ||
"!function(f,b,e,v,n,t,s) ". | ||
"{if(f.fbq)return;n=f.fbq=function(){n.callMethod? ". | ||
"n.callMethod.apply(n,arguments):n.queue.push(arguments)}; ". | ||
"if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0'; ". | ||
"n.queue=[];t=b.createElement(e);t.async=!0; ". | ||
"t.src=v;s=b.getElementsByTagName(e)[0]; ". | ||
"s.parentNode.insertBefore(t,s)}(window, document,'script', ". | ||
"'https://connect.facebook.net/en_US/fbevents.js'); ". | ||
"fbq('init', '$this->fbPixelId'); ". | ||
"fbq('track', 'PageView'); ". | ||
"</script> ". | ||
"<noscript><img height=\"1\" width=\"1\" style=\"display:none\" ". | ||
"src=\"https://www.facebook.com/tr?ev=PageView&noscript=1&id=$this->fbPixelId\" ". | ||
"/></noscript> ". | ||
"<!-- End Facebook Pixel Code -->"; | ||
} | ||
|
||
if ($this->rawHTML && !Type::isTextEmpty($this->rawHTML)) { | ||
$pixelCode = $pixelCode." ".$this->rawHTML; | ||
} | ||
|
||
return $pixelCode; | ||
} | ||
|
||
public function getAnalyticsElement() | ||
{ | ||
$analytics = null; | ||
if (!Type::isTextEmpty($this->getFbPixelScript())) { | ||
$analytics = Analytics::create(); | ||
$analytics->withHTML($this->getFbPixelScript()); | ||
} | ||
return $analytics; | ||
} | ||
} |
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
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
9 changes: 9 additions & 0 deletions
9
tests/Facebook/InstantArticles/Transformer/Example/simple-rules.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