Skip to content

Instantly share code, notes, and snippets.

@lgedeon
Created February 25, 2020 06:51
Show Gist options
  • Save lgedeon/0c5a43b8705a7490810ffcec0f469339 to your computer and use it in GitHub Desktop.
Save lgedeon/0c5a43b8705a7490810ffcec0f469339 to your computer and use it in GitHub Desktop.
GA partial replacement for tightly coupled legacy code.
<?php
namespace IOne\IOne3\Core\Controllers;
use IOne\IOne3\Core\Entities\GoogleAnalyticsDimension;
/**
* Class GoogleAnalyticsController
*
* Consolidate multiple messages that should go into a single dimension.
*
* For now this uses a filter to add to existing GA functionality. Maybe migrate
* that code here eventually?
*
*/
class GoogleAnalyticsController {
/**
* List of all dimensions tracked by this class.
*
* @var GoogleAnalyticsDimension[]
*/
private $dimensions = [];
/**
* Setup hooks.
*/
public function setup() {
add_filter( 'ione_ga_dimensions', [ $this, 'filter_dimensions' ] );
add_action( 'ione__ione3__core__add_google_analytics_value', [ $this, 'add_value' ], 10, 2 );
}
/**
* Setup a dimension to be tracked.
*
* @param GoogleAnalyticsDimension $dimension Dimension object.
*/
public function add_dimension( GoogleAnalyticsDimension $dimension ) {
$number = $dimension->dimension_number();
if ( ! isset( $this->dimensions[ $number ] ) ) {
$this->dimensions[ $number ] = $dimension;
}
}
/**
*
* @param int $dimension_number Dimension number.
*
* @return GoogleAnalyticsDimension|null
*/
public function get_dimension( int $dimension_number ) {
return $this->dimensions[ $dimension_number ] ?? null;
}
/**
* Add stored dimensions to array.
*
* Filters array passed by `ione_ga_dimensions` filter hook for now, but may
* absorb functionality from class-ione-settings.php over time.
*
* @param array $dimensions Google Analytics dimension values.
*
* @return array
*/
public function filter_dimensions( $dimensions ) {
foreach ( $this->dimensions as $number => $dimension ) {
$label = 'dimension' . $number;
$dimensions[ $label ] = $dimension->value_string();
}
return $dimensions;
}
/**
* Allow other controllers to add values to a dimension via
* ione__ione3__core__add_google_analytics_value action.
*
* @param string $value Value to add to dimension.
* @param int $dimension_number Dimension to add value to.
*/
public function add_value( string $value, int $dimension_number ) {
$dimension = $this->get_dimension( $dimension_number );
if ( null !== $dimension ) {
$dimension->add_value( $value );
}
}
}
<?php
namespace IOne\IOne3\Core\Entities;
/**
* Class GoogleAnalyticsDimension
*
* Represent a single dimension that may have multiple values assigned to it.
*/
class GoogleAnalyticsDimension {
/**
* All values assigned to this dimension.
*
* @var string[]
*/
protected $values = [];
/**
* Default value if none other assigned.
*
* @var string
*/
protected $default = '';
/**
* Dimension number.
*
* @var integer
*/
protected $dimension_number = 0;
/**
* GoogleAnalyticsDimension constructor.
*
* @param integer $dimension_number Dimension number.
*/
public function __construct( int $dimension_number ) {
$this->dimension_number = $dimension_number;
}
/**
* Add a value to the list for this dimension.
*
* @param string $value Value to add to dimension.
*/
public function add_value( string $value ) {
if ( ! in_array( $value, $this->values, true ) ) {
$this->values[] = $value;
}
}
/**
* Set default for dimension.
*
* @param string $default Value used if no other values added to dimension.
*/
public function set_default( string $default ) {
$this->default = $default;
}
/**
* Return dimension number.
*
* @return int
*/
public function dimension_number() {
return $this->dimension_number;
}
/**
* Return all values together as a comma separated string.
*
* @return string
*/
public function value_string() {
if ( empty( $this->values ) ) {
return $this->default;
}
return implode( ', ', $this->values );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment