InstantAnalyticsTwigExtension::pageViewEvent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
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
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\instantanalyticsGa4\twigextensions;
12
13
use Br33f\Ga4\MeasurementProtocol\Dto\Event\BaseEvent;
14
use Craft;
15
use craft\helpers\Template;
16
use nystudio107\instantanalyticsGa4\ga4\events\PageViewEvent;
17
use nystudio107\instantanalyticsGa4\helpers\Analytics;
18
use nystudio107\instantanalyticsGa4\InstantAnalytics;
19
use Twig\Extension\AbstractExtension;
20
use Twig\Extension\GlobalsInterface;
21
use Twig\Markup;
22
use Twig\TwigFilter;
23
use Twig\TwigFunction;
24
use yii\base\Exception;
25
26
/**
27
 * Twig can be extended in many ways; you can add extra tags, filters, tests,
28
 * operators, global variables, and functions. You can even extend the parser
29
 * itself with node visitors.
30
 *
31
 * http://twig.sensiolabs.org/doc/advanced.html
0 ignored issues
show
Coding Style introduced by
Doc comment long description must start with a capital letter
Loading history...
32
 *
33
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
34
 * @package   InstantAnalytics
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
35
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
36
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
37
class InstantAnalyticsTwigExtension extends AbstractExtension implements GlobalsInterface
38
{
39
    // Public Methods
40
    // =========================================================================
41
42
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @inheritdoc
44
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
45
    public function getName(): string
46
    {
47
        return 'InstantAnalytics';
48
    }
49
50
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
51
     * @inheritdoc
52
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
53
    public function getGlobals(): array
54
    {
55
        $globals = [];
56
        $view = Craft::$app->getView();
57
        if ($view->getIsRenderingPageTemplate()) {
58
            $request = Craft::$app->getRequest();
59
            if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
60
                // Return our Analytics object as a Twig global
61
                $globals = [
62
                    'instantAnalytics' => InstantAnalytics::$plugin->ga4->getAnalytics(),
63
                ];
64
            }
65
        }
66
67
        return $globals;
68
    }
69
70
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
71
     * @inheritdoc
72
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
73
    public function getFilters(): array
74
    {
75
        return [
76
            new TwigFilter('pageViewEvent', [$this, 'pageViewEvent']),
77
            new TwigFilter('simpleAnalyticsEvent', [$this, 'simpleEvent']),
78
            new TwigFilter('pageViewTrackingUrl', [$this, 'pageViewTrackingUrl']),
79
            new TwigFilter('eventTrackingUrl', [$this, 'eventTrackingUrl']),
80
        ];
81
    }
82
83
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
84
     * @inheritdoc
85
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
86
    public function getFunctions(): array
87
    {
88
        return [
89
            new TwigFunction('pageViewEvent', [$this, 'pageViewEvent']),
90
            new TwigFunction('simpleAnalyticsEvent', [$this, 'simpleEvent']),
91
            new TwigFunction('pageViewTrackingUrl', [$this, 'pageViewTrackingUrl']),
92
            new TwigFunction('eventTrackingUrl', [$this, 'eventTrackingUrl']),
93
        ];
94
    }
95
96
    /**
97
     * Get a PageView analytics object
98
     *
99
     * @param string $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
100
     * @param string $title
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
101
     *
102
     * @return PageViewEvent object
103
     */
104
    public function pageViewEvent(string $url = '', string $title = ''): PageViewEvent
105
    {
106
        return InstantAnalytics::$plugin->ga4->getPageViewEvent($url, $title);
107
    }
108
109
    /**
110
     * Get an Event analytics object
111
     *
112
     * @param string $eventName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
113
     * @return BaseEvent
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
114
     */
115
    public function simpleEvent(string $eventName = ''): BaseEvent
116
    {
117
        return InstantAnalytics::$plugin->ga4->getSimpleEvent($eventName);
118
    }
119
120
    /**
121
     * Get a PageView tracking URL
122
     *
123
     * @param $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
124
     * @param $title
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
125
     *
126
     * @return Markup
127
     * @throws Exception
128
     */
129
    public function pageViewTrackingUrl($url, $title): Markup
130
    {
131
        return Template::raw(Analytics::getPageViewTrackingUrl($url, $title));
132
    }
133
134
    /**
135
     * Get an Event tracking URL
136
     *
137
     * @param string $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
138
     * @param string $eventName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
139
     * @param array $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
140
     * @return Markup
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
141
     * @throws Exception
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
142
     */
143
    public function eventTrackingUrl(
144
        string $url,
145
        string $eventName,
146
        array $params = [],
147
    ): Markup {
148
        return Template::raw(Analytics::getEventTrackingUrl($url, $eventName, $params));
149
    }
150
}
151