| 1 |
<?php |
| 2 |
/** |
| 3 |
* Facebook Instant Articles integration class |
| 4 |
* |
| 5 |
* @package Parsely\Integrations |
| 6 |
* @since 2.6.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\Integrations; |
| 12 |
|
| 13 |
use Parsely\Parsely; |
| 14 |
|
| 15 |
/** |
| 16 |
* Integrates Parse.ly tracking with the Facebook Instant Articles plugin. |
| 17 |
* |
| 18 |
* @since 2.6.0 Moved from Parsely class to this file. |
| 19 |
*/ |
| 20 |
final class Facebook_Instant_Articles implements Integration { |
| 21 |
private const REGISTRY_IDENTIFIER = 'parsely-analytics-for-wordpress'; |
| 22 |
private const REGISTRY_DISPLAY_NAME = 'Parse.ly Analytics'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Apply the hooks that integrate the plugin or theme with the Parse.ly plugin. |
| 26 |
* |
| 27 |
* @since 2.6.0 |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public function integrate(): void { |
| 32 |
if ( defined( 'IA_PLUGIN_VERSION' ) ) { |
| 33 |
add_action( 'instant_articles_compat_registry_analytics', array( $this, 'insert_parsely_tracking' ) ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Add Parse.ly tracking to Facebook instant articles. |
| 39 |
* |
| 40 |
* @since 2.6.0 |
| 41 |
* |
| 42 |
* @param array $registry The registry info for Facebook Instant Articles. |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function insert_parsely_tracking( &$registry ): void { |
| 46 |
$parsely = new Parsely(); |
| 47 |
if ( $parsely->api_key_is_missing() ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$registry[ self::REGISTRY_IDENTIFIER ] = array( |
| 52 |
'name' => self::REGISTRY_DISPLAY_NAME, |
| 53 |
'payload' => $this->get_embed_code( $parsely->get_api_key() ), |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Get the payload / embed code. |
| 59 |
* |
| 60 |
* @since 2.6.0 |
| 61 |
* |
| 62 |
* @param string $api_key API key. |
| 63 |
* @return string Embedded code. |
| 64 |
*/ |
| 65 |
private function get_embed_code( string $api_key ): string { |
| 66 |
return '<script> |
| 67 |
PARSELY = { |
| 68 |
autotrack: false, |
| 69 |
onload: function() { |
| 70 |
PARSELY.beacon.trackPageView({ |
| 71 |
urlref: \'http://facebook.com/instantarticles\' |
| 72 |
}); |
| 73 |
return true; |
| 74 |
} |
| 75 |
} |
| 76 |
</script> |
| 77 |
<script data-cfasync="false" id="parsely-cfg" data-parsely-site="' . esc_attr( $api_key ) . '" src="//cdn.parsely.com/keys/' . esc_attr( $api_key ) . '/p.js"></script>'; |
| 78 |
} |
| 79 |
} |
| 80 |
|