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 |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\instantanalytics\controllers; |
12
|
|
|
|
13
|
|
|
use craft\web\Controller; |
14
|
|
|
use nystudio107\instantanalytics\InstantAnalytics; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* TrackController |
18
|
|
|
* |
19
|
|
|
* @author nystudio107 |
|
|
|
|
20
|
|
|
* @package InstantAnalytics |
|
|
|
|
21
|
|
|
* @since 1.0.0 |
|
|
|
|
22
|
|
|
*/ |
|
|
|
|
23
|
|
|
class TrackController extends Controller |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
// Protected Properties |
27
|
|
|
// ========================================================================= |
28
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @var bool|array Allows anonymous access to this controller's actions. |
31
|
|
|
* The actions must be in 'kebab-case' |
32
|
|
|
* @access protected |
33
|
|
|
*/ |
34
|
|
|
protected $allowAnonymous = [ |
35
|
|
|
'track-page-view-url', |
36
|
|
|
'track-event-url' |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
// Public Methods |
40
|
|
|
// ========================================================================= |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @param string $url |
|
|
|
|
44
|
|
|
* @param string $title |
|
|
|
|
45
|
|
|
*/ |
|
|
|
|
46
|
|
|
public function actionTrackPageViewUrl(string $url, string $title) |
47
|
|
|
{ |
48
|
|
|
$analytics = InstantAnalytics::$plugin->ia->pageViewAnalytics($url, $title); |
49
|
|
|
if ($analytics !== null) { |
50
|
|
|
$analytics->sendPageview(); |
51
|
|
|
} |
52
|
|
|
$this->redirect($url, 200); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* @param string $url |
|
|
|
|
57
|
|
|
* @param string $eventCategory |
|
|
|
|
58
|
|
|
* @param string $eventAction |
|
|
|
|
59
|
|
|
* @param string $eventLabel |
|
|
|
|
60
|
|
|
* @param int $eventValue |
|
|
|
|
61
|
|
|
*/ |
|
|
|
|
62
|
|
|
public function actionTrackEventUrl( |
63
|
|
|
string $url, |
64
|
|
|
string $eventCategory, |
65
|
|
|
string $eventAction, |
66
|
|
|
string $eventLabel, |
67
|
|
|
int $eventValue |
68
|
|
|
) |
69
|
|
|
{ |
|
|
|
|
70
|
|
|
$analytics = InstantAnalytics::$plugin->ia->eventAnalytics( |
71
|
|
|
$eventCategory, |
72
|
|
|
$eventAction, |
73
|
|
|
$eventLabel, |
74
|
|
|
$eventValue |
75
|
|
|
); |
76
|
|
|
// Get the file name |
77
|
|
|
$path = parse_url($url, PHP_URL_PATH); |
78
|
|
|
if ($analytics !== null) { |
79
|
|
|
$analytics->setDocumentPath($path) |
80
|
|
|
->sendEvent(); |
81
|
|
|
} |
82
|
|
|
$this->redirect($url, 200); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|