Jump to content
Vineet Sawant

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

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?

Share this post


Link to post
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?

  • Like 1

Share this post


Link to post
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. :)

Share this post


Link to post
Share on other sites

Update:

Somehow it looks like the problem is only resolved, I didn't get any duplicate entries on server.

Something's wrong with my local server I suppose.

Share this post


Link to post
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 :)

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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  

Share this post


Link to post
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  

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By David Karich
      The Page Hit Counter module for ProcessWire implements a simple page view counter in backend. Page views of visitors are automatically tracked on defined templates, with monitoring of multiple page views. This gives you a quick overview of how many visitors have read a news or a blog post, for example, without first having to open complex tools such as Google Analytics. This module quickly provides simple information, e.g. for editors. Or, for example, to sort certain news by most page views. For example for "Trending Topics".

       
      Works with ProCache and AdBlockers. With a lightweight tracking code of only ~320 bytes (gzipped). And no code changes necessary! In addition GDPR compliant, since no personal data or IP addresses are stored. Only session cookies are stored without information. 
      In addition, there are some options, for example filtering IP addresses (for CronJobs) and filtering bots, spiders and crawlers. You can also configure the lifetime of the session cookies. Repeated page views are not counted during this period. It is also possible to exclude certain roles from tracking. For example, logged in editors who work on a page are not counted as page views.

      Sort by hits and access page views (hit value)
      Each trackable template has an additional field called phits. For example, you want to output all news sorted by the number of page views.
      // It is assumed that the template, e.g. with the name "news", has been configured for tracking. $news = $pages->find("template=news, sort=-phits"); To output the page views of a tracked page, use:
      echo $page->phits; Example: Reset counter per API
      $modules->get("PageHitCounter")->resetPageViews("template=whatever", false); Example: Tracking a page hit via API and jQuery
      If you want to track a template that does not represent a full page to automatically inject a tracking script, you can define allowed API templates in the module that you can track. Below is an example of how you can track a click on news tag using jQuery. This will allow you to find out which keywords are clicked the most. For example, you can sort and display a tag cloud by the number of hits. Suppose your keywords have the template "news_tag". The template "news_tag" was also configured in the Page Hit Counter Module as a trackable API template.
      Example PHP output of keywords / tags:
      // Required: the data attribute "data-pid" with the ID of the template to be tracked. echo $pages->find("template=news_tag, sort=-phits")->each("<a href='{url}' class='news_tag' data-pid='{id}'>{title}</a>"); Example Tracking Script with jQuery:
      /** * Required: Data attribute "data-pid" with the ID of the news tag template * Required: Send the POST request to the URL "location.pathname.replace(/\/?$/, '/') + 'phcv1'" * Required: The POST parameter "pid" with the ID of the template */ $(function(){ if($('a.news_tag').length > 0) { $('a.news_tag').each(function(){ var tPID = $(this).data("pid"); if(tPID) { $(this).on("click", function(){ $.post(location.pathname.replace(/\/?$/, '/') + 'phcv1', {pid: tPID}); }); } }); } }); So simply every click on a tag is counted. Including all checks as for automatic tracking. Like Bot Filtering, Session Lifetime, etc.
      Notice: Tracking with URL segments
      If the option "Allow URL Segments" is activated on a template, the hits are only counted if the base URL of the page is called. If you want the hit to be counted even when a segment is requested, you MUST configure the segments in the template configuration. How to do this can be found here. If you use dynamic segments, configure them as RegEx. There is currently no other option. The problem is that the Page Hit Counter hooked into the PageNotFound process. If URL segments are allowed but not defined, a 404 is never triggered. This means that the Page Hit Counter cannot be called.
      _______________________________________________________
      Background: This module is the result of a customer requirement, where the editors are overwhelmed with analytics or no tracking tools were allowed to be used. However, a way had to be found to at least count page views in a simple form for evaluations. Furthermore, by using ProCache, a way had to be found to count views of a page without clearing the cache.
      _______________________________________________________
      Pros
      Automatic Page View Tracking Lightweight tracking code, only ~320 bytes (gzipped) No code or frontend changes necessary Works with ProCache! Even if no PHP is executed on the cached page, the tracking works Works with browser AdBlockers No cache triggers (for example, ProCache) are triggered. The cache remains persistent GDPR compliant, session-based cookie only, no personal information Filtering of IPs and bots possible Exclude certain roles from tracking Ability to reset Page Views Works with all admin themes Counter database is created as write-optimized InnoDB API to track events for templates that are not viewable No dependencies on libraries, pure VanillaJS (Automatic tracking script) Works in all modern browsers Pages are sortable by hits Cons
      Only for ProcessWire version 3.0.80 or higher (Requires wireCount()) Only for PHP version 5.6.x or higher No support for Internet Explorer <= version 9 (Because of XMLHttpRequest()) No historical data, just simple summation (Because of GDPR) Segment URLs can only be counted if the segments are defined Planned Features / ToDos
      API access to hit values Since version 1.2.1 Possibility to sort the pages by hits (Request by @Zeka) Since version 1.2.0 Don't track logged in users with certain roles (Request by @wbmnfktr) Since version 1.1.0 Possibility to reset the counter for certain pages or templates (Request by @wbmnfktr) Since version 1.1.0 Better bot filter Since version 1.1.0 Disable session lifetime, don't store cookies to track every page view (Request by @matjazp) Since version 1.2.1 Option to hide the counter in the page tree (Request by @matjazp) Since version 1.2.1 Option to hide the counter in the page tree on certain templates Since version 1.2.1 API to track events for templates that are not viewable Since version 1.2.2 Changelog
      1.2.6
      Bug-Fix: Set the counter of a cloned page to 0 Enhancement: The function for resetting counters is now available in the module as a public function to reset counters via own scripts on the API side (Request by @VeiJari) Enhancement: Documentation improvement API reset 1.2.5
      Bug-Fix: When counting 404 hits, cookies are no longer set. The session lifetime is deactivated for the 404 page Enhancement: Documentation improvement regarding URL segments 1.2.4
      Bug-Fix: Resetting the counters on system pages (e.g. 404) does not work (Reported by wbmnfktr) Bug-Fix: Tracking endpoint is logged as 404 if module "Jumplinks" is installed (Reported by wbmnfktr) Enhancement: Corrected few typos (Merged from Sergio #6 – THX!) 1.2.3
      Bug-Fix: Tracking script triggers 404 if pages are configured without slash (#3) Reported by @maxf5 Enhancement: Reduction of the tracking script size if it's gzipped (~320 bytes) Enhancement: Documentation improvement Enhancement: Corrected few typos 1.2.2
      New feature: API to track events for templates that are not viewable Enhancement: Documentation improvement 1.2.1
      API access to hit values Use $page->phits Bug-Fix: No tracking on welcomepage (Reported by wbmnfktr; Thx to matjazp) Bug-Fix: Tracking script path on subfolders (Reported by matjazp) Bug-Fix: Tracking on pages with status "hidden" Enhancement: Change database engine to InnoDB for phits field Enhancement: Option to disable session lifetime set session lifetime to 0, no cookies Enhancement: Better installation check Enhancement: AJAX Request asyncron Enhancement: Reduction of the tracking script size by ~20% Enhancement: Option to hide the counter in the page tree You can output the counter with the field name "phits" Enhancement: Option to hide the counter in the page tree on certain templates Enhancement: Option for activate general IP validation Enhancement: Reduction of tracking overhead up to ~30ms Enhancement: Better bot list for detection 1.2.0
      New feature: Sort pages by hits – New field phits Migrate old counter data to new field 1.1.0
      New feature: Exclude tracking of certain roles New feature: Reset Page Views Better bot filter and detection 1.0.0
      Initial release Notes
      By default, the page views are stored as INT in the database. This allows a maximum counter value of 4.2 billion views (4,294,967,295) per page. If you need more, change the type to BIGINT directly in the database. But I recommend to use Google Analytics or similar tools if you have such a large number of users.
      _______________________________________________________
      Download GitHub: ProcessWire Page Hit Counter (Version 1.2.6)
      PW Module Directory: ProcessWire Page Hit Counter (Version 1.2.6)
      Install via ProcessWire (Classname): PageHitCounter
      _______________________________________________________
      Update information
      If you have used version 1.2.1 from the DEV branch, please replace it completely with the new master version.
    • By quickjeff
      Hi Guys, I have seen some other threads that talk about this but nothing recent nor in line with what I need. 
       
      I have basically created a page in which I am tracking the number of views. I update the counter which is tied to a field I added to the page's template that I am tracking. The field is hidden from the website admin and only used for this data. 
       
      However, now I need to track it on a daily basis. I am thinking of keeping track of all time view total for 30 days and perhaps saving that and allowing the user to see the page views of last 7 days, each day as a number and then 30 days of data. This way if I want to build in reporting functionality I can. 
       
      What I need help with adding a date to the page_views field but also, knowing how many page_views for today, yesterday, day before etc. 
      Here is what I have so far and its working. Now I need to associate a date with every day. NOT every update of the page_views field. 
      if (!$user->hasRole('superuser')) { $page->page_views += 1; $page->of(false); $page->save('page_views'); $page->of(true); } echo $page->page_views; Any guidance, help, suggestions is absolutely appreciated. 
      Thanks!!!!
    • By Sabrina
      Hi,  
      I would like to start tracking my website visitors with Leady software. Searched the forum for similar discussion yet did not find the right answer. Could you please tell me how can I add their javascript to my website? Is there any step by step guide available?
      Any help appreciated. 
    • By reed
      Hey folks, I did some digging to see if I could find anything on this, but I couldn't turn up anything.
      Does any one have a recommendation on how to track the terms that users enter in my site search?
      Thanks for any thoughts!
    • By Nick Belane
      I am new to processwire and I just want to track the amount of downloads of different pdf-files. I only want to see in the backend how many times user downloaded different pdf-files from my website.
      Thats all.
      I do NOT need something like this:
      https://processwire.com/talk/topic/4602-flexible-downloads-using-pages/
      Thanx in advance
      Nick
×
×
  • Create New...