nystudio107 /
craft-instantanalytics
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Instant Analytics plugin for Craft CMS |
||
| 4 | * |
||
| 5 | * Instant Analytics brings full Google Analytics support to your Twig templates |
||
| 6 | * |
||
| 7 | * @link https://nystudio107.com |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 8 | * @copyright Copyright (c) 2017 nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 9 | */ |
||
|
0 ignored issues
–
show
|
|||
| 10 | |||
| 11 | namespace nystudio107\instantanalytics\twigextensions; |
||
| 12 | |||
| 13 | use Craft; |
||
| 14 | use craft\helpers\Template; |
||
| 15 | use nystudio107\instantanalytics\helpers\IAnalytics; |
||
| 16 | use nystudio107\instantanalytics\InstantAnalytics; |
||
| 17 | use Twig\Extension\AbstractExtension; |
||
| 18 | use Twig\Extension\GlobalsInterface; |
||
| 19 | use Twig\Markup; |
||
| 20 | use Twig\TwigFilter; |
||
| 21 | use Twig\TwigFunction; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Twig can be extended in many ways; you can add extra tags, filters, tests, |
||
| 25 | * operators, global variables, and functions. You can even extend the parser |
||
| 26 | * itself with node visitors. |
||
| 27 | * |
||
| 28 | * http://twig.sensiolabs.org/doc/advanced.html |
||
|
0 ignored issues
–
show
|
|||
| 29 | * |
||
| 30 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 31 | * @package InstantAnalytics |
||
|
0 ignored issues
–
show
|
|||
| 32 | * @since 1.0.0 |
||
|
0 ignored issues
–
show
|
|||
| 33 | */ |
||
|
0 ignored issues
–
show
|
|||
| 34 | class InstantAnalyticsTwigExtension extends AbstractExtension implements GlobalsInterface |
||
| 35 | { |
||
| 36 | // Public Methods |
||
| 37 | // ========================================================================= |
||
| 38 | |||
| 39 | /** |
||
|
0 ignored issues
–
show
|
|||
| 40 | * @inheritdoc |
||
| 41 | */ |
||
|
0 ignored issues
–
show
|
|||
| 42 | public function getName() |
||
| 43 | { |
||
| 44 | return 'InstantAnalytics'; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** @noinspection ReturnTypeCanBeDeclaredInspection */ |
||
|
0 ignored issues
–
show
|
|||
| 48 | /** |
||
|
0 ignored issues
–
show
|
|||
| 49 | * @@inheritdoc |
||
| 50 | */ |
||
|
0 ignored issues
–
show
|
|||
| 51 | public function getGlobals() |
||
| 52 | { |
||
| 53 | $globals = []; |
||
| 54 | $view = Craft::$app->getView(); |
||
| 55 | if ($view->getIsRenderingPageTemplate()) { |
||
| 56 | $request = Craft::$app->getRequest(); |
||
| 57 | if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
||
| 58 | // Return our Analytics object as a Twig global |
||
| 59 | $globals = [ |
||
| 60 | 'instantAnalytics' => InstantAnalytics::$plugin->ia->getGlobals(InstantAnalytics::$currentTemplate), |
||
| 61 | ]; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | return $globals; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
|
0 ignored issues
–
show
|
|||
| 69 | * @inheritdoc |
||
| 70 | */ |
||
|
0 ignored issues
–
show
|
|||
| 71 | public function getFilters() |
||
| 72 | { |
||
| 73 | return [ |
||
| 74 | new TwigFilter('pageViewAnalytics', [$this, 'pageViewAnalytics']), |
||
| 75 | new TwigFilter('eventAnalytics', [$this, 'eventAnalytics']), |
||
| 76 | new TwigFilter('pageViewTrackingUrl', [$this, 'pageViewTrackingUrl']), |
||
| 77 | new TwigFilter('eventTrackingUrl', [$this, 'eventTrackingUrl']), |
||
| 78 | ]; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
|
0 ignored issues
–
show
|
|||
| 82 | * @inheritdoc |
||
| 83 | */ |
||
|
0 ignored issues
–
show
|
|||
| 84 | public function getFunctions() |
||
| 85 | { |
||
| 86 | return [ |
||
| 87 | new TwigFunction('pageViewAnalytics', [$this, 'pageViewAnalytics']), |
||
| 88 | new TwigFunction('eventAnalytics', [$this, 'eventAnalytics']), |
||
| 89 | new TwigFunction('pageViewTrackingUrl', [$this, 'pageViewTrackingUrl']), |
||
| 90 | new TwigFunction('eventTrackingUrl', [$this, 'eventTrackingUrl']), |
||
| 91 | ]; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get a PageView analytics object |
||
| 96 | * |
||
| 97 | * @param string $url |
||
|
0 ignored issues
–
show
|
|||
| 98 | * @param string $title |
||
|
0 ignored issues
–
show
|
|||
| 99 | * |
||
| 100 | * @return null|IAnalytics object |
||
| 101 | */ |
||
| 102 | public function pageViewAnalytics($url = '', $title = '') |
||
| 103 | { |
||
| 104 | return InstantAnalytics::$plugin->ia->pageViewAnalytics($url, $title); |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get an Event analytics object |
||
| 109 | * |
||
| 110 | * @param string $eventCategory |
||
|
0 ignored issues
–
show
|
|||
| 111 | * @param string $eventAction |
||
|
0 ignored issues
–
show
|
|||
| 112 | * @param string $eventLabel |
||
|
0 ignored issues
–
show
|
|||
| 113 | * @param int $eventValue |
||
|
0 ignored issues
–
show
|
|||
| 114 | * |
||
| 115 | * @return null|IAnalytics |
||
| 116 | */ |
||
| 117 | public function eventAnalytics($eventCategory = '', $eventAction = '', $eventLabel = '', $eventValue = 0) |
||
| 118 | { |
||
| 119 | return InstantAnalytics::$plugin->ia->eventAnalytics($eventCategory, $eventAction, $eventLabel, $eventValue); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Return an Analytics object |
||
| 124 | * |
||
| 125 | * @return null|IAnalytics |
||
| 126 | */ |
||
| 127 | public function analytics() |
||
| 128 | { |
||
| 129 | return InstantAnalytics::$plugin->ia->analytics(); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get a PageView tracking URL |
||
| 134 | * |
||
| 135 | * @param $url |
||
|
0 ignored issues
–
show
|
|||
| 136 | * @param $title |
||
|
0 ignored issues
–
show
|
|||
| 137 | * |
||
| 138 | * @return Markup |
||
| 139 | * @throws \yii\base\Exception |
||
| 140 | */ |
||
| 141 | public function pageViewTrackingUrl($url, $title): Markup |
||
| 142 | { |
||
| 143 | return Template::raw(InstantAnalytics::$plugin->ia->pageViewTrackingUrl($url, $title)); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get an Event tracking URL |
||
| 148 | * |
||
| 149 | * @param $url |
||
|
0 ignored issues
–
show
|
|||
| 150 | * @param string $eventCategory |
||
|
0 ignored issues
–
show
|
|||
| 151 | * @param string $eventAction |
||
|
0 ignored issues
–
show
|
|||
| 152 | * @param string $eventLabel |
||
|
0 ignored issues
–
show
|
|||
| 153 | * @param int $eventValue |
||
|
0 ignored issues
–
show
|
|||
| 154 | * |
||
| 155 | * @return Markup |
||
| 156 | * @throws \yii\base\Exception |
||
| 157 | */ |
||
| 158 | public function eventTrackingUrl( |
||
| 159 | $url, |
||
| 160 | $eventCategory = '', |
||
| 161 | $eventAction = '', |
||
| 162 | $eventLabel = '', |
||
| 163 | $eventValue = 0 |
||
| 164 | ): Markup |
||
| 165 | { |
||
|
0 ignored issues
–
show
|
|||
| 166 | return Template::raw(InstantAnalytics::$plugin->ia->eventTrackingUrl( |
||
|
0 ignored issues
–
show
|
|||
| 167 | $url, |
||
| 168 | $eventCategory, |
||
| 169 | $eventAction, |
||
| 170 | $eventLabel, |
||
| 171 | $eventValue |
||
| 172 | )); |
||
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
Loading history...
|
|||
| 173 | } |
||
| 174 | } |
||
| 175 |