-
Notifications
You must be signed in to change notification settings - Fork 17
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 #339 from Automattic/master
Alpha release Dec 08
- Loading branch information
Showing
9 changed files
with
150 additions
and
109 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
/** | ||
* Newspack Campaigns custom GA config. | ||
* | ||
* @package Newspack | ||
*/ | ||
|
||
/** | ||
* Extend the base Lightweight_API class. | ||
*/ | ||
require_once dirname( __FILE__ ) . '/../classes/class-lightweight-api.php'; | ||
require_once dirname( __FILE__ ) . '/../segmentation/class-segmentation.php'; | ||
require_once dirname( __FILE__ ) . '/../campaigns/class-campaign-data-utils.php'; | ||
|
||
/** | ||
* GET endpoint to create custom GA Config for AMP Analytics. | ||
*/ | ||
class Segmentation_Custom_GA_Config extends Lightweight_API { | ||
/** | ||
* Constructor. | ||
* | ||
* @codeCoverageIgnore | ||
*/ | ||
public function __construct() { | ||
parent::__construct(); | ||
$this->response = $this->get_custom_analytics_configuration( $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
$this->respond(); | ||
} | ||
|
||
/** | ||
* Get custom Analytics config, with segmentation-related custom dimensions assigned. | ||
* The pageviews will be reported using this configuration, so it's important | ||
* to include the custom dimensions set up by the Newspack Plugin, too. | ||
* | ||
* @param Request $request Request object. | ||
*/ | ||
public function get_custom_analytics_configuration( $request ) { | ||
$client_id = $request['client_id']; | ||
$ga_settings = maybe_unserialize( $this->get_option( 'googlesitekit_analytics_settings' ) ); | ||
if ( ! $client_id || ! $ga_settings || ! isset( $ga_settings['propertyID'] ) ) { | ||
return []; | ||
} | ||
|
||
$custom_dimensions = json_decode( $request['custom_dimensions'] ); | ||
|
||
// Tracking ID from Site Kit. | ||
$gtag_id = $ga_settings['propertyID']; | ||
|
||
$custom_dimensions_values = []; | ||
|
||
$api = new Lightweight_API(); | ||
$client_data = $api->get_client_data( $client_id ); | ||
|
||
foreach ( $custom_dimensions as $custom_dimension ) { | ||
// Strip the `ga:` prefix from gaID. | ||
$dimension_id = substr( $custom_dimension->gaID, 3 ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | ||
switch ( $custom_dimension->role ) { | ||
case Segmentation::CUSTOM_DIMENSIONS_OPTION_NAME_READER_FREQUENCY: | ||
$read_count = count( $client_data['posts_read'] ); | ||
// Tiers mimick NCI's – https://news-consumer-insights.appspot.com. | ||
$read_count_tier = 'casual'; | ||
if ( $read_count > 1 && $read_count <= 14 ) { | ||
$read_count_tier = 'loyal'; | ||
} elseif ( $read_count > 14 ) { | ||
$read_count_tier = 'brand_lover'; | ||
} | ||
$custom_dimensions_values[ $dimension_id ] = $read_count_tier; | ||
break; | ||
case Segmentation::CUSTOM_DIMENSIONS_OPTION_NAME_IS_SUBSCRIBER: | ||
$custom_dimensions_values[ $dimension_id ] = Campaign_Data_Utils::is_subscriber( $client_data, wp_get_referer() ); | ||
break; | ||
case Segmentation::CUSTOM_DIMENSIONS_OPTION_NAME_IS_DONOR: | ||
$custom_dimensions_values[ $dimension_id ] = Campaign_Data_Utils::is_donor( $client_data ); | ||
break; | ||
} | ||
} | ||
|
||
$custom_dimensions_existing_values = (array) json_decode( $request['custom_dimensions_existing_values'] ); | ||
|
||
// This is an AMP Analytics-compliant configuration, which on non-AMP pages will be | ||
// processed by this plugin's amp-analytics polyfill (src/view). | ||
return [ | ||
'vars' => [ | ||
'gtag_id' => $gtag_id, | ||
'config' => [ | ||
$gtag_id => array_merge( | ||
[ | ||
'groups' => 'default', | ||
], | ||
$custom_dimensions_values, | ||
$custom_dimensions_existing_values | ||
), | ||
], | ||
], | ||
'optoutElementId' => '__gaOptOutExtension', | ||
]; | ||
} | ||
} | ||
new Segmentation_Custom_GA_Config(); |
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
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
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
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