ActionDimension.php
6 years ago
ConversionDimension.php
6 years ago
DimensionMetadataProvider.php
6 years ago
VisitDimension.php
6 years ago
ActionDimension.php
132 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\Plugin\Dimension; |
| 10 | |
| 11 | use Piwik\CacheId; |
| 12 | use Piwik\Cache as PiwikCache; |
| 13 | use Piwik\Columns\Dimension; |
| 14 | use Piwik\Plugin\Manager as PluginManager; |
| 15 | use Piwik\Plugin; |
| 16 | use Piwik\Tracker\Action; |
| 17 | use Piwik\Tracker\Request; |
| 18 | use Piwik\Tracker\Visitor; |
| 19 | use Exception; |
| 20 | |
| 21 | /** |
| 22 | * Defines a new action dimension that records any information during tracking for each action. |
| 23 | * |
| 24 | * You can record any action information by implementing one of the following events: {@link onLookupAction()} and |
| 25 | * {@link getActionId()} or {@link onNewAction()}. By defining a {@link $columnName} and {@link $columnType} a new |
| 26 | * column will be created in the database (table `log_link_visit_action`) automatically and the values you return in |
| 27 | * the previous mentioned events will be saved in this column. |
| 28 | * |
| 29 | * You can create a new dimension using the console command `./console generate:dimension`. |
| 30 | * |
| 31 | * @api |
| 32 | * @since 2.5.0 |
| 33 | */ |
| 34 | abstract class ActionDimension extends Dimension |
| 35 | { |
| 36 | const INSTALLER_PREFIX = 'log_link_visit_action.'; |
| 37 | |
| 38 | protected $dbTableName = 'log_link_visit_action'; |
| 39 | protected $category = 'General_Actions'; |
| 40 | |
| 41 | /** |
| 42 | * If the value you want to save for your dimension is something like a page title or page url, you usually do not |
| 43 | * want to save the raw value over and over again to save bytes in the database. Instead you want to save each value |
| 44 | * once in the log_action table and refer to this value by its ID in the log_link_visit_action table. You can do |
| 45 | * this by returning an action id in "getActionId()" and by returning a value here. If a value should be ignored |
| 46 | * or not persisted just return boolean false. Please note if you return a value here and you implement the event |
| 47 | * "onNewAction" the value will be probably overwritten by the other event. So make sure to implement only one of |
| 48 | * those. |
| 49 | * |
| 50 | * @param Request $request |
| 51 | * @param Action $action |
| 52 | * |
| 53 | * @return false|mixed |
| 54 | * @api |
| 55 | */ |
| 56 | public function onLookupAction(Request $request, Action $action) |
| 57 | { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * An action id. The value returned by the lookup action will be associated with this id in the log_action table. |
| 63 | * @return int |
| 64 | * @throws Exception in case not implemented |
| 65 | */ |
| 66 | public function getActionId() |
| 67 | { |
| 68 | throw new Exception('You need to overwrite the getActionId method in case you implement the onLookupAction method in class: ' . get_class($this)); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * This event is triggered before a new action is logged to the `log_link_visit_action` table. It overwrites any |
| 73 | * looked up action so it makes usually no sense to implement both methods but it sometimes does. You can assign |
| 74 | * any value to the column or return boolan false in case you do not want to save any value. |
| 75 | * |
| 76 | * @param Request $request |
| 77 | * @param Visitor $visitor |
| 78 | * @param Action $action |
| 79 | * |
| 80 | * @return mixed|false |
| 81 | * @api |
| 82 | */ |
| 83 | public function onNewAction(Request $request, Visitor $visitor, Action $action) |
| 84 | { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get all action dimensions that are defined by all activated plugins. |
| 90 | * @return ActionDimension[] |
| 91 | * @ignore |
| 92 | */ |
| 93 | public static function getAllDimensions() |
| 94 | { |
| 95 | $cacheId = CacheId::pluginAware('ActionDimensions'); |
| 96 | $cache = PiwikCache::getTransientCache(); |
| 97 | |
| 98 | if (!$cache->contains($cacheId)) { |
| 99 | $plugins = PluginManager::getInstance()->getPluginsLoadedAndActivated(); |
| 100 | $instances = array(); |
| 101 | |
| 102 | foreach ($plugins as $plugin) { |
| 103 | foreach (self::getDimensions($plugin) as $instance) { |
| 104 | $instances[] = $instance; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | $cache->save($cacheId, $instances); |
| 109 | } |
| 110 | |
| 111 | return $cache->fetch($cacheId); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get all action dimensions that are defined by the given plugin. |
| 116 | * @param Plugin $plugin |
| 117 | * @return ActionDimension[] |
| 118 | * @ignore |
| 119 | */ |
| 120 | public static function getDimensions(Plugin $plugin) |
| 121 | { |
| 122 | $dimensions = $plugin->findMultipleComponents('Columns', '\\Piwik\\Plugin\\Dimension\\ActionDimension'); |
| 123 | $instances = array(); |
| 124 | |
| 125 | foreach ($dimensions as $dimension) { |
| 126 | $instances[] = new $dimension(); |
| 127 | } |
| 128 | |
| 129 | return $instances; |
| 130 | } |
| 131 | } |
| 132 |