I've been testing this module for two weeks now and I noticed that it never resets week counter. I guess that the same may be true for month counter or year counter.
This happens because counters are reset when variable statistics_day_timestamp shows different week (or month or year) than current week (or month or year). The problem is that this variable is updated by the statistics module as soon as statistics module updates its core counters. Function statistics_counter_cron is set to run after statistics_cron, so it seems that inside statistics_counter_cron value of statistics_day_timestamp always shows current timestamp, hence week (or month or year) change is never detected.
The solution would be to make sure that statistics_day_timestamp is run before statistics_cron.
So instead of this
/**
* Implements hook_module_implements_alter().
*/
function statistics_counter_module_implements_alter(&$implementations, $hook) {
$module_name = 'statistics_counter';
switch ($hook) {
case 'cron':
case 'exit':
if (isset($implementations[$module_name])) {
unset($implementations[$module_name]);
$implementations[$module_name] = $hook;
}
break;
}
}
we need this:
/**
* Implements hook_module_implements_alter().
*/
function statistics_counter_module_implements_alter(&$implementations, $hook) {
$module_name = 'statistics_counter';
switch ($hook) {
case 'cron':
case 'exit':
if (isset($implementations[$module_name])) {
$new = array($module_name => $implementations[$module_name]);
unset($implementations[$module_name]);
$implementations = $new + $implementations;
}
break;
}
}
Comment | File | Size | Author |
---|---|---|---|
#1 | statistics_counter-fix-counter-reset-2211099-1.patch | 593 bytes | SiliconMind |
Comments
Comment #1
SiliconMind CreditAttribution: SiliconMind commentedI guess I was right. Patch included.
Comment #2
Proteo CreditAttribution: Proteo commentedI'm having this exact issue. Applied the patch, but it didn't make any difference, then realized that... according to the readme file, the module keeps a record of "views this week". Just to be sure: what's exactly "this week"? The last 7 days, the days from the last monday, or the days from the first day of the week set in Drupal?
If "this week" means the last 7 days, the counter shouldn't be reset to begin with, I think.
Comment #3
SiliconMind CreditAttribution: SiliconMind commentedProteo, the original code does not change week counters at all. They keep adding and adding for weeks, just like normal page counter provided by the statistics module. If you take a look at my issue description and the module's source code, then you should understand what I'm talking about. The source code resets week counter if current week is different than the one stored by the statistics module. The problem is that the module runs after statistics module, so when this module runs, the current week variable created by statistics module is already updated. Hence this module "thinks", that week did not change and there is no need to reset week counters.
Take a look at
statistics_counter_cron()
:Comment #4
Proteo CreditAttribution: Proteo commentedI see, thanks for the clarification. As I mentioned before, the patch had no effect in the week count of a live site. I'm using ultimate cron to run the regular statistics job at 12:05, then statistics counter at 12:10. I tried it just a few minutes after today's midnight (which is monday).
Do you think it's necessary to use ISO-8601 weeks in /admin/config/regional/settings?
Best regards.
Comment #5
SiliconMind CreditAttribution: SiliconMind commentedProteo, if you specifically make regular statistics cron to run BEFORE statistics counter, then it wont work. The point of this patch is to make regular statistics to run AFTER statistics counter. It will never work the other way around.
In other words, you have to make sure that statistics counter cron job runs first, possibly just after midnight.
Comment #6
stephencross CreditAttribution: stephencross commentedI'm considering installing this module. It's not clear to me what needs to be done to make sure the counting is done properly. Do I need this patch, change cron or what?
I would like to make sure this works properly from install and not wait a week to see the results.
Thank you.
-S
Comment #7
JordanMagnuson CreditAttribution: JordanMagnuson commentedSeems that it would be simpler and more foolproof to just implement our own timestamp lookups separate from the statistics module.
See hook_cron() from the similar statistics_granularity module:
Comment #8
JordanMagnuson CreditAttribution: JordanMagnuson commentedThere's really no reason that resetting the aggregated counts should be tied to the core statistics module.
Comment #9
thmnhat CreditAttribution: thmnhat commentedHi, sorry for late reply. I agree that we should use different timestamp for this module. I've pushed a fix, please help to review and test the dev version
Comment #10
thmnhat CreditAttribution: thmnhat commentedComment #11
stefanotabarelli CreditAttribution: stefanotabarelli commentedI have used the dev version for a week and the counter seems to reset correctly now. I will suggest to include this improvement in the next release.
Comment #12
thmnhat CreditAttribution: thmnhat commented