Skip to content

Commit

Permalink
This Month vs Last Month option added in analytics stats view - ADDED
Browse files Browse the repository at this point in the history
  • Loading branch information
kprajapatii committed Oct 15, 2019
1 parent d4709fa commit 793ed20
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
87 changes: 87 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ function geodir_ga_get_analytics( $page, $ga_start, $ga_end ) {
$start_date = date('Y-m-d', strtotime("-13 day"));
$end_date = date('Y-m-d', strtotime("-7 day"));
$dimensions = "ga:date,ga:nthDay";
} elseif (isset($_REQUEST['ga_type']) && $_REQUEST['ga_type'] == 'thismonth') {
$start_date = date('Y-m-01');
$end_date = date('Y-m-d');
$dimensions = "ga:date,ga:nthDay";
} elseif (isset($_REQUEST['ga_type']) && $_REQUEST['ga_type'] == 'lastmonth') {
$start_date = date('Y-m-01', strtotime("-1 month"));
$end_date = date('Y-m-t', strtotime("-1 month"));
$dimensions = "ga:date,ga:nthDay";
} elseif (isset($_REQUEST['ga_type']) && $_REQUEST['ga_type'] == 'thisyear') {
$start_date = date('Y')."-01-01";
$end_date = date('Y-m-d');
Expand Down Expand Up @@ -182,6 +190,11 @@ function geodir_ga_display_analytics($args = array()) {
}

$id = trim( geodir_get_option( 'ga_account_id' ) );
$month_last_day = max( (int) date( 't' ), (int) date( 't', strtotime( '-1 month' ) ) );
$month_days = array();
for ( $d = 1; $d <= $month_last_day; $d++ ) {
$month_days[] = $d;
}

if ( ! $id ) {
return; // if no Google Analytics ID then bail.
Expand Down Expand Up @@ -264,6 +277,19 @@ function gdga_weekVSweek() {
}});
}

function gdga_monthVSmonth() {
jQuery.ajax({url: "<?php echo admin_url('admin-ajax.php?action=geodir_ga_stats&ga_page='.$page_url.'&ga_type=thismonth'); ?>", success: function(result){
ga_data1 = jQuery.parseJSON(result);
if(ga_data1.error){jQuery('#ga_stats').html(result);return;}
gd_renderMonthOverMonthChart();
}});

jQuery.ajax({url: "<?php echo admin_url('admin-ajax.php?action=geodir_ga_stats&ga_page='.$page_url.'&ga_type=lastmonth'); ?>", success: function(result){
ga_data2 = jQuery.parseJSON(result);
gd_renderMonthOverMonthChart();
}});
}

function gdga_yearVSyear() {
jQuery.ajax({url: "<?php echo admin_url('admin-ajax.php?action=geodir_ga_stats&ga_page='.$page_url.'&ga_type=thisyear'); ?>", success: function(result){
ga_data3 = jQuery.parseJSON(result);
Expand Down Expand Up @@ -517,6 +543,64 @@ function gd_renderWeekOverWeekChart() {
});
}

function gd_renderMonthOverMonthChart() {
if(ga_data1 && ga_data2){
thisMonth = ga_data1;
lastMonth = ga_data2;
ga_data1 = false;
ga_data2 = false;
}else{
return;
}

jQuery('#gdga-chart-container').show();
jQuery('#gdga-legend-container').show();
gdga_refresh(true);
jQuery('.gdga-type-container').show();
jQuery('#gdga-select-analytic').prop('disabled', false);

// Adjust `now` to experiment with different days, for testing only...
var now = moment();

Promise.all([thisMonth, lastMonth]).then(function(results) {
var data1 = results[0].rows.map(function(row) { return +row[2]; });
var data2 = results[1].rows.map(function(row) { return +row[2]; });
var labels = results[1].rows.map(function(row) { return +row[0]; });

labels = [<?php echo implode( ",", $month_days ) ?>];

for (var i = 0, len = labels.length; i < len; i++) {
if (data1[i] === undefined) data1[i] = null;
if (data2[i] === undefined) data2[i] = 0;
}

var data = {
labels : labels,
datasets : [
{
label: '<?php _e('Last Month', 'geodir-ga');?>',
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
data : data2
},
{
label: '<?php _e('This Month', 'geodir-ga');?>',
fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,1)",
pointColor : "rgba(151,187,205,1)",
pointStrokeColor : "#fff",
data : data1
}
]
};

new Chart(makeCanvas('gdga-chart-container')).Line(data);
generateLegend('gdga-legend-container', data.datasets);
});
}

/**
* Create a new canvas inside the specified element. Set it to be the width
* and height of its container.
Expand Down Expand Up @@ -559,6 +643,8 @@ function gdga_select_option() {

if (gaType == 'weeks') {
gdga_weekVSweek();
} else if (gaType == 'months') {
gdga_monthVSmonth();
} else if (gaType == 'years') {
gdga_yearVSyear();
} else if (gaType == 'country') {
Expand Down Expand Up @@ -622,6 +708,7 @@ function gdga_refresh(stop) {
<div class="gdga-type-container" style="display:none">
<select id="gdga-select-analytic" class="geodir-select" onchange="gdga_select_option();">
<option value="weeks"><?php _e("Last Week vs This Week", 'geodir-ga');?></option>
<option value="months"><?php _e("This Month vs Last Month", 'geodir-ga');?></option>
<option value="years"><?php _e("This Year vs Last Year", 'geodir-ga');?></option>
<option value="country"><?php _e("Top Countries", 'geodir-ga');?></option>
</select>
Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ An answer to that question.

== Changelog ==

= 2.0.0.5 =
* This Month vs Last Month option added in analytics stats view - ADDED

= 2.0.0.4 =
* Analytics widget can now be placed on any page and shown to anyone - ADDED

Expand Down

0 comments on commit 793ed20

Please sign in to comment.