PDF rausgenommen
This commit is contained in:
131
msd2/tracking/piwik/core/Plugin/Dimension/ActionDimension.php
Normal file
131
msd2/tracking/piwik/core/Plugin/Dimension/ActionDimension.php
Normal file
@ -0,0 +1,131 @@
|
||||
<?php
|
||||
/**
|
||||
* Piwik - free/libre analytics platform
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
namespace Piwik\Plugin\Dimension;
|
||||
|
||||
use Piwik\CacheId;
|
||||
use Piwik\Cache as PiwikCache;
|
||||
use Piwik\Columns\Dimension;
|
||||
use Piwik\Plugin\Manager as PluginManager;
|
||||
use Piwik\Plugin;
|
||||
use Piwik\Tracker\Action;
|
||||
use Piwik\Tracker\Request;
|
||||
use Piwik\Tracker\Visitor;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Defines a new action dimension that records any information during tracking for each action.
|
||||
*
|
||||
* You can record any action information by implementing one of the following events: {@link onLookupAction()} and
|
||||
* {@link getActionId()} or {@link onNewAction()}. By defining a {@link $columnName} and {@link $columnType} a new
|
||||
* column will be created in the database (table `log_link_visit_action`) automatically and the values you return in
|
||||
* the previous mentioned events will be saved in this column.
|
||||
*
|
||||
* You can create a new dimension using the console command `./console generate:dimension`.
|
||||
*
|
||||
* @api
|
||||
* @since 2.5.0
|
||||
*/
|
||||
abstract class ActionDimension extends Dimension
|
||||
{
|
||||
const INSTALLER_PREFIX = 'log_link_visit_action.';
|
||||
|
||||
protected $dbTableName = 'log_link_visit_action';
|
||||
protected $category = 'General_Actions';
|
||||
|
||||
/**
|
||||
* If the value you want to save for your dimension is something like a page title or page url, you usually do not
|
||||
* want to save the raw value over and over again to save bytes in the database. Instead you want to save each value
|
||||
* once in the log_action table and refer to this value by its ID in the log_link_visit_action table. You can do
|
||||
* this by returning an action id in "getActionId()" and by returning a value here. If a value should be ignored
|
||||
* or not persisted just return boolean false. Please note if you return a value here and you implement the event
|
||||
* "onNewAction" the value will be probably overwritten by the other event. So make sure to implement only one of
|
||||
* those.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Action $action
|
||||
*
|
||||
* @return false|mixed
|
||||
* @api
|
||||
*/
|
||||
public function onLookupAction(Request $request, Action $action)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* An action id. The value returned by the lookup action will be associated with this id in the log_action table.
|
||||
* @return int
|
||||
* @throws Exception in case not implemented
|
||||
*/
|
||||
public function getActionId()
|
||||
{
|
||||
throw new Exception('You need to overwrite the getActionId method in case you implement the onLookupAction method in class: ' . get_class($this));
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is triggered before a new action is logged to the `log_link_visit_action` table. It overwrites any
|
||||
* looked up action so it makes usually no sense to implement both methods but it sometimes does. You can assign
|
||||
* any value to the column or return boolan false in case you do not want to save any value.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Visitor $visitor
|
||||
* @param Action $action
|
||||
*
|
||||
* @return mixed|false
|
||||
* @api
|
||||
*/
|
||||
public function onNewAction(Request $request, Visitor $visitor, Action $action)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all action dimensions that are defined by all activated plugins.
|
||||
* @return ActionDimension[]
|
||||
* @ignore
|
||||
*/
|
||||
public static function getAllDimensions()
|
||||
{
|
||||
$cacheId = CacheId::pluginAware('ActionDimensions');
|
||||
$cache = PiwikCache::getTransientCache();
|
||||
|
||||
if (!$cache->contains($cacheId)) {
|
||||
$plugins = PluginManager::getInstance()->getPluginsLoadedAndActivated();
|
||||
$instances = array();
|
||||
|
||||
foreach ($plugins as $plugin) {
|
||||
foreach (self::getDimensions($plugin) as $instance) {
|
||||
$instances[] = $instance;
|
||||
}
|
||||
}
|
||||
|
||||
$cache->save($cacheId, $instances);
|
||||
}
|
||||
|
||||
return $cache->fetch($cacheId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all action dimensions that are defined by the given plugin.
|
||||
* @param Plugin $plugin
|
||||
* @return ActionDimension[]
|
||||
* @ignore
|
||||
*/
|
||||
public static function getDimensions(Plugin $plugin)
|
||||
{
|
||||
$dimensions = $plugin->findMultipleComponents('Columns', '\\Piwik\\Plugin\\Dimension\\ActionDimension');
|
||||
$instances = array();
|
||||
|
||||
foreach ($dimensions as $dimension) {
|
||||
$instances[] = new $dimension();
|
||||
}
|
||||
|
||||
return $instances;
|
||||
}
|
||||
}
|
@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/**
|
||||
* Piwik - free/libre analytics platform
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
namespace Piwik\Plugin\Dimension;
|
||||
|
||||
use Piwik\CacheId;
|
||||
use Piwik\Cache as PiwikCache;
|
||||
use Piwik\Columns\Dimension;
|
||||
use Piwik\Plugin\Manager as PluginManager;
|
||||
use Piwik\Tracker\Action;
|
||||
use Piwik\Tracker\GoalManager;
|
||||
use Piwik\Tracker\Request;
|
||||
use Piwik\Tracker\Visitor;
|
||||
use Piwik\Plugin;
|
||||
|
||||
/**
|
||||
* Defines a new conversion dimension that records any visit related information during tracking.
|
||||
*
|
||||
* You can record any visit information by implementing one of the following events:
|
||||
* {@link onEcommerceOrderConversion()}, {@link onEcommerceCartUpdateConversion()} or {@link onGoalConversion()}.
|
||||
* By defining a {@link $columnName} and {@link $columnType} a new column will be created in the database
|
||||
* (table `log_conversion`) automatically and the values you return in the previous mentioned events will be saved in
|
||||
* this column.
|
||||
*
|
||||
* You can create a new dimension using the console command `./console generate:dimension`.
|
||||
*
|
||||
* @api
|
||||
* @since 2.5.0
|
||||
*/
|
||||
abstract class ConversionDimension extends Dimension
|
||||
{
|
||||
const INSTALLER_PREFIX = 'log_conversion.';
|
||||
|
||||
protected $dbTableName = 'log_conversion';
|
||||
protected $category = 'Goals_Conversion';
|
||||
|
||||
/**
|
||||
* Get all conversion dimensions that are defined by all activated plugins.
|
||||
* @ignore
|
||||
*/
|
||||
public static function getAllDimensions()
|
||||
{
|
||||
$cacheId = CacheId::pluginAware('ConversionDimensions');
|
||||
$cache = PiwikCache::getTransientCache();
|
||||
|
||||
if (!$cache->contains($cacheId)) {
|
||||
$plugins = PluginManager::getInstance()->getPluginsLoadedAndActivated();
|
||||
$instances = array();
|
||||
|
||||
foreach ($plugins as $plugin) {
|
||||
foreach (self::getDimensions($plugin) as $instance) {
|
||||
$instances[] = $instance;
|
||||
}
|
||||
}
|
||||
|
||||
$cache->save($cacheId, $instances);
|
||||
}
|
||||
|
||||
return $cache->fetch($cacheId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all conversion dimensions that are defined by the given plugin.
|
||||
* @param Plugin $plugin
|
||||
* @return ConversionDimension[]
|
||||
* @ignore
|
||||
*/
|
||||
public static function getDimensions(Plugin $plugin)
|
||||
{
|
||||
$dimensions = $plugin->findMultipleComponents('Columns', '\\Piwik\\Plugin\\Dimension\\ConversionDimension');
|
||||
$instances = array();
|
||||
|
||||
foreach ($dimensions as $dimension) {
|
||||
$instances[] = new $dimension();
|
||||
}
|
||||
|
||||
return $instances;
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is triggered when an ecommerce order is converted. Any returned value will be persist in the database.
|
||||
* Return boolean `false` if you do not want to change the value in some cases.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Visitor $visitor
|
||||
* @param Action|null $action
|
||||
* @param GoalManager $goalManager
|
||||
*
|
||||
* @return mixed|false
|
||||
* @api
|
||||
*/
|
||||
public function onEcommerceOrderConversion(Request $request, Visitor $visitor, $action, GoalManager $goalManager)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is triggered when an ecommerce cart update is converted. Any returned value will be persist in the
|
||||
* database. Return boolean `false` if you do not want to change the value in some cases.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Visitor $visitor
|
||||
* @param Action|null $action
|
||||
* @param GoalManager $goalManager
|
||||
*
|
||||
* @return mixed|false
|
||||
* @api
|
||||
*/
|
||||
public function onEcommerceCartUpdateConversion(Request $request, Visitor $visitor, $action, GoalManager $goalManager)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is triggered when an any custom goal is converted. Any returned value will be persist in the
|
||||
* database. Return boolean `false` if you do not want to change the value in some cases.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Visitor $visitor
|
||||
* @param Action|null $action
|
||||
* @param GoalManager $goalManager
|
||||
*
|
||||
* @return mixed|false
|
||||
* @api
|
||||
*/
|
||||
public function onGoalConversion(Request $request, Visitor $visitor, $action, GoalManager $goalManager)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
<?php
|
||||
/**
|
||||
* Piwik - free/libre analytics platform
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace Piwik\Plugin\Dimension;
|
||||
use Piwik\Piwik;
|
||||
|
||||
/**
|
||||
* Provides metadata about dimensions for the LogDataPurger class.
|
||||
*/
|
||||
class DimensionMetadataProvider
|
||||
{
|
||||
/**
|
||||
* Overrids for the result of the getActionReferenceColumnsByTable() method. Exists so Piwik
|
||||
* instances can be monkey patched, in case there are idaction columns that this class does not
|
||||
* naturally discover.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $actionReferenceColumnsOverride;
|
||||
|
||||
public function __construct(array $actionReferenceColumnsOverride = array())
|
||||
{
|
||||
$this->actionReferenceColumnsOverride = $actionReferenceColumnsOverride;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of idaction column names organized by table name. Uses dimension metadata
|
||||
* to find idaction columns dynamically.
|
||||
*
|
||||
* Note: It is not currently possible to use the Piwik platform to add idaction columns to tables
|
||||
* other than log_link_visit_action (w/o doing something unsupported), so idaction columns in
|
||||
* other tables are hard coded.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function getActionReferenceColumnsByTable()
|
||||
{
|
||||
$result = array(
|
||||
'log_link_visit_action' => array('idaction_url',
|
||||
'idaction_url_ref',
|
||||
'idaction_name_ref'
|
||||
),
|
||||
|
||||
'log_conversion' => array('idaction_url'),
|
||||
|
||||
'log_visit' => array('visit_exit_idaction_url',
|
||||
'visit_exit_idaction_name',
|
||||
'visit_entry_idaction_url',
|
||||
'visit_entry_idaction_name'),
|
||||
|
||||
'log_conversion_item' => array('idaction_sku',
|
||||
'idaction_name',
|
||||
'idaction_category',
|
||||
'idaction_category2',
|
||||
'idaction_category3',
|
||||
'idaction_category4',
|
||||
'idaction_category5')
|
||||
);
|
||||
|
||||
$dimensionIdActionColumns = $this->getVisitActionTableActionReferences();
|
||||
$result['log_link_visit_action'] = array_unique(
|
||||
array_merge($result['log_link_visit_action'], $dimensionIdActionColumns));
|
||||
|
||||
foreach ($this->actionReferenceColumnsOverride as $table => $columns) {
|
||||
if (empty($result[$table])) {
|
||||
$result[$table] = $columns;
|
||||
} else {
|
||||
$result[$table] = array_unique(array_merge($result[$table], $columns));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Triggered when detecting which log_action entries to keep. Any log tables that use the log_action
|
||||
* table to reference text via an ID should add their table info so no actions that are still in use
|
||||
* will be accidentally deleted.
|
||||
*
|
||||
* **Example**
|
||||
*
|
||||
* Piwik::addAction('Db.getActionReferenceColumnsByTable', function(&$result) {
|
||||
* $tableNameUnprefixed = 'log_example';
|
||||
* $columnNameThatReferencesIdActionInLogActionTable = 'idaction_example';
|
||||
* $result[$tableNameUnprefixed] = array($columnNameThatReferencesIdActionInLogActionTable);
|
||||
* });
|
||||
* @param array $result
|
||||
*/
|
||||
Piwik::postEvent('Db.getActionReferenceColumnsByTable', array(&$result));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getVisitActionTableActionReferences()
|
||||
{
|
||||
$idactionColumns = array();
|
||||
foreach (ActionDimension::getAllDimensions() as $actionDimension) {
|
||||
if ($this->isActionReference($actionDimension)) {
|
||||
$idactionColumns[] = $actionDimension->getColumnName();
|
||||
}
|
||||
}
|
||||
return $idactionColumns;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns `true` if the column for this dimension is a reference to the `log_action` table (ie, an "idaction column"),
|
||||
* `false` if otherwise.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isActionReference(ActionDimension $dimension)
|
||||
{
|
||||
try {
|
||||
$dimension->getActionId();
|
||||
|
||||
return true;
|
||||
} catch (\Exception $ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
349
msd2/tracking/piwik/core/Plugin/Dimension/VisitDimension.php
Normal file
349
msd2/tracking/piwik/core/Plugin/Dimension/VisitDimension.php
Normal file
@ -0,0 +1,349 @@
|
||||
<?php
|
||||
/**
|
||||
* Piwik - free/libre analytics platform
|
||||
*
|
||||
* @link http://piwik.org
|
||||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
|
||||
*
|
||||
*/
|
||||
namespace Piwik\Plugin\Dimension;
|
||||
|
||||
use Piwik\CacheId;
|
||||
use Piwik\Cache as PiwikCache;
|
||||
use Piwik\Columns\Dimension;
|
||||
use Piwik\Common;
|
||||
use Piwik\Db;
|
||||
use Piwik\DbHelper;
|
||||
use Piwik\Plugin\Manager as PluginManager;
|
||||
use Piwik\Tracker\Request;
|
||||
use Piwik\Tracker\Visitor;
|
||||
use Piwik\Tracker\Action;
|
||||
use Piwik\Plugin;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Defines a new visit dimension that records any visit related information during tracking.
|
||||
*
|
||||
* You can record any visit information by implementing one of the following events: {@link onNewVisit()},
|
||||
* {@link onExistingVisit()}, {@link onConvertedVisit()} or {@link onAnyGoalConversion()}. By defining a
|
||||
* {@link $columnName} and {@link $columnType} a new column will be created in the database (table `log_visit`)
|
||||
* automatically and the values you return in the previous mentioned events will be saved in this column.
|
||||
*
|
||||
* You can create a new dimension using the console command `./console generate:dimension`.
|
||||
*
|
||||
* @api
|
||||
* @since 2.5.0
|
||||
*/
|
||||
abstract class VisitDimension extends Dimension
|
||||
{
|
||||
const INSTALLER_PREFIX = 'log_visit.';
|
||||
|
||||
protected $dbTableName = 'log_visit';
|
||||
protected $category = 'General_Visitors';
|
||||
|
||||
public function install()
|
||||
{
|
||||
if (empty($this->columnType) || empty($this->columnName)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$changes = array(
|
||||
$this->dbTableName => array("ADD COLUMN `$this->columnName` $this->columnType")
|
||||
);
|
||||
|
||||
if ($this->isHandlingLogConversion()) {
|
||||
$changes['log_conversion'] = array("ADD COLUMN `$this->columnName` $this->columnType");
|
||||
}
|
||||
|
||||
return $changes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ActionDimension::update()
|
||||
* @return array
|
||||
* @ignore
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
if (!$this->columnType) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$conversionColumns = DbHelper::getTableColumns(Common::prefixTable('log_conversion'));
|
||||
|
||||
$changes = array();
|
||||
|
||||
$changes[$this->dbTableName] = array("MODIFY COLUMN `$this->columnName` $this->columnType");
|
||||
|
||||
$handlingConversion = $this->isHandlingLogConversion();
|
||||
$hasConversionColumn = array_key_exists($this->columnName, $conversionColumns);
|
||||
|
||||
if ($hasConversionColumn && $handlingConversion) {
|
||||
$changes['log_conversion'] = array("MODIFY COLUMN `$this->columnName` $this->columnType");
|
||||
} elseif (!$hasConversionColumn && $handlingConversion) {
|
||||
$changes['log_conversion'] = array("ADD COLUMN `$this->columnName` $this->columnType");
|
||||
} elseif ($hasConversionColumn && !$handlingConversion) {
|
||||
$changes['log_conversion'] = array("DROP COLUMN `$this->columnName`");
|
||||
}
|
||||
|
||||
return $changes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @ignore
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
return $this->columnType . $this->isHandlingLogConversion();
|
||||
}
|
||||
|
||||
private function isHandlingLogConversion()
|
||||
{
|
||||
if (empty($this->columnName) || empty($this->columnType)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->hasImplementedEvent('onAnyGoalConversion');
|
||||
}
|
||||
|
||||
/**
|
||||
* Uninstalls the dimension if a {@link $columnName} and {@link columnType} is set. In case you perform any custom
|
||||
* actions during {@link install()} - for instance adding an index - you should make sure to undo those actions by
|
||||
* overwriting this method. Make sure to call this parent method to make sure the uninstallation of the column
|
||||
* will be done.
|
||||
* @throws Exception
|
||||
* @api
|
||||
*/
|
||||
public function uninstall()
|
||||
{
|
||||
if (empty($this->columnName) || empty($this->columnType)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$sql = "ALTER TABLE `" . Common::prefixTable($this->dbTableName) . "` DROP COLUMN `$this->columnName`";
|
||||
Db::exec($sql);
|
||||
} catch (Exception $e) {
|
||||
if (!Db::get()->isErrNo($e, '1091')) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (!$this->isHandlingLogConversion()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sql = "ALTER TABLE `" . Common::prefixTable('log_conversion') . "` DROP COLUMN `$this->columnName`";
|
||||
Db::exec($sql);
|
||||
} catch (Exception $e) {
|
||||
if (!Db::get()->isErrNo($e, '1091')) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sometimes you may want to make sure another dimension is executed before your dimension so you can persist
|
||||
* this dimensions' value depending on the value of other dimensions. You can do this by defining an array of
|
||||
* dimension names. If you access any value of any other column within your events, you should require them here.
|
||||
* Otherwise those values may not be available.
|
||||
* @return array
|
||||
* @api
|
||||
*/
|
||||
public function getRequiredVisitFields()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* The `onNewVisit` method is triggered when a new visitor is detected. This means you can define an initial
|
||||
* value for this user here. By returning boolean `false` no value will be saved. Once the user makes another action
|
||||
* the event "onExistingVisit" is executed. Meaning for each visitor this method is executed once.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Visitor $visitor
|
||||
* @param Action|null $action
|
||||
* @return mixed|false
|
||||
* @api
|
||||
*/
|
||||
public function onNewVisit(Request $request, Visitor $visitor, $action)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `onExistingVisit` method is triggered when a visitor was recognized meaning it is not a new visitor.
|
||||
* You can overwrite any previous value set by the event `onNewVisit` by implemting this event. By returning boolean
|
||||
* `false` no value will be updated.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Visitor $visitor
|
||||
* @param Action|null $action
|
||||
* @return mixed|false
|
||||
* @api
|
||||
*/
|
||||
public function onExistingVisit(Request $request, Visitor $visitor, $action)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This event is executed shortly after `onNewVisit` or `onExistingVisit` in case the visitor converted a goal.
|
||||
* Usually this event is not needed and you can simply remove this method therefore. An example would be for
|
||||
* instance to persist the last converted action url. Return boolean `false` if you do not want to change the
|
||||
* current value.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Visitor $visitor
|
||||
* @param Action|null $action
|
||||
* @return mixed|false
|
||||
* @api
|
||||
*/
|
||||
public function onConvertedVisit(Request $request, Visitor $visitor, $action)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* By implementing this event you can persist a value to the `log_conversion` table in case a conversion happens.
|
||||
* The persisted value will be logged along the conversion and will not be changed afterwards. This allows you to
|
||||
* generate reports that shows for instance which url was called how often for a specific conversion. Once you
|
||||
* implement this event and a $columnType is defined a column in the `log_conversion` MySQL table will be
|
||||
* created automatically.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Visitor $visitor
|
||||
* @param Action|null $action
|
||||
* @return mixed|false
|
||||
* @api
|
||||
*/
|
||||
public function onAnyGoalConversion(Request $request, Visitor $visitor, $action)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This hook is executed by the tracker when determining if an action is the start of a new visit
|
||||
* or part of an existing one. Derived classes can use it to force new visits based on dimension
|
||||
* data.
|
||||
*
|
||||
* For example, the Campaign dimension in the Referrers plugin will force a new visit if the
|
||||
* campaign information for the current action is different from the last.
|
||||
*
|
||||
* @param Request $request The current tracker request information.
|
||||
* @param Visitor $visitor The information for the currently recognized visitor.
|
||||
* @param Action|null $action The current action information (if any).
|
||||
* @return bool Return true to force a visit, false if otherwise.
|
||||
* @api
|
||||
*/
|
||||
public function shouldForceNewVisit(Request $request, Visitor $visitor, Action $action = null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all visit dimensions that are defined by all activated plugins.
|
||||
* @return VisitDimension[]
|
||||
*/
|
||||
public static function getAllDimensions()
|
||||
{
|
||||
$cacheId = CacheId::pluginAware('VisitDimensions');
|
||||
$cache = PiwikCache::getTransientCache();
|
||||
|
||||
if (!$cache->contains($cacheId)) {
|
||||
$plugins = PluginManager::getInstance()->getPluginsLoadedAndActivated();
|
||||
$instances = array();
|
||||
|
||||
foreach ($plugins as $plugin) {
|
||||
foreach (self::getDimensions($plugin) as $instance) {
|
||||
$instances[] = $instance;
|
||||
}
|
||||
}
|
||||
|
||||
$instances = self::sortDimensions($instances);
|
||||
|
||||
$cache->save($cacheId, $instances);
|
||||
}
|
||||
|
||||
return $cache->fetch($cacheId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
* @param VisitDimension[] $dimensions
|
||||
*/
|
||||
public static function sortDimensions($dimensions)
|
||||
{
|
||||
$sorted = array();
|
||||
$exists = array();
|
||||
|
||||
// we first handle all the once without dependency
|
||||
foreach ($dimensions as $index => $dimension) {
|
||||
$fields = $dimension->getRequiredVisitFields();
|
||||
if (empty($fields)) {
|
||||
$sorted[] = $dimension;
|
||||
$exists[] = $dimension->getColumnName();
|
||||
unset($dimensions[$index]);
|
||||
}
|
||||
}
|
||||
|
||||
// find circular references
|
||||
// and remove dependencies whose column cannot be resolved because it is not installed / does not exist / is defined by core
|
||||
$dependencies = array();
|
||||
foreach ($dimensions as $dimension) {
|
||||
$dependencies[$dimension->getColumnName()] = $dimension->getRequiredVisitFields();
|
||||
}
|
||||
|
||||
foreach ($dependencies as $column => $fields) {
|
||||
foreach ($fields as $key => $field) {
|
||||
if (empty($dependencies[$field]) && !in_array($field, $exists)) {
|
||||
// we cannot resolve that dependency as it does not exist
|
||||
unset($dependencies[$column][$key]);
|
||||
} elseif (!empty($dependencies[$field]) && in_array($column, $dependencies[$field])) {
|
||||
throw new Exception("Circular reference detected for required field $field in dimension $column");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$count = 0;
|
||||
while (count($dimensions) > 0) {
|
||||
$count++;
|
||||
if ($count > 1000) {
|
||||
foreach ($dimensions as $dimension) {
|
||||
$sorted[] = $dimension;
|
||||
}
|
||||
break; // to prevent an endless loop
|
||||
}
|
||||
foreach ($dimensions as $key => $dimension) {
|
||||
$fields = $dependencies[$dimension->getColumnName()];
|
||||
if (count(array_intersect($fields, $exists)) === count($fields)) {
|
||||
$sorted[] = $dimension;
|
||||
$exists[] = $dimension->getColumnName();
|
||||
unset($dimensions[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $sorted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all visit dimensions that are defined by the given plugin.
|
||||
* @param Plugin $plugin
|
||||
* @return VisitDimension[]
|
||||
* @ignore
|
||||
*/
|
||||
public static function getDimensions(Plugin $plugin)
|
||||
{
|
||||
$dimensions = $plugin->findMultipleComponents('Columns', '\\Piwik\\Plugin\\Dimension\\VisitDimension');
|
||||
$instances = array();
|
||||
|
||||
foreach ($dimensions as $dimension) {
|
||||
$instances[] = new $dimension();
|
||||
}
|
||||
|
||||
return $instances;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user