PluginProbe
Parse.ly / 3.6.2
Parse.ly v3.6.2
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / Integrations / class-facebook-instant-articles.php

class-facebook-instant-articles.php in Parse.ly 3.6.2, at src/Integrations/class-facebook-instant-articles.php

78 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Integrations: Facebook Instant Articles integration class
4 *
5 * @package Parsely
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 extends Integration {
21 private const REGISTRY_IDENTIFIER = 'parsely-analytics-for-wordpress';
22 private const REGISTRY_DISPLAY_NAME = 'Parse.ly Analytics';
23
24 /**
25 * Applies the hooks that integrate the plugin or theme with the Parse.ly
26 * plugin.
27 *
28 * @since 2.6.0
29 */
30 public function integrate(): void {
31 if ( defined( 'IA_PLUGIN_VERSION' ) ) {
32 add_action( 'instant_articles_compat_registry_analytics', array( $this, 'insert_parsely_tracking' ) );
33 }
34 }
35
36 /**
37 * Adds Parse.ly tracking to Facebook instant articles.
38 *
39 * @since 2.6.0
40 *
41 * @param array $registry The registry info for Facebook Instant Articles.
42 */
43 public function insert_parsely_tracking( &$registry ): void {
44 $parsely = new Parsely();
45 if ( $parsely->api_key_is_missing() ) {
46 return;
47 }
48
49 $registry[ self::REGISTRY_IDENTIFIER ] = array(
50 'name' => self::REGISTRY_DISPLAY_NAME,
51 'payload' => $this->get_embed_code( $parsely->get_api_key() ),
52 );
53 }
54
55 /**
56 * Returns the payload / embed code.
57 *
58 * @since 2.6.0
59 *
60 * @param string $api_key API key.
61 * @return string Embedded code.
62 */
63 private function get_embed_code( string $api_key ): string {
64 return '<script>
65 PARSELY = {
66 autotrack: false,
67 onload: function() {
68 PARSELY.beacon.trackPageView({
69 urlref: \'http://facebook.com/instantarticles\'
70 });
71 return true;
72 }
73 }
74 </script>
75 <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>';
76 }
77 }
78