feat: Add Group By option to Website Analytics report

- Also fixed order_by of main query
This commit is contained in:
Suraj Shetty 2023-02-28 11:43:20 +05:30
parent b9f632aa8d
commit ad2d651e45
2 changed files with 18 additions and 4 deletions

View file

@ -28,5 +28,16 @@ frappe.query_reports["Website Analytics"] = {
default: "Daily",
reqd: 1,
},
{
fieldname: "group_by",
label: __("Group By"),
fieldtype: "Select",
options: [
{ value: "path", label: __("Path") },
{ value: "browser", label: __("Browser") },
{ value: "referrer", label: __("Referrer") },
],
default: "path",
},
],
};

View file

@ -28,6 +28,7 @@ class WebsiteAnalytics:
self.filters.to_date = frappe.utils.add_days(self.filters.to_date, 1)
self.query_filters = {"creation": ["between", [self.filters.from_date, self.filters.to_date]]}
self.group_by = self.filters.group_by
def run(self):
columns = self.get_columns()
@ -38,8 +39,10 @@ class WebsiteAnalytics:
return columns, data[:250], None, chart, summary
def get_columns(self):
meta = frappe.get_meta("Web Page View")
group_by = meta.get_field(self.group_by)
return [
{"fieldname": "path", "label": "Page", "fieldtype": "Data", "width": 300},
{"fieldname": group_by.fieldname, "label": group_by.label, "fieldtype": "Data", "width": 300},
{"fieldname": "count", "label": "Page Views", "fieldtype": "Int", "width": 150},
{"fieldname": "unique_count", "label": "Unique Visitors", "fieldtype": "Int", "width": 150},
]
@ -52,12 +55,12 @@ class WebsiteAnalytics:
return (
frappe.qb.from_(WebPageView)
.select("path", count_all, count_is_unique)
.select(self.group_by, count_all, count_is_unique)
.where(
Coalesce(WebPageView.creation, "0001-01-01")[self.filters.from_date : self.filters.to_date]
)
.groupby(WebPageView.path)
.orderby("count", Order=frappe.qb.desc)
.groupby(self.group_by)
.orderby("count", order=frappe.qb.desc)
).run()
def _get_query_for_mariadb(self):