mirror of
https://github.com/monstra-cms/monstra.git
synced 2025-08-03 19:57:57 +02:00
google analytics init
This commit is contained in:
@@ -1,5 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
// Add Plugin Javascript
|
||||||
|
Javascript::add('plugins/box/dashboard/js/ganalytics.js', 'backend');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dashboard admin class
|
* Dashboard admin class
|
||||||
*/
|
*/
|
||||||
@@ -11,9 +14,49 @@ class DashboardAdmin extends Backend
|
|||||||
public static function main()
|
public static function main()
|
||||||
{
|
{
|
||||||
|
|
||||||
// Display view
|
// set/update google analytics settings
|
||||||
View::factory('box/dashboard/views/backend/index')->display();
|
if (Request::post('ga_settings_update')) {
|
||||||
|
|
||||||
|
if (Security::check(Request::post('csrf'))) {
|
||||||
|
|
||||||
|
// client id
|
||||||
|
$ga_client_id = trim(Request::post('ga_client_id'));
|
||||||
|
if (!empty($ga_client_id)) {
|
||||||
|
$opt_client_id = Option::get('ga_client_id');
|
||||||
|
if (empty($opt_client_id)) {
|
||||||
|
Option::add('ga_client_id', $ga_client_id);
|
||||||
|
} else {
|
||||||
|
Option::update('ga_client_id', $ga_client_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// API key
|
||||||
|
$ga_api_key = trim(Request::post('ga_api_key'));
|
||||||
|
if (!empty($ga_api_key)) {
|
||||||
|
$opt_api_key = Option::get('ga_api_key');
|
||||||
|
if (empty($opt_api_key)) {
|
||||||
|
Option::add('ga_api_key', $ga_api_key);
|
||||||
|
} else {
|
||||||
|
Option::update('ga_api_key', $ga_api_key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// view id
|
||||||
|
$ga_view_id = trim(Request::post('ga_view_id'));
|
||||||
|
if (!empty($ga_view_id)) {
|
||||||
|
$opt_view_id = Option::get('ga_view_id');
|
||||||
|
if (empty($opt_view_id)) {
|
||||||
|
Option::add('ga_view_id', $ga_view_id);
|
||||||
|
} else {
|
||||||
|
Option::update('ga_view_id', $ga_view_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Display view
|
||||||
|
View::factory('box/dashboard/views/backend/index')->display();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
149
plugins/box/dashboard/js/ganalytics.js
Normal file
149
plugins/box/dashboard/js/ganalytics.js
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
if (typeof $.monstra == 'undefined') $.monstra = {};
|
||||||
|
|
||||||
|
function glibOnloadHandle(){$.monstra.ganalytics.libOnloadHandle();}
|
||||||
|
|
||||||
|
$.monstra.ganalytics = {
|
||||||
|
|
||||||
|
conf: {
|
||||||
|
clientId: '',
|
||||||
|
apiKey: '',
|
||||||
|
viewId: '',
|
||||||
|
authScopes: 'https://www.googleapis.com/auth/analytics.readonly'
|
||||||
|
},
|
||||||
|
|
||||||
|
_gaAreas: '#authOk,#authFail,#gaSettings,#gaLoading',
|
||||||
|
|
||||||
|
init: function(data){
|
||||||
|
$.extend(this.conf, data);
|
||||||
|
},
|
||||||
|
|
||||||
|
libOnloadHandle: function(){
|
||||||
|
if ($.monstra.ganalytics.conf.clientId == ''
|
||||||
|
|| $.monstra.ganalytics.conf.apiKey == ''
|
||||||
|
|| $.monstra.ganalytics.conf.viewId == ''
|
||||||
|
) {
|
||||||
|
$.monstra.ganalytics.show('#gaSettings');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
gapi.client.setApiKey(this.conf.apiKey);
|
||||||
|
window.setTimeout(function(){
|
||||||
|
$.monstra.ganalytics.checkAuth(true);
|
||||||
|
},1);
|
||||||
|
},
|
||||||
|
|
||||||
|
checkAuth: function(immediate){
|
||||||
|
gapi.auth.authorize({
|
||||||
|
client_id: $.monstra.ganalytics.conf.clientId,
|
||||||
|
scope: $.monstra.ganalytics.conf.authScopes,
|
||||||
|
immediate: immediate
|
||||||
|
}, $.monstra.ganalytics.handleAuthResult);
|
||||||
|
return immediate;
|
||||||
|
},
|
||||||
|
|
||||||
|
handleAuthResult: function(authResult){
|
||||||
|
if (authResult && !authResult.error) {
|
||||||
|
$.monstra.ganalytics.show('#authOk');
|
||||||
|
$.monstra.ganalytics.getAnalyticsInfo();
|
||||||
|
} else {
|
||||||
|
$.monstra.ganalytics.show('#authFail');
|
||||||
|
if (authResult && typeof authResult.error != 'undefined') {
|
||||||
|
$.monstra.ganalytics.showError(authResult.error.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
$('#authorizeButton').on('click', function(e){
|
||||||
|
$.monstra.ganalytics.checkAuth(false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
getAnalyticsInfo: function() {
|
||||||
|
gapi.client.load('analytics', 'v3', function(){
|
||||||
|
var dateOffset = (24*60*60*1000) * 30;
|
||||||
|
var cdate = new Date();
|
||||||
|
var mdate = new Date(cdate.getTime() - dateOffset);
|
||||||
|
gapi.client.analytics.data.ga.get({
|
||||||
|
'ids': 'ga:'+ $.monstra.ganalytics.conf.viewId,
|
||||||
|
'start-date': $.monstra.ganalytics.formatDate(mdate),
|
||||||
|
'end-date': $.monstra.ganalytics.formatDate(cdate),
|
||||||
|
'metrics': 'ga:visits,ga:pageviews,ga:visitors',
|
||||||
|
'dimensions': 'ga:date'
|
||||||
|
}).execute($.monstra.ganalytics.gaReportingResults);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
gaReportingResults: function(res){
|
||||||
|
if (typeof res.error != 'undefined' && typeof res.error.message != 'undefined') {
|
||||||
|
$.monstra.ganalytics.showError(res.error.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// build chart data
|
||||||
|
var dataArr = [['Date', 'Visits']];
|
||||||
|
for (r in res.rows) {
|
||||||
|
var tmpr = [];
|
||||||
|
for (h in res.columnHeaders) {
|
||||||
|
if (res.columnHeaders[h].name == 'ga:visits') {
|
||||||
|
tmpr[1] = parseInt(res.rows[r][h]);
|
||||||
|
} else if (res.columnHeaders[h].name == 'ga:date') {
|
||||||
|
tmpr[0] = res.rows[r][h];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res.rows.length == (parseInt(r)+1)) {
|
||||||
|
switch(res.columnHeaders[h].name) {
|
||||||
|
case 'ga:visits': $.monstra.ganalytics.setVisits(res.rows[r][h]); break;
|
||||||
|
case 'ga:pageviews': $.monstra.ganalytics.setPageviews(res.rows[r][h]); break;
|
||||||
|
case 'ga:visitors': $.monstra.ganalytics.setVisitors(res.rows[r][h]); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dataArr.push(tmpr);
|
||||||
|
}
|
||||||
|
|
||||||
|
var data = google.visualization.arrayToDataTable(dataArr);
|
||||||
|
|
||||||
|
var options = {
|
||||||
|
title: 'Visits',
|
||||||
|
hAxis: {title: 'Date', titleTextStyle: {color: '#333'}},
|
||||||
|
vAxis: {minValue: 0}
|
||||||
|
};
|
||||||
|
|
||||||
|
var chart = new google.visualization.AreaChart(document.getElementById('gaChart'));
|
||||||
|
chart.draw(data, options);
|
||||||
|
|
||||||
|
},
|
||||||
|
|
||||||
|
formatDate: function(dateObj){
|
||||||
|
var m = dateObj.getMonth()+1;
|
||||||
|
var d = dateObj.getDate();
|
||||||
|
m = m > 9 ? m : '0'+m;
|
||||||
|
d = d > 9 ? d : '0'+d;
|
||||||
|
return dateObj.getFullYear() +'-'+ m +'-'+ d;
|
||||||
|
},
|
||||||
|
|
||||||
|
show: function(selector){
|
||||||
|
$('#gaAlerts').html('');
|
||||||
|
$($.monstra.ganalytics._gaAreas).addClass('hide');
|
||||||
|
$(selector).removeClass('hide').show();
|
||||||
|
},
|
||||||
|
|
||||||
|
showError: function(msg){
|
||||||
|
$('#gaAlerts').html(msg);
|
||||||
|
},
|
||||||
|
|
||||||
|
setVisits: function(val){
|
||||||
|
$('#gaVisits').html(val);
|
||||||
|
},
|
||||||
|
|
||||||
|
setVisitors: function(val){
|
||||||
|
$('#gaVisitors').html(val);
|
||||||
|
},
|
||||||
|
|
||||||
|
setPageviews: function(val){
|
||||||
|
$('#gaPageviews').html(val);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$(document).ready(function(){
|
||||||
|
$.monstra.ganalytics.init($.parseJSON($('#gaInitData').val()));
|
||||||
|
});
|
||||||
|
|
@@ -15,12 +15,71 @@
|
|||||||
<li><?php echo ( Html::anchor(__('Snippets', 'snippets'), 'index.php?id=snippets&action=add_snippet', array('title' => __('Snippet', 'pages')))); ?></li>
|
<li><?php echo ( Html::anchor(__('Snippets', 'snippets'), 'index.php?id=snippets&action=add_snippet', array('title' => __('Snippet', 'pages')))); ?></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<?php echo ( Html::anchor(__('Upload File', 'dashboard'), 'index.php?id=filesmanager', array('title' => __('Upload File', 'dashboard'), 'class' => 'btn btn-primary'))); ?>
|
<?php echo ( Html::anchor(__('Upload File', 'filesmanager'), 'index.php?id=filesmanager', array('title' => __('Upload File', 'filesmanager'), 'class' => 'btn btn-primary'))); ?>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
google.load("visualization", "1", {packages:["corechart"]});
|
||||||
|
</script>
|
||||||
|
<input type="hidden" id="gaInitData" value='<?php echo json_encode(array(
|
||||||
|
'clientId' => Option::get('ga_client_id'),
|
||||||
|
'apiKey' => Option::get('ga_api_key'),
|
||||||
|
'viewId' => Option::get('ga_view_id')
|
||||||
|
)); ?>' />
|
||||||
|
<script src="https://apis.google.com/js/client.js?onload=glibOnloadHandle"></script>
|
||||||
|
|
||||||
|
<div class="well dashboard-well">
|
||||||
|
<div class="row"><div class="col-md-12"><h4><?php echo __('Goggle Analytics', 'system'); ?></h4></div></div>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-8">
|
||||||
|
|
||||||
|
<div id="gaAlerts" class="alert-warning"></div>
|
||||||
|
|
||||||
|
<div id="gaLoading">
|
||||||
|
spinner here ?
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="authOk" class="hide">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-8">
|
||||||
|
<div id="gaChart"></div>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-4">
|
||||||
|
Today:<br/>
|
||||||
|
Visits:<span id="gaVisits"></span><br/>
|
||||||
|
Visitors:<span id="gaVisitors"></span><br/>
|
||||||
|
Pageviews:<span id="gaPageviews"></span>
|
||||||
|
</div>
|
||||||
|
<div/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="authFail" class="hide">
|
||||||
|
<button class="btn btn-default" id="authorizeButton"><?php echo __('Authorize', 'system'); ?></button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="gaSettings" class="hide">
|
||||||
|
<form method="POST">
|
||||||
|
<?php echo Form::hidden('csrf', Security::token()); ?>
|
||||||
|
|
||||||
|
<label><?php echo __('Client ID', 'system'); ?><input name="ga_client_id" value="<?php echo Option::get('ga_client_id'); ?>" placeholder="<?php echo __('Client ID', 'system'); ?>" /></label>
|
||||||
|
|
||||||
|
<label><?php echo __('API key', 'system'); ?><input name="ga_api_key" value="<?php echo Option::get('ga_api_key'); ?>" placeholder="<?php echo __('API key', 'system'); ?>" /></label>
|
||||||
|
|
||||||
|
<label><?php echo __('View ID', 'system'); ?><input name="ga_view_id" value="<?php echo Option::get('ga_view_id'); ?>" placeholder="<?php echo __('View ID', 'system'); ?>" /></label>
|
||||||
|
|
||||||
|
<input type="hidden" name="ga_settings_update" value="1" />
|
||||||
|
<button type="submit" class="btn btn-default"><?php echo __('Save', 'system'); ?></button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="well dashboard-well">
|
<div class="well dashboard-well">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-3">
|
<div class="col-md-3">
|
||||||
|
@@ -1 +1,12 @@
|
|||||||
<!-- Place Google Analytics code here... -->
|
<script type="text/javascript">
|
||||||
|
(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','_mga');
|
||||||
|
|
||||||
|
_mga('create', '<?php echo Option::get('ga_tracking_id'); ?>', 'auto');
|
||||||
|
_mga('send', 'pageview', {
|
||||||
|
'page': '<?php echo Url::current(); ?>',
|
||||||
|
'title': '<?php echo Site::title(); ?>'
|
||||||
|
});
|
||||||
|
</script>
|
Reference in New Issue
Block a user