You are here:  » Clicks daily

Active Forum Topics


Clicks daily

Submitted by MarcoCH on Sat, 2019-02-02 13:47 in

Hello

I would like to save the daily clicks in a .txt file or admin area, is that possible?

Thanks and regards
Marco

Submitted by support on Mon, 2019-02-04 10:06

Hi Marco,

Sure - if you don't mind the clicks being reset to zero daily it's no problem to log the clicks per feed to a file. If you create the following as scripts/clicks.php

<?php
  
require("../includes/common.php");
  
$fp fopen($config_feedDirectory."clicks_".date("Y-m-d").".txt","w");
  
$sql "SELECT filename,clicks FROM `".$config_databaseTablePrefix."feeds`";
  
database_querySelect($sql,$feeds);
  foreach(
$feeds as $feed)
  {
    
fwrite($fp,$feed["filename"].",".$feed["clicks"]."\n");
  }
  
fclose($fp);
  
$sql "UPDATE `".$config_databaseTablePrefix."feeds` SET clicks='0'";
  
database_queryModify($sql,$result);
?>

...and then schedule that to run as a cronjob as close to midnight as possible (but not at or over) and this will output the daily clicks to a file in the feeds directory for example:

clicks_2019-02-04.txt

I've used the feeds directory is it would normally be set-up to be writeable by PHP but if you'd prefer to use an alternative location just replace $config_feedDirectory with an appropriate path e.g.

  $fp = fopen("clicks/clicks_".date("Y-m-d").".txt","w");

...which in this case would be a clicks/ sub-directory of the scripts folder.

Make sure that the location is writable by PHP (set to the same mode as the feeds/ folders), and when setting up your cronjob, don't forget to include a cd command so that the relative location can be found just as for cron.php e.g.

cd /home/username/public_html/scripts/;/usr/bin/php clicks.php

Cheers,
David.
--
PriceTapestry.com

Submitted by MarcoCH on Fri, 2019-02-08 19:57

Hello

Thank you for the help, works so far.
Can I spend the last 10 maybe on the admin page.
So I see a big change.

regards
Marco

Submitted by support on Sat, 2019-02-09 12:04

Hi Marco,

To add last 10 days clicks to admin home page, edit admin/index.php and look for the following code at line 244:

  require("admin_footer.php");

...and REPLACE with:

  $clicks = array();
  $date = time();
  for($i=1;$i<=10;$i++)
  {
    $date -= 86400;
    $filename = $config_feedDirectory."clicks_".date("Y-m-d",$date).".txt";
    $heading = date("m-d",$date);
    if (file_exists($filename))
    {
      $fp = fopen($filename,"r");
      while($row = fgetcsv($fp))
      {
        $feed = $row[0];
        $clicks[$feed][$heading] = $row[1];
      }
    }
  }
  if (count($clicks))
  {
    print "<table>";
    print "<tr>";
    print "<th>&nbsp;</th>";
    foreach($clicks[$feed] as $heading => $v)
    {
      print "<th>".$heading."</th>";
    }
    print "</tr>";
    foreach($clicks as $feed => $row)
    {
      print "<tr>";
      print "<th class='pta_key'>".$feed."</th>";
      foreach($row as $v)
      {
        print "<td class='pta_num'>".$v."</td>";
      }
      print "</tr>";
    }
    print "</table>";
  }
  require("admin_footer.php");

(if you're saving clicks to a directory other than the feed directory make the same change to $config_feedDirectory in line 5 of the above replacement as in the clicks.php script)

Cheers,
David.
--
PriceTapestry.com

Submitted by tommo101 on Fri, 2019-04-26 23:46

Hi there,

I've also implemented this modification as I like the sound of seeing daily clicks rather than the forever increasing click stats that normally show by default.

I set it up yesterday and the first file was created perfectly and all the clicks have reset to 0 as designed.

I'm having an issue with it displaying the results though, i'm getting the following error and can't get my head round it:

PHP Warning: Illegal offset type in (path would be here) on line 264
PHP Warning: Invalid argument supplied for foreach() in (path would be here) on line 264

Which is saying would be this line:

foreach($clicks[$feed] as $heading => $v)

Could you possibly help me with this one please.

Submitted by support on Mon, 2019-04-29 07:45

Hi,

The output section should have been contained within an IF condition so that it only shows if the $clicks array has been populated - i've added that above. That would indicate that the files may not be accumulating correctly (the error would only have appeared until a clicks_[yesterday].txt had been created so it may have resolved itself now but if the daily clicks are still not showing let me know and I'll check it out further with you...

Cheers,
David.
--
PriceTapestry.com

Submitted by tommo101 on Mon, 2019-04-29 12:36

No, sadly not, I'm still getting the same error as above.

The clicks file has been successfully created for 3 days now.

The Clicks get reset as they should on the admin index and the Total Clicks do reflect the correct amount of clicks happening each day still.

It's simply not outputting the contents of the file as a top 10 daily clicks thing in a table as it should be.

Submitted by support on Mon, 2019-04-29 13:13

Hi,

This would sound like file_exists() is not behaving as expected and I have come across this before, if you are running PHP on Windows this might be the case - try without the check using the result of fopen() instead - here's a complete replacement of the display code from above to use in admin/index.php

  $clicks = array();
  $date = time();
  for($i=1;$i<=10;$i++)
  {
    $date -= 86400;
    $filename = $config_feedDirectory."clicks_".date("Y-m-d",$date).".txt";
    $heading = date("m-d",$date);
    if ($fp = fopen($filename,"r"))
    {
      while($row = fgetcsv($fp))
      {
        $feed = $row[0];
        $clicks[$feed][$heading] = $row[1];
      }
    }
  }
  if (count($clicks))
  {
    print "<table>";
    print "<tr>";
    print "<th>&nbsp;</th>";
    foreach($clicks[$feed] as $heading => $v)
    {
      print "<th>".$heading."</th>";
    }
    print "</tr>";
    foreach($clicks as $feed => $row)
    {
      print "<tr>";
      print "<th class='pta_key'>".$feed."</th>";
      foreach($row as $v)
      {
        print "<td class='pta_num'>".$v."</td>";
      }
      print "</tr>";
    }
    print "</table>";
  }
  require("admin_footer.php");

If still no joy, are you using the default $config_feedDirectory (config.advanced.php) of "../feeds/"?

Cheers,
David.
--
PriceTapestry.com

Submitted by tommo101 on Mon, 2019-04-29 14:31

That kind of worked, but has made a mess of the formatting of the entire page.

It is now displaying the clicks for every feed regardless of whether there has been clicks or not and their daily clicks for the last 3 days, but is't also displaying some weird info afterwards.

It's also shifted the entire page to the left hand side, not centered as normal.

The after bits it is showing is:

follow" />
uk
initial-scale=1.0">
300

and a load of empty table cells

For info purposes, I am not using the default feeds directory. But I had edited the setting to that so that it does find the file before attempting to display results.

Submitted by support on Tue, 2019-04-30 07:33

Hi,

It looks like something in the mod has broken the page markup - could you perhaps post the full code you have added to admin/index.php and I'll take a look / recreate your access to the clicks file on my test server and check it out further for you...

Cheers,
David.
--
PriceTapestry.com

Submitted by tommo101 on Tue, 2019-04-30 09:03

This is my entire admin/index.php:

{code saved}

Submitted by support on Tue, 2019-04-30 09:52

Hi,

Ah - I see you're using an HTTP URL as the $filename to access the clicks and it would appear that this request is not returning the contents of the .txt file but instead arbitrary HTML, which is then being combined with the table causing the layout to break.

Assuming the files are on the same server, so you have

/admin/index.php
/scripts/clicks/clicks_[date].txt

...then in index.php instead of access the files by URL, have a go with:

$filename = "../scripts/clicks/clicks_".date("Y-m-d",$date).".txt";

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by tommo101 on Tue, 2019-04-30 14:46

Perfect, working exactly as it all should now, thanks so very much for the assist... :)

Oh and DOH at me not spotting my own silly rookie mistake there... :O