-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add migration * move scroll_depth_enabled? fn * maybe set engagement_metrics_enabled_at when requesting dashboard * maybe set engagement_metrics_enabled_at in shared_link action * maybe set engagement_metrics_enabled_at on full export * fix tests * feature gate scroll depth on the dashboard with site.engagement_metrics_enabled_at * feature gate scroll depth in full export too * fix npm ci * Rename things into FE, remove unneccessary flag checks * Continue with renaming * Rename site flag to be more descriptive * Move business logic, calculate based on scroll depth, make more precalculatable * Some docs * Rename to site.scrollDepthVisible in frontend * Update migration * Fix template * Remove boilerplate from tests * Update tests * More straight-forward test * Update condition --------- Co-authored-by: Robert Joonas <[email protected]>
- Loading branch information
1 parent
94799b6
commit 7c6ba04
Showing
19 changed files
with
455 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
defmodule Plausible.Stats.ScrollDepth do | ||
@moduledoc """ | ||
Module to check whether the scroll depth metric is available and visible for a site. | ||
""" | ||
|
||
import Ecto.Query | ||
require Logger | ||
|
||
alias Plausible.ClickhouseRepo | ||
|
||
def feature_available?(site, user) do | ||
FunWithFlags.enabled?(:scroll_depth, for: user) || | ||
FunWithFlags.enabled?(:scroll_depth, for: site) | ||
end | ||
|
||
def feature_visible?(site, user) do | ||
feature_available?(site, user) && not is_nil(site.scroll_depth_visible_at) | ||
end | ||
|
||
@doc """ | ||
Checks whether the scroll depth feature is visible for a site and updates the site record if it is. | ||
Note this function queries ClickHouse and may take a while to complete. | ||
""" | ||
def check_feature_visible!(site, user) do | ||
cond do | ||
not feature_available?(site, user) -> | ||
false | ||
|
||
not is_nil(site.scroll_depth_visible_at) -> | ||
true | ||
|
||
is_nil(site.scroll_depth_visible_at) -> | ||
visible? = has_scroll_data_last_30d?(site) | ||
|
||
if visible? do | ||
Plausible.Sites.set_scroll_depth_visible_at(site) | ||
end | ||
|
||
visible? | ||
end | ||
end | ||
|
||
defp has_scroll_data_last_30d?(site) do | ||
try do | ||
ClickhouseRepo.exists?( | ||
from(e in "events_v2", | ||
where: | ||
e.site_id == ^site.id and | ||
e.name == "pageleave" and | ||
e.timestamp >= fragment("toStartOfDay(now() - toIntervalDay(30))") and | ||
e.scroll_depth > 0 and e.scroll_depth <= 100 | ||
) | ||
) | ||
rescue | ||
# Avoid propagating error to the user, bringing down the site. | ||
error -> | ||
Logger.error("Error checking scroll data for site #{site.id}: #{inspect(error)}") | ||
|
||
false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.