tighten / nova-google-analytics

Google Analytics integration with Laravel Nova
MIT License
165 stars 30 forks source link

Feature: stats for single resource #12

Closed ahmic closed 3 years ago

ahmic commented 5 years ago

Resource tool for showing pageviews for a single resource on detail screen


Single Resource analytics

Preview

Pageviews data for single page (Nova resource) shown on detail screen for particular resource

Usage

To enable stats for a specific model, you'll need to add the tool to that model's Nova resource. Make sure to specify the class name so the resource tool can load it properly.

Replace _PATH_ with appropriate path for your model

use Tightenco\NovaGoogleAnalytics\StatsPerPage;

// ...

StatsPerPage::make()
    ->path(_PATH_),

Example If your url scheme looks like https://domain.com/posts/123/post-slug path will be /posts/123/post-slug

ahmic commented 5 years ago

Hi @mattstauffer, would appreciate if you can take a look at PR. I'm available to make improvements if necessary..

mattstauffer commented 5 years ago

@ahmic Taking a look now! :) Been a busy summer here. :) Thanks for your PR!

mattstauffer commented 5 years ago

@ahmic I took a quick peek and it's not working on a Nova 1. app. Is this panel.fields something that's only going to work on Nova 2? (I haven't done much addon dev in 2. so I'm not familiar.)

Either way, let's also get some documentation into the readme for how to use it.

Thanks!

ahmic commented 5 years ago

@mattstauffer You are right. As per documentation in Nova 1.* tool's metadata is accessible by const issuesRefunds = this.field.issuesRefunds;

In Nova 2.* const issuesRefunds = this.panel.fields[0].issuesRefunds;

If you have a plan to release a new major version this would work. For version 1.* I can make a change

mattstauffer commented 5 years ago

@ahmic I would love it if it works for both versions. I'll see if I can do a quick modification that makes it work on my 1.* app.

mattstauffer commented 5 years ago

OK, I have it running in 1.*. A problems right now:

This error is formatted wrong. Is this how it looks in 2., or do we need to format the error differently in 1. vs 2.*?

image

mattstauffer commented 5 years ago

I think I'm also having trouble with the data because the path that I'm testing is getting zero results back, but it shows up as my top result in the "GA most-visited pages this week." I'm suspicious that this is because my account has multiple domains that it tracks, but I'm not entirely sure.

You can see this result here:

image

The URL for that one is /index.php?page=yahtzee, but when I pass that path into this tool:

class User
{
    public function fields()
    {
        return [
            \Tightenco\NovaGoogleAnalytics\StatsPerPage::make()->path('/index.php?action=yahtzee'),
        ];
    }
}

I get no data:

image

Also, changing the timeline doesn't change anything. Because, again, I haven't worked with these things in quite a while, I'm curious if it's because I'm doing it wrong (let's get some documentation into this PR!) or whether there's a bug or whether it's because it's Nova 1.*.

ahmic commented 5 years ago

I have been working on this today, using Laravel 5.7 with Nova 1.2.0 and Laravel 5.8 with Nova 2.0.11, and here is the update:

Laravel 5.8 with Nova 2.0.1

Everything the same Nova 2

Laravel 5.7 with Nova 1.2.0

Nova 2

It's working now but there is a minor layout issue. In Nova 1 resource tool title is shown by default, and complete tool component is wrapped inside a card by default. I can the hide title but I think is not a big deal?

To continue working on this @mattstauffer please test again in the same environment like before. If you get zero results again maybe it's related to Google Analytics account setup..

mattstauffer commented 5 years ago

Almost there! It's now rendering correctly, but the dropdown ranges aren't working correctly.

  1. When I select a new range, it doesn't stay selected in the dropdown (but it does change the content of the graph.)
  2. If I select the same range twice in a row, it breaks the thing.

It's possible these are bugs in Nova but I think it's more likely something that changed between Nova 1 and 2?

image

As you can see, the selected range key is getting passed in correctly; as is the value for the range entry. Not sure what's going on here, honestly

ahmic commented 5 years ago

@mattstauffer Really strange. I'm currently testing with Nova v1.2.0 and it's working, no errors. Which version do you have?

mattstauffer commented 5 years ago

v1.3.2. I'll see if I can find one on 1.2.0 later.

Can you show me the actual, specific implementation you have of where you pulled this in and on what sort of resource?

ahmic commented 5 years ago

Just pulled Nova v1.3.2 and still working, without any errors. Regarding my setup for Nova v1.* it's a demo app I created to test this since I first made it on Nova v2. I'm using it on the default user resource, path is hardcoded form one of my production apps. Nothing special there..

mattstauffer commented 5 years ago

Can you share the full User resource then? I feel like there might be a different implementation detail here OR it might be something that I have cached since this is a very old Nova install that I've slowly upgraded. I'll try to do it on a fresh Nova install at 1.3.2 a bit later.

ahmic commented 5 years ago
// app/Nova/User.php

public function fields(Request $request)
{
        return [
            \Tightenco\NovaGoogleAnalytics\StatsPerPage::make()
                ->path('/vijesti/1373/lejla-omerovic-kada-jednom-krocite-u-it-svijet-samo-nebo-je-granica'),

            ID::make()->sortable(),

            // ...
       ];

}
mattstauffer commented 5 years ago

Gahh.. I cannot figure out what it is. I just wiped the vendor dir, composer updated, re ran npm dev on both the plugin and my site... cannot reproduce yours working.