forked from bolt/GoogleAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtension.php
More file actions
164 lines (121 loc) · 5.5 KB
/
Copy pathExtension.php
File metadata and controls
164 lines (121 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
// Google Analytics extension for Bolt
namespace Bolt\Extension\Bolt\GoogleAnalytics;
use Bolt\Translation\Translator as Trans;
use Symfony\Component\HttpFoundation\Response,
Symfony\Component\Translation\Loader as TranslationLoader;
use Bolt\Extensions\Snippets\Location as SnippetLocation;
class Extension extends \Bolt\BaseExtension
{
public function getName()
{
return "Google Analytics";
}
function initialize() {
$this->path = $this->app['config']->get('general/branding/path') . '/extensions/google-analytics';
$this->app->match($this->path, array($this, 'GoogleAnalytics'));
$this->app['htmlsnippets'] = true;
if ($this->app['config']->getWhichEnd()=='frontend') {
$this->addSnippet('endofhead', 'insertAnalytics');
} else {
$this->app->before(array($this, 'before'));
}
if (isset($this->config['backend']) && $this->config['backend']) {
$this->addMenuOption(Trans::__('Statistics'), $this->app['paths']['bolt'] . 'extensions/google-analytics', "fa:area-chart");
}
}
public function before()
{
$this->translationDir = __DIR__.'/locales/' . substr($this->app['locale'], 0, 2);
if (is_dir($this->translationDir))
{
$iterator = new \DirectoryIterator($this->translationDir);
foreach ($iterator as $fileInfo)
{
if ($fileInfo->isFile())
{
$this->app['translator']->addLoader('yml', new TranslationLoader\YamlFileLoader());
$this->app['translator']->addResource('yml', $fileInfo->getRealPath(), $this->app['locale']);
}
}
}
}
public function GoogleAnalytics()
{
$this->addJavascript('assets/es6-promise.min.js', 1);
$this->addJavascript('assets/active-users.js', 1);
$this->addJavascript('assets/date-range-selector.js', 1);
$this->addJavascript('assets/moment-with-locales.min.js', 1);
$this->addJavascript('assets/Chart.min.js', 1);
$this->addJavascript('assets/googleanalytics.js', array('late' => true, 'priority' => 1000));
$this->addCss('assets/styles.css', 1);
$data = [
"locale" => substr($this->app['locale'], 0, 2),
"token" => $this->getService(),
"profile" => $this->config['ga_profile_id']
];
$this->app['twig.loader.filesystem']->addPath(__DIR__.'/views/', 'GoogleAnalytics');
$html = $this->app['render']->render("@GoogleAnalytics/base.twig", $data);
return new Response($html);
}
public function insertAnalytics()
{
if (empty($this->config['webproperty'])) {
$this->config['webproperty'] = "property-not-set";
}
if ($this->config['universal']) {
$html = <<< EOM
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '%webproperty%', '%domainname%');
ga('send', 'pageview');
</script>
EOM;
} else {
$html = <<< EOM
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '%webproperty%']);
_gaq.push(['_setDomainName', '%domainname%']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
EOM;
}
$html = str_replace("%webproperty%", $this->config['webproperty'], $html);
$html = str_replace("%domainname%", ( $this->config['universal'] ? $this->config['universal_domainname'] : $_SERVER['HTTP_HOST'] ), $html);
return new \Twig_Markup($html, 'UTF-8');
}
private function getService()
{
if (empty($this->config['service_account_email'])) { return "service_account_email not set in config.yml."; }
if (empty($this->config['key_file_location'])) { return "key_file_location not set in config.yml."; }
if (empty($this->config['ga_profile_id'])) { return "ga_profile_id not set in config.yml."; }
require_once(__DIR__.'/Google/autoload.php');
$service_account_email = $this->config['service_account_email']; //Email Address
$key_file_location = $this->config['key_file_location']; //key.p12
// Create and configure a new client object.
$client = new \Google_Client();
$client->setApplicationName("HelloAnalytics");
$analytics = new \Google_Service_Analytics($client);
// Read the generated client_secrets.p12 key.
$key = file_get_contents($key_file_location);
$cred = new \Google_Auth_AssertionCredentials(
$service_account_email,
array(\Google_Service_Analytics::ANALYTICS_READONLY),
$key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
return $client->getAccessToken();
}
}