foothing / laravel-simple-pageviews

Tracks page views of your Laravel 5 app for traffic monitoring.
MIT License
28 stars 7 forks source link

Not seems to be working #3

Open joashp opened 6 years ago

joashp commented 6 years ago

I have followed the instructions, but when I visit any page. There are no records generated in visits and visits_buffer tables.

brazorf commented 6 years ago

Hard to tell without clues.

  1. Do you have any error in your logfile?

  2. Which laravel version are you using?

  3. Can you post your app/Http/Kernel.php and your config/visits.php?

joashp commented 6 years ago

No error logs. Laravel v5.4

Kernel.php file protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, 'Foothing\Laravel\Visits\Middlewares\CountPageView', ];

the config/visits.php file `<?php return [

// Enable or disable tracking.
"enabled" => true,

// Add patterns to be blacklisted (ignored and not tracked).
// These patterns will apply if the UrlWhitelist rule is
// enabled in the chain.
"blacklist" => [
    // i.e. '/^(admin|api|auth).*/'
],

// Rules chain.
"rules" => [
    "Foothing\Laravel\Visits\Rules\Crawler",
    "Foothing\Laravel\Visits\Rules\UrlWhitelist",
],

"cache" => true,

];`

brazorf commented 6 years ago

Also, which version of this library are you using?

joashp commented 6 years ago

The latest v1.0.0 stable

joashp commented 6 years ago

Could point me to the files where I add log statements and find out where it is failing?

brazorf commented 6 years ago

I'm looking into this but really i can't get any clue.

Try log something in:

Also make sure your database connection is properly configured and working, i.e. you are able to insert records in any of your tables.

joashp commented 6 years ago

Okay. I added logs in both files.

I was able to get the log from CountPageView handle But, it did not get any logs from Visits track

And yes, my database connection is proper, I have other records already with CRUD operations.

Okay, public function track(Request $request) { Log::info('Visits track before'); if (! $this->trackable($request)) { return false; } Log::info('Visits track'); I get "Visits track before", but not "Visits track"

brazorf commented 6 years ago

That is weird, because the only thing middleware does it's invoking Visits::track(). Can you put a log in the very first line of the track method, and another just after the first if block? Might be that for some reason your request is not trackable()

If it's not, you might add some log in the trackable() method aswell.

joashp commented 6 years ago

It's failing here, I get [2018-02-16 09:15:45] local.INFO: Visits trackable
[2018-02-16 09:15:45] local.INFO: Visits trackable

public function trackable(Request $request) {
        Log::info('Visits trackable');
        if (! Config::get('visits.enabled')) {
Log::info('Visits trackable'. Config::get('visits.enabled'));
            return false;
        }
brazorf commented 6 years ago

Can you please add this log in the first line of trackable method:

\Log::debug( print_r(Config::get('visits'), true));

then give me the resulting log?

joashp commented 6 years ago

I get this,

[2018-02-16 09:27:11] local.DEBUG: 1

brazorf commented 6 years ago

You should get an array dumped instead, which is the whole configuration array.

Try this instead: Log::debug(print_r(Config::get('app'), true));

joashp commented 6 years ago

Yes, I tried this. I get the entire array from config/app.php for

Log::debug(print_r(Config::get('app'), true));

But for visits, i just get

[2018-02-16 09:31:08] local.DEBUG: 1

brazorf commented 6 years ago

Are you sure you published the config file and that it is in your project/config folder?

joashp commented 6 years ago

Yes, I have in the project folder. project_name/config/visits.php

brazorf commented 6 years ago

I believe this is not something wrong with the library itself then, you should investigate why when you issue a Config::get('visits') you are not receiving the configuration array.

Just to be sure, can you do the following

Replace config/visits.php contents as follows

<?php
return ['foo' => 'bar'];

Add a route as follows

Route::get('test', function(){
    dump(\Config::get('visits'));
    dump(\Config::get('database'));
});

Then navigate to http://yourproject/test and see output

joashp commented 6 years ago

Okay. I got the config visits file working. I had to clear the cache. Ran php artisan config:cache This worked.

But now I get, in track function Call to a member function getId() on null

in Visits.php (line 33)

brazorf commented 6 years ago

Seems like your Session is null, which is also weird. Can i see your config/session.php?

joashp commented 6 years ago

Yes, here

`<?php

return [

'driver' => env('SESSION_DRIVER', 'file'),

'lifetime' => 120,

'expire_on_close' => false,

'encrypt' => false,

'files' => storage_path('framework/sessions'),

'connection' => null,

'table' => 'sessions',

'store' => null,

'lottery' => [2, 100],

'cookie' => 'laravel_session',

'path' => '/',

'domain' => env('SESSION_DOMAIN', null),

'secure' => env('SESSION_SECURE_COOKIE', false),

'http_only' => true,

];

`

brazorf commented 6 years ago

Use test route again.

Route::get('test', function(\Illuminate\Http\Request $request){
    dump($request->getSession());
    dump($request);
});

What's the output here?

joashp commented 6 years ago

I only get the dump of $request, the $request->getSession() gives empty. null. screen shot 2018-02-16 at 4 11 16 pm

brazorf commented 6 years ago

That's your issue then, you have an empty session for some reason. I won't ask for your .env as it might contain private info such as passwords etc, but you should check there.

What driver are you using? If it is file, make sure your sessions directory is writable. If it is database, make sure you ran the session migration.

More info here https://laravel.com/docs/5.5/session

I'll be glad to give you more support, however i'm closing this as it doesn't seem related to the library.

joashp commented 6 years ago

Alright, thanks for your help and support.

I am using file-based sessions and sessions are being generated in the storage folder.
anyways, I shall check once again.

brazorf commented 6 years ago

@joashp did you get this sorted?

joashp commented 6 years ago

@brazorf No, I tried with changing the session driver. Still no luck. I have a auth based system, once I login, the sessions are being created in the session folder. But for non auth users sessions are not being generated. Will be giving another shot, trying why it's not working.

joashp commented 6 years ago

@brazorf Alright, I got the sessions working. But when I enabled the plugin from the middleware, it still gave session null. So, I changed and moved the plugin to the middleware group. And it started working.

` protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, 'Foothing\Laravel\Visits\Middlewares\CountPageView', ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

`

Now, I have records in visits_buffer table, but no records in visits table. When do the records get inserted into visits table? I checked logs, no errors other than this.

[2018-02-17 05:20:55] local.DEBUG: Cache set pageviews:aggregate:default [2018-02-17 05:21:18] local.DEBUG: Cache hit pageviews:countOverallVisits:currentYear [2018-02-17 05:21:18] local.DEBUG: Cache set pageviews:countOverallVisits:currentYear [2018-02-17 05:21:18] local.DEBUG: Cache set pageviews:aggregate:currentYear

brazorf commented 6 years ago

I might need to update the docs, as the config section mostly refer to versions of Laravel < 5.3.

Your visits table will populate according to your Schedule settings (see docs), however you can do the swap manually with a php artisan visits:buffer