Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #320 from facebook/analytics_ads
Browse files Browse the repository at this point in the history
Configuration for ADS, Analytics and style on Transformation
  • Loading branch information
everton-rosario authored Feb 27, 2018
2 parents 90d5c6a + 7b60855 commit 093d3e0
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/Facebook/InstantArticles/Transformer/Settings/AdSettings.php
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;
}
}
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;
}
}
105 changes: 105 additions & 0 deletions src/Facebook/InstantArticles/Transformer/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
use Facebook\InstantArticles\Transformer\Warnings\UnrecognizedElement;
use Facebook\InstantArticles\Transformer\Logs\TransformerLog;
use Facebook\InstantArticles\Transformer\Rules\Rule;
use Facebook\InstantArticles\Transformer\Settings\AdSettings;
use Facebook\InstantArticles\Transformer\Settings\AnalyticsSettings;
use Facebook\InstantArticles\Elements\InstantArticle;
use Facebook\InstantArticles\Elements\Ad;
use Facebook\InstantArticles\Elements\Analytics;
use Facebook\InstantArticles\Validators\Type;
use Facebook\InstantArticles\Validators\InstantArticleValidator;

Expand Down Expand Up @@ -52,6 +56,21 @@ class Transformer
*/
private $defaultDateTimeZone;

/**
* @var string The default style name to be used on the Instant Article generated from this transformation.
*/
private $defaultStyleName;

/**
* @var AdSettings The content settings for ads on this transformation cycle.
*/
private $adsSettings;

/**
* @var AnalyticsSettings The content settings for analytics on this transformation cycle.
*/
private $analyticsSettings;

/**
* Flag attribute added to elements processed by a getter, so they
* are not processed again by other rules.
Expand Down Expand Up @@ -244,6 +263,9 @@ public function transformString($context, $content, $encoding = "utf-8")
libxml_clear_errors();
libxml_use_internal_errors($libxml_previous_state);
$result = $this->transform($context, $document);
if (Type::is($result, InstantArticle::getClassName())) {
$result = $this->handleTransformationSettings($result);
}
$totalTime = round(microtime(true) - $start, 3)*1000;
$totalWarnings = count($this->getWarnings());
$this->addLog(
Expand Down Expand Up @@ -362,6 +384,8 @@ public function transform($context, $node)
public function loadRules($json_file)
{
$configuration = json_decode($json_file, true);

// Treats the Rules configuration
if ($configuration && isset($configuration['rules'])) {
foreach ($configuration['rules'] as $configuration_rule) {
$class = $configuration_rule['class'];
Expand All @@ -377,6 +401,21 @@ public function loadRules($json_file)
$this->addRule($factory_method->invoke(null, $configuration_rule));
}
}

// Treats the ADS configuration
if ($configuration && isset($configuration['ads'])) {
$this->loadAdsConfiguration($configuration['ads']);
}

// Treats the Analyticds configuration
if ($configuration && isset($configuration['analytics'])) {
$this->loadAnalyicsConfiguration($configuration['analytics']);
}

// Treats the Style configuration
if ($configuration && isset($configuration['style_name'])) {
$this->setDefaultStyleName($configuration['style_name']);
}
}

/**
Expand Down Expand Up @@ -444,4 +483,70 @@ public function getDefaultDateTimeZone()
{
return $this->defaultDateTimeZone;
}

/**
* Sets the default style to be applyied to the articles generated from this transformation.
*
* @param string $defaultStyleName
*/
public function setDefaultStyleName($defaultStyleName)
{
Type::enforce($defaultStyleName, Type::STRING);
$this->defaultStyleName = $defaultStyleName;
}

/**
* Gets the default style name for Instant Article generated during transformation.
*
* @return string
*/
public function getDefaultStyleName()
{
return $this->defaultStyleName;
}

/**
* Applies the settings loaded from the rules.json informed on loadRules method.
*
* @param InstantArticle The InstantArticle to have the settings set.
* @return InstantArticle The article with settings added if needed.
*/
public function handleTransformationSettings($instantArticle)
{
if (!Type::isTextEmpty($this->getDefaultStyleName())) {
$instantArticle->withStyle($this->getDefaultStyleName());
}

if ($this->adsSettings) {
$ad = $this->adsSettings->getAdElement();
if ($ad) {
$instantArticle->getHeader()->addAd($ad);
}
}

if ($this->analyticsSettings) {
$analytics = $this->analyticsSettings->getAnalyticsElement();
if ($analytics) {
$instantArticle->addChild($analytics);
}
}

return $instantArticle;
}

public function loadAdsConfiguration($adsSettings)
{
$this->adsSettings = new AdSettings(
$adsSettings['audience_network_placement_id'],
$adsSettings['raw_html']
);
}

public function loadAnalyicsConfiguration($analyticsSettings)
{
$this->analyticsSettings = new AnalyticsSettings(
$analyticsSettings['fb_pixel_id'],
$analyticsSettings['raw_html']
);
}
}
18 changes: 18 additions & 0 deletions tests/Facebook/InstantArticles/Transformer/Example/simple-ia.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,35 @@
<meta property="op:generator:transformer" content="facebook-instant-articles-sdk-php"/>
<meta property="op:generator:transformer:version" content="1.0.0"/>
<meta property="op:markup_version" content="v1.0"/>
<meta property="fb:use_automatic_ad_placement" content="enable=true ad_density=default"/>
<meta property="fb:article_style" content="mydefaultstyle"/>
</head>
<body>
<article>
<header>
<h1>The article title</h1>
<figure class="op-ad">
<iframe src="https://www.facebook.com/adnw_request?placement=1234">
<script>alert('hello world!');</script>
<div class='block'></div>
</iframe>
</figure>
</header>
<p>Lorem <b>ipsum</b> dolor sit <b>amet</b>.</p>
<figure>
<img src="http://domain.com/image-body.png"/>
<figcaption>Photographer</figcaption>
</figure>
<figure class="op-tracker">
<iframe>
<!-- 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', '4321'); fbq('track', 'PageView'); </script>
<noscript><img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?ev=PageView&noscript=1&id=4321" /></noscript>
<!-- End Facebook Pixel Code -->
<script>alert('hello world!');</script>
<div class='block'></div></iframe></figure>
</article>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{
"style_name" : "mydefaultstyle",
"ads" : {
"audience_network_placement_id": "1234",
"raw_html": "<script>alert('hello world!');</script><div class='block'></div>"
},
"analytics" : {
"fb_pixel_id": "4321",
"raw_html": "<script>alert('hello world!');</script><div class='block'></div>"
},
"rules" :
[
{
Expand Down

0 comments on commit 093d3e0

Please sign in to comment.