Jump to content

[SOLVED] Tracking User activity on pages with activity_log.txt


Vineet Sawant
 Share

Recommended Posts

Hello everyone,

Drama booking site that I made, is doing good now, thanks to you all guys for making it possible.

Sometimes users report that they couldn't buy ticket due to some problem, we need to check where they ran into problem.

Also, I need to save a log for every successful or failed transaction.

I'm doing it with a txt file which is generated by following code:

//Logging User Activity

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((int)$usec + (int)$sec);
}

$filename = $config->urls->template."activity/".$user.".txt";

if (file_exists($filename)) {

   $handle = fopen($filename, 'a') or die('Cannot open file:  '.$filename); //implicitly creates file
   $new_data = "\nPage Name: {$page->url} \nTime Stamp: ".date('d/M/Y h:i:s A')."\nUser Agent: ".$_SERVER['HTTP_USER_AGENT']."\nIP Address: ".$_SERVER['REMOTE_ADDR']."\n============================";
   fwrite($handle, $new_data.PHP_EOL);

} else {

    $log_file = "activity/".$user.".txt";
    $handle = fopen($log_file, 'w') or die('Cannot open file:  '.$log_file); //implicitly creates file
    $data = "UserName = {$user->u_fullname}\nPage Name: {$page->url} \nTime Stamp: ".date('d/M/Y h:i:s A')."\nUser Agent: ".$_SERVER['HTTP_USER_AGENT']."\nIP Address: ".$_SERVER['REMOTE_ADDR']."\n============================";
    fwrite($handle, $data);
}

fclose($handle);

//End of Logging Code

The file that is generated shows output as follows:


Page Name: /dramas/white-lily-aani-knight-rider/ 
Time Stamp: 30/Oct/2013 06:41:58 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

Page Name: /404-page-not-found/ 
Time Stamp: 30/Oct/2013 06:41:59 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

Page Name: /404-page-not-found/ 
Time Stamp: 30/Oct/2013 06:43:10 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

Page Name: /dramas/white-lily-aani-knight-rider/ 
Time Stamp: 30/Oct/2013 06:43:21 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

Page Name: /404-page-not-found/ 
Time Stamp: 30/Oct/2013 06:43:22 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

As you can see there are 404 records after every page visit. I don't know why it is happening, while browsing the site there are certainly no 404 errors. Also for each visit, the file shows two records, an actual page record & other 404.

Anyone knows why is it happening?

Link to comment
Share on other sites

Maybe you have some other request (iframe, ajax loading, something?) that is happening behind the scenes.

Maybe add $_SERVER['REQUEST_URI'] to your log?

Thanks apesia for your reply.

I've added the code in head.inc, does that make any difference?

I've  added $_SERVER['REQUEST_URI'], that's a very nice suggestion, very helpful. :)

Link to comment
Share on other sites

Glad it's now working for you. This is not really related to your problem and maybe you have take the route of using a text log file for a reason, but I was thinking that if you have admin users who might need to check to see if transactions were successful or not, it might be more friendly to store this information in a PW page tree with each transaction as a child page with a fields for things like page name, timestamp, user agent, ip address, success of transaction, transaction number, purchase price etc.

You could then offer excel downloads of this data among other things. I did something similar and it works great with one of the PHP excel libraries available and it can be easily integrated into the PW admin without the need to create a model by using diogo's custom admin page module (http://modules.processwire.com/modules/process-admin-custom-pages/)

Maybe I am getting too carried away with ideas for things you don't need :)

Link to comment
Share on other sites

Hello Adrian,

Your suggestion of having page tree for purchase history was my first thought too. But we already have tons of pages such as shows, show histories, user histories etc, do you think it might be a problem when site grows? I'm worried about database size & site performance. I'm not sure how processwire will handle such a large db.

No you are not getting carried away, it's always good to have a strong backend to manage things effectively & easily. :)

About custom admin page, it's something I've always wanted to do and have tried already few times but sadly it just doesn't seem to work for me :(

I keep getting message saying this page has no process associated with it, anyways, that itself can be a different forum topic.

What I'd like to know is how database size & number of pages affect processwire's performance? Do you think I should be deleting old show pages which I'm keeping for maintaining records?

Link to comment
Share on other sites

There should be no reason to worry about deleting old show pages. PW should be able to handle millions of pages (http://processwire.com/talk/topic/3111-a-modx-refugee-questions-on-features-of-processwire/?p=30901), but regarding storing your log information, that shouldn't matter anyway because it would only be available via the admin interface anyway. If you were really worried, you could store the log in a custom SQL table, and create a custom fieldtype so that each entry would be a single database row, but I think that is completely unnecessary in this case. After-all, you are only logging transactions and not all page views - correct?

If you are having issues with the custom admin page module, you should after diogo on the module's support thread - I am sure he can get you going with it.

Link to comment
Share on other sites

Thanks Adrian, that's a relief to know that PW can handle millions of pages.

I'm not just logging transactions, I'm currently logging whole activity, every page visited. So I guess for now I'll keep it in text files. Later once I'm more confident about the system & won't need usage report, I'll just track transactions.

About the module, yes I suppose I should discuss the problem with diogo.

Thanks again, have a great weekend. :)

  • Like 1
Link to comment
Share on other sites

  • 5 years later...
On 10/30/2013 at 7:00 PM, Vineet Sawant said:

Hello everyone,

Drama booking site that I made, is doing good now, thanks to you all guys for making it possible.

Sometimes users report that they couldn't buy ticket due to some problem, we need to check where they ran into problem.

Also, I need to save a log for every successful or failed transaction.

I'm doing it with a txt file which is generated by following code:


//Logging User Activity

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((int)$usec + (int)$sec);
}

$filename = $config->urls->template."activity/".$user.".txt";

if (file_exists($filename)) {

   $handle = fopen($filename, 'a') or die('Cannot open file:  '.$filename); //implicitly creates file
   $new_data = "\nPage Name: {$page->url} \nTime Stamp: ".date('d/M/Y h:i:s A')."\nUser Agent: ".$_SERVER['HTTP_USER_AGENT']."\nIP Address: ".$_SERVER['REMOTE_ADDR']."\n============================";
   fwrite($handle, $new_data.PHP_EOL);

} else {

    $log_file = "activity/".$user.".txt";
    $handle = fopen($log_file, 'w') or die('Cannot open file:  '.$log_file); //implicitly creates file
    $data = "UserName = {$user->u_fullname}\nPage Name: {$page->url} \nTime Stamp: ".date('d/M/Y h:i:s A')."\nUser Agent: ".$_SERVER['HTTP_USER_AGENT']."\nIP Address: ".$_SERVER['REMOTE_ADDR']."\n============================";
    fwrite($handle, $data);
}

fclose($handle);

//End of Logging Code

The file that is generated shows output as follows:



Page Name: /dramas/white-lily-aani-knight-rider/ 
Time Stamp: 30/Oct/2013 06:41:58 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

Page Name: /404-page-not-found/ 
Time Stamp: 30/Oct/2013 06:41:59 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

Page Name: /404-page-not-found/ 
Time Stamp: 30/Oct/2013 06:43:10 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

Page Name: /dramas/white-lily-aani-knight-rider/ 
Time Stamp: 30/Oct/2013 06:43:21 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

Page Name: /404-page-not-found/ 
Time Stamp: 30/Oct/2013 06:43:22 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

As you can see there are 404 records after every page visit. I don't know why it is happening, while browsing the site there are certainly no 404 errors. Also for each visit, the file shows two records, an actual page record & other 404.

Anyone knows why is it happening?

how to check which user make changes in database and what operation has been performed 

all user and changes show as a notification in admin panel  

Link to comment
Share on other sites

On 10/30/2013 at 7:00 PM, Vineet Sawant said:

Hello everyone,

Drama booking site that I made, is doing good now, thanks to you all guys for making it possible.

Sometimes users report that they couldn't buy ticket due to some problem, we need to check where they ran into problem.

Also, I need to save a log for every successful or failed transaction.

I'm doing it with a txt file which is generated by following code:


//Logging User Activity

function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((int)$usec + (int)$sec);
}

$filename = $config->urls->template."activity/".$user.".txt";

if (file_exists($filename)) {

   $handle = fopen($filename, 'a') or die('Cannot open file:  '.$filename); //implicitly creates file
   $new_data = "\nPage Name: {$page->url} \nTime Stamp: ".date('d/M/Y h:i:s A')."\nUser Agent: ".$_SERVER['HTTP_USER_AGENT']."\nIP Address: ".$_SERVER['REMOTE_ADDR']."\n============================";
   fwrite($handle, $new_data.PHP_EOL);

} else {

    $log_file = "activity/".$user.".txt";
    $handle = fopen($log_file, 'w') or die('Cannot open file:  '.$log_file); //implicitly creates file
    $data = "UserName = {$user->u_fullname}\nPage Name: {$page->url} \nTime Stamp: ".date('d/M/Y h:i:s A')."\nUser Agent: ".$_SERVER['HTTP_USER_AGENT']."\nIP Address: ".$_SERVER['REMOTE_ADDR']."\n============================";
    fwrite($handle, $data);
}

fclose($handle);

//End of Logging Code

The file that is generated shows output as follows:



Page Name: /dramas/white-lily-aani-knight-rider/ 
Time Stamp: 30/Oct/2013 06:41:58 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

Page Name: /404-page-not-found/ 
Time Stamp: 30/Oct/2013 06:41:59 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

Page Name: /404-page-not-found/ 
Time Stamp: 30/Oct/2013 06:43:10 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

Page Name: /dramas/white-lily-aani-knight-rider/ 
Time Stamp: 30/Oct/2013 06:43:21 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

Page Name: /404-page-not-found/ 
Time Stamp: 30/Oct/2013 06:43:22 PM
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
IP Address: 127.0.0.1
============================

As you can see there are 404 records after every page visit. I don't know why it is happening, while browsing the site there are certainly no 404 errors. Also for each visit, the file shows two records, an actual page record & other 404.

Anyone knows why is it happening?

how to check which user make changes in database and what operation has been performed 

all user and changes show as a notification in admin panel  

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...