Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Pass interval param to CSV export (#2469)

Vini Brasilffbfb14714fc
diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.js
index 1eb0d80fe7..183d65bb4a 100644
--- a/assets/js/dashboard/stats/graph/visitor-graph.js
+++ b/assets/js/dashboard/stats/graph/visitor-graph.js
@@ -233,7 +233,9 @@ class LineGraph extends React.Component {
</div>
)
} else {
- const endpoint = `/${encodeURIComponent(this.props.site.domain)}/export${api.serializeQuery(this.props.query)}`
+ const interval = this.props.graphData?.interval || this.state.interval
+ const queryParams = api.serializeQuery(this.props.query, [{ interval }])
+ const endpoint = `/${encodeURIComponent(this.props.site.domain)}/export${queryParams}`
return (
<a className="w-4 h-4 mx-2" href={endpoint} download onClick={this.downloadSpinner.bind(this)}>
diff --git a/test/plausible_web/controllers/stats_controller_test.exs b/test/plausible_web/controllers/stats_controller_test.exs
index 2d822f21e7..6c8c7e3856 100644
--- a/test/plausible_web/controllers/stats_controller_test.exs
+++ b/test/plausible_web/controllers/stats_controller_test.exs
@@ -102,6 +102,32 @@ defmodule PlausibleWeb.StatsControllerTest do
conn = get(conn, "/" <> site.domain <> "/export?date=2021-10-20")
assert_zip(conn, "30d")
end
+
+ test "exports data grouped by interval", %{conn: conn, site: site} do
+ populate_exported_stats(site)
+ conn = get(conn, "/" <> site.domain <> "/export?date=2021-10-20&period=30d&interval=week")
+
+ assert response = response(conn, 200)
+ {:ok, zip} = :zip.unzip(response, [:memory])
+
+ {_filename, visitors} =
+ Enum.find(zip, fn {filename, _data} -> filename == 'visitors.csv' end)
+
+ parsed_csv =
+ visitors
+ |> String.split("\r\n")
+ |> Enum.map(&String.split(&1, ","))
+
+ assert parsed_csv == [
+ ["date", "visitors", "pageviews", "bounce_rate", "visit_duration"],
+ ["2021-09-20", "1", "1", "100", "0"],
+ ["2021-09-27", "0", "0", "", ""],
+ ["2021-10-04", "0", "0", "", ""],
+ ["2021-10-11", "0", "0", "", ""],
+ ["2021-10-18", "3", "3", "67", "20"],
+ [""]
+ ]
+ end
end
describe "GET /:website/export - via shared link" do