Compare commits
1 Commits
master
...
feat-tag-c
| Author | SHA1 | Date |
|---|---|---|
|
|
da0638d198 | 3 years ago |
@ -1,73 +0,0 @@
|
||||
# This file excludes paths from the Docker build context.
|
||||
#
|
||||
# By default, Docker's build context includes all files (and folders) in the
|
||||
# current directory. Even if a file isn't copied into the container it is still sent to
|
||||
# the Docker daemon.
|
||||
#
|
||||
# There are multiple reasons to exclude files from the build context:
|
||||
#
|
||||
# 1. Prevent nested folders from being copied into the container (ex: exclude
|
||||
# /assets/node_modules when copying /assets)
|
||||
# 2. Reduce the size of the build context and improve build time (ex. /build, /deps, /doc)
|
||||
# 3. Avoid sending files containing sensitive information
|
||||
#
|
||||
# More information on using .dockerignore is available here:
|
||||
# https://docs.docker.com/engine/reference/builder/#dockerignore-file
|
||||
|
||||
.dockerignore
|
||||
|
||||
# Ignore git, but keep git HEAD and refs to access current commit hash if needed:
|
||||
#
|
||||
# $ cat .git/HEAD | awk '{print ".git/"$2}' | xargs cat
|
||||
# d0b8727759e1e0e7aa3d41707d12376e373d5ecc
|
||||
.git
|
||||
!.git/HEAD
|
||||
!.git/refs
|
||||
|
||||
# Common development/test artifacts
|
||||
/cover/
|
||||
/doc/
|
||||
/test/
|
||||
/tmp/
|
||||
.elixir_ls
|
||||
plausible-report.xml
|
||||
.env
|
||||
.idea
|
||||
*.iml
|
||||
*.log
|
||||
*.code-workspace
|
||||
.vscode
|
||||
|
||||
# Mix artifacts
|
||||
/_build/
|
||||
/deps/
|
||||
*.ez
|
||||
|
||||
# Generated on crash by the VM
|
||||
erl_crash.dump
|
||||
|
||||
# If NPM crashes, it generates a log, let's ignore it too.
|
||||
npm-debug.log
|
||||
|
||||
# Static artifacts - These should be fetched and built inside the Docker image
|
||||
/assets/node_modules/
|
||||
/tracker/node_modules/
|
||||
/priv/static/cache_manifest.json
|
||||
/priv/static/css
|
||||
/priv/static/js
|
||||
/priv/version.json
|
||||
|
||||
# Auto-generated tracker files
|
||||
/priv/tracker/js/*.js
|
||||
|
||||
# Dializer
|
||||
/priv/plts/*.plt
|
||||
/priv/plts/*.plt.hash
|
||||
|
||||
# Geolocation databases
|
||||
/priv/geodb/*.mmdb
|
||||
/priv/geodb/*.mmdb.gz
|
||||
|
||||
# Docker volumes
|
||||
.clickhouse_db_vol*
|
||||
plausible_db*
|
||||
@ -1,67 +0,0 @@
|
||||
name: Build Private Images GHCR
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master, stable]
|
||||
tags: ['r*']
|
||||
pull_request:
|
||||
types: [synchronize, labeled]
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
build:
|
||||
if: ${{ github.event_name == 'push' || contains(github.event.pull_request.labels.*.name, 'deploy-to-staging') }}
|
||||
runs-on: buildjet-16vcpu-ubuntu-2204
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: Docker meta
|
||||
id: meta
|
||||
uses: docker/metadata-action@v5.0.0
|
||||
with:
|
||||
images: ghcr.io/plausible/analytics/ee
|
||||
tags: |
|
||||
type=ref,event=pr
|
||||
type=ref,event=branch
|
||||
type=ref,event=tag
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Login to GitHub Container Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Build and push
|
||||
id: docker_build
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
push: true
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
build-args: |
|
||||
MIX_ENV=prod
|
||||
BUILD_METADATA=${{ steps.meta.outputs.json }}
|
||||
|
||||
- name: Image digest
|
||||
run: echo ${{ steps.docker_build.outputs.digest }}
|
||||
|
||||
- name: Notify team on failure
|
||||
if: ${{ failure() }}
|
||||
uses: fjogeleit/http-request-action@v1
|
||||
with:
|
||||
url: ${{ secrets.BUILD_NOTIFICATION_URL }}
|
||||
method: 'POST'
|
||||
customHeaders: '{"Content-Type": "application/json"}'
|
||||
data: '{"content": "<a href=\"https://github.com/plausible/analytics/actions/workflows/build-private-images.yml\">Build failed</a>"}'
|
||||
@ -1,68 +0,0 @@
|
||||
name: Build Public Images GHCR
|
||||
|
||||
on:
|
||||
push:
|
||||
tags: ['v*']
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: buildjet-16vcpu-ubuntu-2204
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: Docker meta
|
||||
id: meta
|
||||
uses: docker/metadata-action@v5
|
||||
with:
|
||||
images: ghcr.io/plausible/community-edition
|
||||
tags: |
|
||||
type=semver,pattern={{version}},prefix=v
|
||||
type=semver,pattern={{major}}.{{minor}},prefix=v
|
||||
type=semver,pattern={{major}},prefix=v
|
||||
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@v3
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Login to GitHub Container Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Build and push
|
||||
id: docker_build
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
push: true
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
platforms: linux/amd64, linux/arm64
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
build-args: |
|
||||
MIX_ENV=small
|
||||
BUILD_METADATA=${{ steps.meta.outputs.json }}
|
||||
ERL_FLAGS=+JPperf true
|
||||
|
||||
- name: Image digest
|
||||
run: echo ${{ steps.docker_build.outputs.digest }}
|
||||
|
||||
- name: Notify team on failure
|
||||
if: ${{ failure() }}
|
||||
uses: fjogeleit/http-request-action@v1
|
||||
with:
|
||||
url: ${{ secrets.BUILD_NOTIFICATION_URL }}
|
||||
method: 'POST'
|
||||
customHeaders: '{"Content-Type": "application/json"}'
|
||||
data: '{"content": "<a href=\"https://github.com/plausible/analytics/actions/workflows/build-public-images.yml\">Build failed</a>"}'
|
||||
@ -1,28 +0,0 @@
|
||||
name: Validate migrations
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
paths:
|
||||
- 'priv/repo/migrations/**'
|
||||
- 'priv/ingest_repo/migrations/**'
|
||||
|
||||
jobs:
|
||||
validate:
|
||||
name: App code does not change at the same time
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: dorny/paths-filter@v3
|
||||
id: changes
|
||||
with:
|
||||
list-files: json
|
||||
filters: |
|
||||
lib:
|
||||
- 'lib/**'
|
||||
- 'extra/**'
|
||||
- 'config/**'
|
||||
|
||||
- if: steps.changes.outputs.lib == 'true'
|
||||
run: |
|
||||
echo "::error file=${{ fromJSON(steps.changes.outputs.lib_files)[0] }}::Code and migrations shouldn't be changed at the same time"
|
||||
exit 1
|
||||
@ -1,5 +0,0 @@
|
||||
Plausible copyright notice
|
||||
|
||||
This software is Copyright 2024 Plausible Insights OÜ
|
||||
|
||||
This repository is provided for informational purposes only, and no rights to use, distribute or otherwise exploit this software are granted to you. Our other repositories include software that is offered under the Affero GPL, but this repository is not. If you wish to discuss a license, or our hosted service that uses the software, please contact us at hello@plausible.io
|
||||
@ -1,247 +0,0 @@
|
||||
import React from 'react';
|
||||
import { withRouter } from 'react-router-dom'
|
||||
import Chart from 'chart.js/auto';
|
||||
import { navigateToQuery } from '../../query'
|
||||
import GraphTooltip from './graph-tooltip'
|
||||
import { buildDataSet, METRIC_LABELS, METRIC_FORMATTER } from './graph-util'
|
||||
import dateFormatter from './date-formatter';
|
||||
import FadeIn from '../../fade-in';
|
||||
import classNames from 'classnames';
|
||||
|
||||
const calculateMaximumY = function(dataset) {
|
||||
const yAxisValues = dataset
|
||||
.flatMap((item) => item.data)
|
||||
.map((item) => item || 0)
|
||||
|
||||
if (yAxisValues) {
|
||||
return Math.max(...yAxisValues)
|
||||
} else {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
class LineGraph extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.regenerateChart = this.regenerateChart.bind(this);
|
||||
this.updateWindowDimensions = this.updateWindowDimensions.bind(this);
|
||||
}
|
||||
|
||||
getGraphMetric() {
|
||||
let metric = this.props.graphData.metric
|
||||
|
||||
if (metric == 'visitors' && this.props.query.filters.goal) {
|
||||
return 'conversions'
|
||||
} else {
|
||||
return metric
|
||||
}
|
||||
}
|
||||
|
||||
regenerateChart() {
|
||||
const { graphData, query } = this.props
|
||||
const metric = this.getGraphMetric()
|
||||
const graphEl = document.getElementById("main-graph-canvas")
|
||||
this.ctx = graphEl.getContext('2d');
|
||||
const dataSet = buildDataSet(graphData.plot, graphData.comparison_plot, graphData.present_index, this.ctx, METRIC_LABELS[metric])
|
||||
|
||||
return new Chart(this.ctx, {
|
||||
type: 'line',
|
||||
data: {
|
||||
labels: graphData.labels,
|
||||
datasets: dataSet
|
||||
},
|
||||
options: {
|
||||
animation: false,
|
||||
plugins: {
|
||||
legend: { display: false },
|
||||
tooltip: {
|
||||
enabled: false,
|
||||
mode: 'index',
|
||||
intersect: false,
|
||||
position: 'average',
|
||||
external: GraphTooltip(graphData, metric, query)
|
||||
},
|
||||
},
|
||||
responsive: true,
|
||||
maintainAspectRatio: false,
|
||||
onResize: this.updateWindowDimensions,
|
||||
elements: { line: { tension: 0 }, point: { radius: 0 } },
|
||||
onClick: this.maybeHopToHoveredPeriod.bind(this),
|
||||
scale: {
|
||||
ticks: { precision: 0, maxTicksLimit: 8 }
|
||||
},
|
||||
scales: {
|
||||
y: {
|
||||
min: 0,
|
||||
suggestedMax: calculateMaximumY(dataSet),
|
||||
ticks: {
|
||||
callback: METRIC_FORMATTER[metric],
|
||||
color: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined
|
||||
},
|
||||
grid: {
|
||||
zeroLineColor: 'transparent',
|
||||
drawBorder: false,
|
||||
}
|
||||
},
|
||||
yComparison: {
|
||||
min: 0,
|
||||
suggestedMax: calculateMaximumY(dataSet),
|
||||
display: false,
|
||||
grid: { display: false },
|
||||
},
|
||||
x: {
|
||||
grid: { display: false },
|
||||
ticks: {
|
||||
callback: function(val, _index, _ticks) {
|
||||
if (this.getLabelForValue(val) == "__blank__") return ""
|
||||
|
||||
const hasMultipleYears =
|
||||
graphData.labels
|
||||
.filter((date) => typeof date === 'string')
|
||||
.map(date => date.split('-')[0])
|
||||
.filter((value, index, list) => list.indexOf(value) === index)
|
||||
.length > 1
|
||||
|
||||
if (graphData.interval === 'hour' && query.period !== 'day') {
|
||||
const date = dateFormatter({
|
||||
interval: "date",
|
||||
longForm: false,
|
||||
period: query.period,
|
||||
shouldShowYear: hasMultipleYears,
|
||||
})(this.getLabelForValue(val))
|
||||
|
||||
const hour = dateFormatter({
|
||||
interval: graphData.interval,
|
||||
longForm: false,
|
||||
period: query.period,
|
||||
shouldShowYear: hasMultipleYears,
|
||||
})(this.getLabelForValue(val))
|
||||
|
||||
// Returns a combination of date and hour. This is because
|
||||
// small intervals like hour may return multiple days
|
||||
// depending on the query period.
|
||||
return `${date}, ${hour}`
|
||||
}
|
||||
|
||||
if (graphData.interval === 'minute' && query.period !== 'realtime') {
|
||||
return dateFormatter({
|
||||
interval: "hour", longForm: false, period: query.period,
|
||||
})(this.getLabelForValue(val))
|
||||
}
|
||||
|
||||
return dateFormatter({
|
||||
interval: graphData.interval, longForm: false, period: query.period, shouldShowYear: hasMultipleYears,
|
||||
})(this.getLabelForValue(val))
|
||||
},
|
||||
color: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined
|
||||
}
|
||||
}
|
||||
},
|
||||
interaction: {
|
||||
mode: 'index',
|
||||
intersect: false,
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
repositionTooltip(e) {
|
||||
const tooltipEl = document.getElementById('chartjs-tooltip');
|
||||
if (tooltipEl && window.innerWidth >= 768) {
|
||||
if (e.clientX > 0.66 * window.innerWidth) {
|
||||
tooltipEl.style.right = (window.innerWidth - e.clientX) + window.pageXOffset + 'px'
|
||||
tooltipEl.style.left = null;
|
||||
} else {
|
||||
tooltipEl.style.right = null;
|
||||
tooltipEl.style.left = e.clientX + window.pageXOffset + 'px'
|
||||
}
|
||||
tooltipEl.style.top = e.clientY + window.pageYOffset + 'px'
|
||||
tooltipEl.style.opacity = 1;
|
||||
}
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
if (this.props.graphData) {
|
||||
this.chart = this.regenerateChart();
|
||||
}
|
||||
window.addEventListener('mousemove', this.repositionTooltip);
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps) {
|
||||
const { graphData, darkTheme } = this.props;
|
||||
const tooltip = document.getElementById('chartjs-tooltip');
|
||||
|
||||
if (
|
||||
graphData !== prevProps.graphData ||
|
||||
darkTheme !== prevProps.darkTheme
|
||||
) {
|
||||
|
||||
if (graphData) {
|
||||
if (this.chart) {
|
||||
this.chart.destroy();
|
||||
}
|
||||
this.chart = this.regenerateChart();
|
||||
this.chart.update();
|
||||
}
|
||||
|
||||
if (tooltip) {
|
||||
tooltip.style.display = 'none';
|
||||
}
|
||||
}
|
||||
|
||||
if (!graphData) {
|
||||
if (this.chart) {
|
||||
this.chart.destroy();
|
||||
}
|
||||
|
||||
if (tooltip) {
|
||||
tooltip.style.display = 'none';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
// Ensure that the tooltip doesn't hang around when we are loading more data
|
||||
const tooltip = document.getElementById('chartjs-tooltip');
|
||||
if (tooltip) {
|
||||
tooltip.style.opacity = 0;
|
||||
tooltip.style.display = 'none';
|
||||
}
|
||||
window.removeEventListener('mousemove', this.repositionTooltip)
|
||||
}
|
||||
|
||||
/**
|
||||
* The current ticks' limits are set to treat iPad (regular/Mini/Pro) as a regular screen.
|
||||
* @param {*} chart - The chart instance.
|
||||
* @param {*} dimensions - An object containing the new dimensions *of the chart.*
|
||||
*/
|
||||
updateWindowDimensions(chart, dimensions) {
|
||||
chart.options.scales.x.ticks.maxTicksLimit = dimensions.width < 720 ? 5 : 8
|
||||
}
|
||||
|
||||
maybeHopToHoveredPeriod(e) {
|
||||
const element = this.chart.getElementsAtEventForMode(e, 'index', { intersect: false })[0]
|
||||
const date = this.props.graphData.labels[element.index] || this.props.graphData.comparison_labels[element.index]
|
||||
|
||||
if (this.props.graphData.interval === 'month') {
|
||||
navigateToQuery(this.props.history, this.props.query, { period: 'month', date })
|
||||
} else if (this.props.graphData.interval === 'date') {
|
||||
navigateToQuery(this.props.history, this.props.query, { period: 'day', date })
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const { graphData } = this.props
|
||||
const canvasClass = classNames('mt-4 select-none', { 'cursor-pointer': !['minute', 'hour'].includes(graphData?.interval) })
|
||||
|
||||
return (
|
||||
<FadeIn show={graphData}>
|
||||
<div className="relative h-96 print:h-auto print:pb-8 w-full z-0">
|
||||
<canvas id="main-graph-canvas" className={canvasClass}></canvas>
|
||||
</div>
|
||||
</FadeIn>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default withRouter(LineGraph)
|
||||
@ -1,17 +0,0 @@
|
||||
import React from "react"
|
||||
|
||||
export default function SamplingNotice({topStatData}) {
|
||||
const samplePercent = topStatData?.samplePercent
|
||||
|
||||
if (samplePercent && samplePercent < 100) {
|
||||
return (
|
||||
<div tooltip={`Stats based on a ${samplePercent}% sample of all visitors`} className="cursor-pointer w-4 h-4 mx-2">
|
||||
<svg className="absolute w-4 h-4 dark:text-gray-300 text-gray-700" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
|
||||
</svg>
|
||||
</div>
|
||||
)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@ -1,53 +0,0 @@
|
||||
import React, { useState } from "react"
|
||||
import * as api from '../../api'
|
||||
import { getCurrentInterval } from "./interval-picker"
|
||||
|
||||
export default function StatsExport({site, query}) {
|
||||
const [exporting, setExporting] = useState(false)
|
||||
|
||||
function startExport() {
|
||||
setExporting(true)
|
||||
document.cookie = "exporting="
|
||||
pollExportReady()
|
||||
}
|
||||
|
||||
function pollExportReady() {
|
||||
if (document.cookie.includes('exporting')) {
|
||||
setTimeout(pollExportReady, 1000)
|
||||
} else {
|
||||
setExporting(false)
|
||||
}
|
||||
}
|
||||
|
||||
function renderLoading() {
|
||||
return (
|
||||
<svg className="animate-spin h-4 w-4 text-indigo-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
|
||||
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
|
||||
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
|
||||
function renderExportLink() {
|
||||
const interval = getCurrentInterval(site, query)
|
||||
const queryParams = api.serializeQuery(query, [{ interval }])
|
||||
const endpoint = `/${encodeURIComponent(site.domain)}/export${queryParams}`
|
||||
|
||||
return (
|
||||
<a href={endpoint} download onClick={startExport}>
|
||||
<svg className="absolute text-gray-700 feather dark:text-gray-300 hover:text-indigo-600 dark:hover:text-indigo-600" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
|
||||
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
|
||||
<polyline points="7 10 12 15 17 10"></polyline>
|
||||
<line x1="12" y1="15" x2="12" y2="3"></line>
|
||||
</svg>
|
||||
</a>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-4 h-4 mx-2">
|
||||
{exporting && renderLoading()}
|
||||
{!exporting && renderExportLink()}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@ -1,40 +0,0 @@
|
||||
import React from "react"
|
||||
import { parseNaiveDate, isBefore } from '../../util/date'
|
||||
import { Link } from 'react-router-dom'
|
||||
import * as url from '../../util/url'
|
||||
import { BarsArrowUpIcon } from '@heroicons/react/20/solid'
|
||||
|
||||
export default function WithImportedSwitch({site, topStatData}) {
|
||||
if (!topStatData?.imports_exist) {
|
||||
return null
|
||||
}
|
||||
|
||||
function isBeforeNativeStats(date) {
|
||||
if (!date) return false
|
||||
|
||||
const nativeStatsBegin = parseNaiveDate(site.nativeStatsBegin)
|
||||
const parsedDate = parseNaiveDate(date)
|
||||
|
||||
return isBefore(parsedDate, nativeStatsBegin, "day")
|
||||
}
|
||||
|
||||
const isQueryingImportedPeriod = isBeforeNativeStats(topStatData.from)
|
||||
const isComparingImportedPeriod = isBeforeNativeStats(topStatData.comparing_from)
|
||||
|
||||
if (isQueryingImportedPeriod || isComparingImportedPeriod) {
|
||||
const withImported = topStatData.with_imported;
|
||||
const toggleColor = withImported ? " dark:text-gray-300 text-gray-700" : " dark:text-gray-500 text-gray-400"
|
||||
const target = url.setQuery('with_imported', !withImported)
|
||||
const tip = withImported ? "" : "do not ";
|
||||
|
||||
return (
|
||||
<Link to={target} className="w-4 h-4 mx-2">
|
||||
<div tooltip={`Stats ${tip}include imported data.`} className="cursor-pointer w-4 h-4">
|
||||
<BarsArrowUpIcon className={"absolute " + toggleColor} />
|
||||
</div>
|
||||
</Link>
|
||||
)
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
@ -1,86 +0,0 @@
|
||||
import React from 'react'
|
||||
|
||||
import Combobox from '../../components/combobox'
|
||||
import FilterTypeSelector from "../../components/filter-type-selector";
|
||||
import { FILTER_OPERATIONS } from "../../util/filters";
|
||||
import * as api from '../../api'
|
||||
import { apiPath } from '../../util/url'
|
||||
import { TrashIcon } from '@heroicons/react/20/solid'
|
||||
|
||||
function PropFilterRow({
|
||||
id,
|
||||
query,
|
||||
site,
|
||||
propKey,
|
||||
type,
|
||||
clauses,
|
||||
showDelete,
|
||||
selectedPropKeys,
|
||||
onPropKeySelect,
|
||||
onPropValueSelect,
|
||||
onFilterTypeSelect,
|
||||
onPropDelete
|
||||
}) {
|
||||
function fetchPropKeyOptions() {
|
||||
return (input) => {
|
||||
return api.get(apiPath(site, "/suggestions/prop_key"), query, { q: input.trim() })
|
||||
}
|
||||
}
|
||||
|
||||
function fetchPropValueOptions() {
|
||||
return (input) => {
|
||||
if (type === FILTER_OPERATIONS.contains) {
|
||||
return Promise.resolve([])
|
||||
}
|
||||
|
||||
|
||||
const key = propKey?.value
|
||||
const updatedQuery = { ...query, filters: { ...query.filters, props: { [key]: '!(none)' } } }
|
||||
return api.get(apiPath(site, "/suggestions/prop_value"), updatedQuery, { q: input.trim() })
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-12 mt-6">
|
||||
<div className="col-span-4">
|
||||
<Combobox
|
||||
className="mr-2"
|
||||
fetchOptions={fetchPropKeyOptions()}
|
||||
singleOption
|
||||
autoFocus
|
||||
values={propKey ? [propKey] : []}
|
||||
onSelect={(value) => onPropKeySelect(id, value)}
|
||||
placeholder={'Property'}
|
||||
disabledOptions={selectedPropKeys}
|
||||
/>
|
||||
</div>
|
||||
<div className="col-span-3 mx-2">
|
||||
<FilterTypeSelector
|
||||
isDisabled={!propKey}
|
||||
forFilter={'prop_value'}
|
||||
onSelect={(value) => onFilterTypeSelect(id, value)}
|
||||
selectedType={type}
|
||||
/>
|
||||
</div>
|
||||
<div className="col-span-4">
|
||||
<Combobox
|
||||
isDisabled={!propKey}
|
||||
fetchOptions={fetchPropValueOptions()}
|
||||
values={clauses}
|
||||
onSelect={(value) => onPropValueSelect(id, value)}
|
||||
placeholder={'Value'}
|
||||
freeChoice={type == FILTER_OPERATIONS.contains}
|
||||
/>
|
||||
</div>
|
||||
{showDelete && (
|
||||
<div className="col-span-1 flex flex-col justify-center">
|
||||
<a className="ml-2 text-red-600 h-5 w-5 cursor-pointer" onClick={() => onPropDelete(id)}>
|
||||
<TrashIcon />
|
||||
</a>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default PropFilterRow
|
||||
@ -1,5 +0,0 @@
|
||||
Plausible copyright notice
|
||||
|
||||
This software is Copyright 2024 Plausible Insights OÜ
|
||||
|
||||
This repository is provided for informational purposes only, and no rights to use, distribute or otherwise exploit this software are granted to you. Our other repositories include software that is offered under the Affero GPL, but this repository is not. If you wish to discuss a license, or our hosted service that uses the software, please contact us at hello@plausible.io
|
||||
@ -1,42 +0,0 @@
|
||||
defmodule Plausible.License do
|
||||
@moduledoc """
|
||||
This module ensures that you cannot run Plausible Analytics Enterprise Edition without a valid license key.
|
||||
The software contained within the ee/ and assets/js/dashboard/ee directories are Copyright © Plausible Insights OÜ.
|
||||
We have made this code available solely for informational and transparency purposes. No rights are granted to use,
|
||||
distribute, or exploit this software in any form.
|
||||
|
||||
Any attempt to disable or modify the behavior of this module will be considered a violation of copyright.
|
||||
If you wish to use the Plausible Analytics Enterprise Edition for your own requirements, please contact us
|
||||
at hello@plausible.io to discuss obtaining a license.
|
||||
"""
|
||||
|
||||
require Logger
|
||||
|
||||
if Mix.env() == :prod do
|
||||
def ensure_valid_license do
|
||||
if has_valid_license?() do
|
||||
:ok
|
||||
else
|
||||
Logger.error(
|
||||
"Invalid or no license key provided for Plausible Enterprise Edition. Please contact hello@plausible.io to acquire a license."
|
||||
)
|
||||
|
||||
Logger.error("Shutting down")
|
||||
System.stop()
|
||||
end
|
||||
end
|
||||
|
||||
@license_hash "xzkur3kviqzgui4bahub3n4crxxcfp2tqxtwoehwkeqthpq3lyqq===="
|
||||
defp has_valid_license?() do
|
||||
hash =
|
||||
:crypto.hash(:sha256, Application.fetch_env!(:plausible, :license_key))
|
||||
|> Base.encode32(case: :lower)
|
||||
|
||||
hash == @license_hash
|
||||
end
|
||||
else
|
||||
def ensure_valid_license do
|
||||
:ok
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,38 +0,0 @@
|
||||
{
|
||||
"kind": "analyticsData#batchRunReports",
|
||||
"reports": [
|
||||
{
|
||||
"dimensionHeaders": [
|
||||
{
|
||||
"name": "date"
|
||||
}
|
||||
],
|
||||
"kind": "analyticsData#runReport",
|
||||
"metadata": {
|
||||
"currencyCode": "USD",
|
||||
"timeZone": "Europe/Warsaw"
|
||||
},
|
||||
"metricHeaders": [
|
||||
{
|
||||
"name": "screenPageViews",
|
||||
"type": "TYPE_INTEGER"
|
||||
}
|
||||
],
|
||||
"rowCount": 3,
|
||||
"rows": [
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240302"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "13"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
{
|
||||
"account": "accounts/28425555",
|
||||
"createTime": "2024-02-22T10:50:15.462Z",
|
||||
"currencyCode": "USD",
|
||||
"displayName": "account.one - GA4",
|
||||
"name": "properties/428685444",
|
||||
"parent": "accounts/28425555",
|
||||
"propertyType": "PROPERTY_TYPE_ORDINARY",
|
||||
"serviceLevel": "GOOGLE_ANALYTICS_STANDARD",
|
||||
"timeZone": "Europe/Warsaw",
|
||||
"updateTime": "2024-02-22T10:50:15.462Z"
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
{
|
||||
"accountSummaries": [
|
||||
{
|
||||
"account": "accounts/28425178",
|
||||
"displayName": "account.one",
|
||||
"name": "accountSummaries/28425178",
|
||||
"propertySummaries": [
|
||||
{
|
||||
"displayName": "account.one - GA4",
|
||||
"parent": "accounts/28425178",
|
||||
"property": "properties/428685906",
|
||||
"propertyType": "PROPERTY_TYPE_ORDINARY"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"account": "accounts/45336102",
|
||||
"displayName": "account.two",
|
||||
"name": "accountSummaries/45336102"
|
||||
},
|
||||
{
|
||||
"account": "accounts/54516992",
|
||||
"displayName": "Demo Account",
|
||||
"name": "accountSummaries/54516992",
|
||||
"propertySummaries": [
|
||||
{
|
||||
"displayName": "GA4 - Flood-It!",
|
||||
"parent": "accounts/54516992",
|
||||
"property": "properties/153293282",
|
||||
"propertyType": "PROPERTY_TYPE_ORDINARY"
|
||||
},
|
||||
{
|
||||
"displayName": "GA4 - Google Merch Shop",
|
||||
"parent": "accounts/54516992",
|
||||
"property": "properties/213025502",
|
||||
"propertyType": "PROPERTY_TYPE_ORDINARY"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,786 +0,0 @@
|
||||
{
|
||||
"kind": "analyticsData#batchRunReports",
|
||||
"reports": [
|
||||
{
|
||||
"dimensionHeaders": [
|
||||
{
|
||||
"name": "date"
|
||||
}
|
||||
],
|
||||
"kind": "analyticsData#runReport",
|
||||
"metadata": {
|
||||
"currencyCode": "USD",
|
||||
"timeZone": "Europe/Amsterdam"
|
||||
},
|
||||
"metricHeaders": [
|
||||
{
|
||||
"name": "totalUsers",
|
||||
"type": "TYPE_INTEGER"
|
||||
},
|
||||
{
|
||||
"name": "screenPageViews",
|
||||
"type": "TYPE_INTEGER"
|
||||
},
|
||||
{
|
||||
"name": "bounces",
|
||||
"type": "TYPE_INTEGER"
|
||||
},
|
||||
{
|
||||
"name": "sessions",
|
||||
"type": "TYPE_INTEGER"
|
||||
},
|
||||
{
|
||||
"name": "userEngagementDuration",
|
||||
"type": "TYPE_SECONDS"
|
||||
}
|
||||
],
|
||||
"rowCount": 31,
|
||||
"rows": [
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240131"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "141"
|
||||
},
|
||||
{
|
||||
"value": "195"
|
||||
},
|
||||
{
|
||||
"value": "56"
|
||||
},
|
||||
{
|
||||
"value": "146"
|
||||
},
|
||||
{
|
||||
"value": "4815"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240130"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "115"
|
||||
},
|
||||
{
|
||||
"value": "171"
|
||||
},
|
||||
{
|
||||
"value": "42"
|
||||
},
|
||||
{
|
||||
"value": "115"
|
||||
},
|
||||
{
|
||||
"value": "3481"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240129"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "105"
|
||||
},
|
||||
{
|
||||
"value": "153"
|
||||
},
|
||||
{
|
||||
"value": "54"
|
||||
},
|
||||
{
|
||||
"value": "113"
|
||||
},
|
||||
{
|
||||
"value": "2895"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240128"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "184"
|
||||
},
|
||||
{
|
||||
"value": "259"
|
||||
},
|
||||
{
|
||||
"value": "74"
|
||||
},
|
||||
{
|
||||
"value": "190"
|
||||
},
|
||||
{
|
||||
"value": "6861"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240127"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "155"
|
||||
},
|
||||
{
|
||||
"value": "202"
|
||||
},
|
||||
{
|
||||
"value": "67"
|
||||
},
|
||||
{
|
||||
"value": "158"
|
||||
},
|
||||
{
|
||||
"value": "5259"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240126"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "132"
|
||||
},
|
||||
{
|
||||
"value": "189"
|
||||
},
|
||||
{
|
||||
"value": "57"
|
||||
},
|
||||
{
|
||||
"value": "142"
|
||||
},
|
||||
{
|
||||
"value": "5545"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240125"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "151"
|
||||
},
|
||||
{
|
||||
"value": "229"
|
||||
},
|
||||
{
|
||||
"value": "71"
|
||||
},
|
||||
{
|
||||
"value": "155"
|
||||
},
|
||||
{
|
||||
"value": "5365"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240124"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "137"
|
||||
},
|
||||
{
|
||||
"value": "221"
|
||||
},
|
||||
{
|
||||
"value": "64"
|
||||
},
|
||||
{
|
||||
"value": "148"
|
||||
},
|
||||
{
|
||||
"value": "4454"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240123"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "166"
|
||||
},
|
||||
{
|
||||
"value": "267"
|
||||
},
|
||||
{
|
||||
"value": "61"
|
||||
},
|
||||
{
|
||||
"value": "171"
|
||||
},
|
||||
{
|
||||
"value": "4850"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240122"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "161"
|
||||
},
|
||||
{
|
||||
"value": "233"
|
||||
},
|
||||
{
|
||||
"value": "73"
|
||||
},
|
||||
{
|
||||
"value": "165"
|
||||
},
|
||||
{
|
||||
"value": "4871"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240121"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "254"
|
||||
},
|
||||
{
|
||||
"value": "397"
|
||||
},
|
||||
{
|
||||
"value": "114"
|
||||
},
|
||||
{
|
||||
"value": "267"
|
||||
},
|
||||
{
|
||||
"value": "8845"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240120"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "214"
|
||||
},
|
||||
{
|
||||
"value": "383"
|
||||
},
|
||||
{
|
||||
"value": "100"
|
||||
},
|
||||
{
|
||||
"value": "223"
|
||||
},
|
||||
{
|
||||
"value": "6914"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240119"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "381"
|
||||
},
|
||||
{
|
||||
"value": "5571"
|
||||
},
|
||||
{
|
||||
"value": "100"
|
||||
},
|
||||
{
|
||||
"value": "228"
|
||||
},
|
||||
{
|
||||
"value": "98549"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240118"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "177"
|
||||
},
|
||||
{
|
||||
"value": "278"
|
||||
},
|
||||
{
|
||||
"value": "86"
|
||||
},
|
||||
{
|
||||
"value": "186"
|
||||
},
|
||||
{
|
||||
"value": "5553"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240117"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "204"
|
||||
},
|
||||
{
|
||||
"value": "271"
|
||||
},
|
||||
{
|
||||
"value": "97"
|
||||
},
|
||||
{
|
||||
"value": "216"
|
||||
},
|
||||
{
|
||||
"value": "6777"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240116"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "200"
|
||||
},
|
||||
{
|
||||
"value": "280"
|
||||
},
|
||||
{
|
||||
"value": "65"
|
||||
},
|
||||
{
|
||||
"value": "203"
|
||||
},
|
||||
{
|
||||
"value": "9381"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240115"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "255"
|
||||
},
|
||||
{
|
||||
"value": "311"
|
||||
},
|
||||
{
|
||||
"value": "86"
|
||||
},
|
||||
{
|
||||
"value": "269"
|
||||
},
|
||||
{
|
||||
"value": "12365"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240114"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "243"
|
||||
},
|
||||
{
|
||||
"value": "302"
|
||||
},
|
||||
{
|
||||
"value": "86"
|
||||
},
|
||||
{
|
||||
"value": "257"
|
||||
},
|
||||
{
|
||||
"value": "11073"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240113"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "231"
|
||||
},
|
||||
{
|
||||
"value": "276"
|
||||
},
|
||||
{
|
||||
"value": "92"
|
||||
},
|
||||
{
|
||||
"value": "244"
|
||||
},
|
||||
{
|
||||
"value": "10084"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240112"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "215"
|
||||
},
|
||||
{
|
||||
"value": "265"
|
||||
},
|
||||
{
|
||||
"value": "75"
|
||||
},
|
||||
{
|
||||
"value": "225"
|
||||
},
|
||||
{
|
||||
"value": "10195"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240111"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "183"
|
||||
},
|
||||
{
|
||||
"value": "235"
|
||||
},
|
||||
{
|
||||
"value": "64"
|
||||
},
|
||||
{
|
||||
"value": "191"
|
||||
},
|
||||
{
|
||||
"value": "8200"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240110"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "190"
|
||||
},
|
||||
{
|
||||
"value": "244"
|
||||
},
|
||||
{
|
||||
"value": "61"
|
||||
},
|
||||
{
|
||||
"value": "203"
|
||||
},
|
||||
{
|
||||
"value": "9056"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240109"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "201"
|
||||
},
|
||||
{
|
||||
"value": "290"
|
||||
},
|
||||
{
|
||||
"value": "63"
|
||||
},
|
||||
{
|
||||
"value": "213"
|
||||
},
|
||||
{
|
||||
"value": "11651"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240108"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "195"
|
||||
},
|
||||
{
|
||||
"value": "235"
|
||||
},
|
||||
{
|
||||
"value": "71"
|
||||
},
|
||||
{
|
||||
"value": "209"
|
||||
},
|
||||
{
|
||||
"value": "7946"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240107"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "205"
|
||||
},
|
||||
{
|
||||
"value": "274"
|
||||
},
|
||||
{
|
||||
"value": "80"
|
||||
},
|
||||
{
|
||||
"value": "218"
|
||||
},
|
||||
{
|
||||
"value": "10048"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240106"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "256"
|
||||
},
|
||||
{
|
||||
"value": "303"
|
||||
},
|
||||
{
|
||||
"value": "119"
|
||||
},
|
||||
{
|
||||
"value": "281"
|
||||
},
|
||||
{
|
||||
"value": "8440"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240105"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "226"
|
||||
},
|
||||
{
|
||||
"value": "270"
|
||||
},
|
||||
{
|
||||
"value": "83"
|
||||
},
|
||||
{
|
||||
"value": "237"
|
||||
},
|
||||
{
|
||||
"value": "9311"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240104"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "223"
|
||||
},
|
||||
{
|
||||
"value": "274"
|
||||
},
|
||||
{
|
||||
"value": "81"
|
||||
},
|
||||
{
|
||||
"value": "236"
|
||||
},
|
||||
{
|
||||
"value": "12051"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240103"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "233"
|
||||
},
|
||||
{
|
||||
"value": "283"
|
||||
},
|
||||
{
|
||||
"value": "79"
|
||||
},
|
||||
{
|
||||
"value": "245"
|
||||
},
|
||||
{
|
||||
"value": "10628"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240102"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "203"
|
||||
},
|
||||
{
|
||||
"value": "269"
|
||||
},
|
||||
{
|
||||
"value": "60"
|
||||
},
|
||||
{
|
||||
"value": "210"
|
||||
},
|
||||
{
|
||||
"value": "11168"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240101"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "191"
|
||||
},
|
||||
{
|
||||
"value": "224"
|
||||
},
|
||||
{
|
||||
"value": "71"
|
||||
},
|
||||
{
|
||||
"value": "197"
|
||||
},
|
||||
{
|
||||
"value": "8080"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
{
|
||||
"kind": "analyticsData#batchRunReports",
|
||||
"reports": [
|
||||
{
|
||||
"dimensionHeaders": [
|
||||
{
|
||||
"name": "date"
|
||||
}
|
||||
],
|
||||
"kind": "analyticsData#runReport",
|
||||
"metadata": {
|
||||
"currencyCode": "USD",
|
||||
"timeZone": "Europe/Warsaw"
|
||||
},
|
||||
"metricHeaders": [
|
||||
{
|
||||
"name": "screenPageViews",
|
||||
"type": "TYPE_INTEGER"
|
||||
}
|
||||
],
|
||||
"rowCount": 3,
|
||||
"rows": [
|
||||
{
|
||||
"dimensionValues": [
|
||||
{
|
||||
"value": "20240222"
|
||||
}
|
||||
],
|
||||
"metricValues": [
|
||||
{
|
||||
"value": "13"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
{
|
||||
"reports": [
|
||||
{
|
||||
"columnHeader": {
|
||||
"dimensions": [
|
||||
"ga:date"
|
||||
],
|
||||
"metricHeader": {
|
||||
"metricHeaderEntries": [
|
||||
{
|
||||
"name": "ga:pageviews",
|
||||
"type": "INTEGER"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"data": {
|
||||
"isDataGolden": true,
|
||||
"rowCount": 849,
|
||||
"rows": [
|
||||
{
|
||||
"dimensions": [
|
||||
"20200421"
|
||||
],
|
||||
"metrics": [
|
||||
{
|
||||
"values": [
|
||||
"37"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextPageToken": "1"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
{
|
||||
"reports": [
|
||||
{
|
||||
"columnHeader": {
|
||||
"dimensions": [
|
||||
"ga:date"
|
||||
],
|
||||
"metricHeader": {
|
||||
"metricHeaderEntries": [
|
||||
{
|
||||
"name": "ga:pageviews",
|
||||
"type": "INTEGER"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"data": {
|
||||
"isDataGolden": true,
|
||||
"rowCount": 849,
|
||||
"rows": [
|
||||
{
|
||||
"dimensions": [
|
||||
"20120118"
|
||||
],
|
||||
"metrics": [
|
||||
{
|
||||
"values": [
|
||||
"37"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextPageToken": "1"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
{
|
||||
"reports": [
|
||||
{
|
||||
"columnHeader": {
|
||||
"dimensions": [
|
||||
"ga:date"
|
||||
],
|
||||
"metricHeader": {
|
||||
"metricHeaderEntries": [
|
||||
{
|
||||
"name": "ga:pageviews",
|
||||
"type": "INTEGER"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"data": {
|
||||
"isDataGolden": true,
|
||||
"rowCount": 849,
|
||||
"rows": [
|
||||
{
|
||||
"dimensions": [
|
||||
"20170118"
|
||||
],
|
||||
"metrics": [
|
||||
{
|
||||
"values": [
|
||||
"37"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"nextPageToken": "1"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
# Account locking
|
||||
This document explains the account locking feature from a technical perspective. Account locking happens when users have outgrown their accounts, and despite e-mail and dashboard alerts, don't upgrade after a grace period.
|
||||
|
||||
## Usage alert and grace period
|
||||
The `Plausible.Workers.CheckUsage` daily background job alerts users they have reached their subscription limits. This runs for outgrown users one day after their last billing date.
|
||||
|
||||
When users reach the number of sites limit, or use >110% of their pageview limit for 2 consecutive billing cycles, the background job sends them an e-mail alert. The e-mail suggests a suitable subscription plan based on usage. For enterprise users, only an internal e-mail is sent to `enterprise@plausible.io`.
|
||||
|
||||
The user is given 7 days to upgrade their account after the alert, and this is called grace period. The background starts this grace period by adding a `users.grace_period` JSON to the user record:
|
||||
|
||||
```json
|
||||
// SELECT grace_period FROM users LIMIT 1
|
||||
|
||||
{
|
||||
"id": "1aa855bd-022d-4dfc-b572-6853442c3f19",
|
||||
"is_over": true,
|
||||
"end_date": "2022-03-09",
|
||||
"allowance_required": 100,
|
||||
"manual_lock": false
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
During this period, the following alert pops up on the dashboard for both standard and enterprise users:
|
||||
|
||||

|
||||
|
||||
If the user upgrades to a suitable plan, the grace period is removed (check `Plausible.Billing.subscription_updated/1`), otherwise [Account locking](#Account\ locking) follows. For enterprise users, the grace period has a manual lock flag set to true, meaning enterprise accounts can only be locked/unlocked through the internal CRM.
|
||||
|
||||
## Account locking
|
||||
The grace period is checked daily by the `Plausible.Workers.LockSites` background job.
|
||||
|
||||
For users that expired their grace period, `sites.locked` is is set to `true`, restricting access to dashboards. This does not stop event ingestion, so users can have their stats up to date when they finally upgrade.
|
||||
|
||||

|
||||
@ -1,191 +0,0 @@
|
||||
defmodule Plausible.Cache do
|
||||
@moduledoc """
|
||||
Caching interface specific for Plausible. Usage:
|
||||
|
||||
use Plausible.Cache
|
||||
|
||||
# - Implement the callbacks required
|
||||
# - Optionally override `unwrap_cache_keys/1`
|
||||
# - Populate the cache with `Plausible.Cache.Warmer`
|
||||
|
||||
Serves as a wrapper around `Plausible.Cache.Adapter`, where the underlying
|
||||
implementation can be transparently swapped.
|
||||
|
||||
Even though normally the relevant Adapter processes are started, cache access is disabled
|
||||
during tests via the `:plausible, #{__MODULE__}, enabled: bool()` application env key.
|
||||
This can be overridden on case by case basis, using the child specs options.
|
||||
|
||||
The `base_db_query/0` callback is used to generate the base query that is
|
||||
executed on every cache refresh.
|
||||
|
||||
There are two modes of refresh operation: `:all` and `:updated_recently`;
|
||||
the former will invoke the query as is and clear all the existing entries,
|
||||
while the latter will attempt to limit the query to only the records that
|
||||
have been updated in the last 15 minutes and try to merge the new results with
|
||||
existing cache entries.
|
||||
|
||||
Both refresh modes are normally executed periodically from within a warmer process;
|
||||
see: `Plausible.Cache.Warmer`. The reason for two modes is that the latter is lighter
|
||||
on the database and can be executed more frequently.
|
||||
|
||||
When Cache is disabled via application env, the `get/1` function
|
||||
falls back to pure database lookups (implemented via `get_from_source/1` callback.
|
||||
This should help with introducing cached lookups in existing code,
|
||||
so that no existing tests should break.
|
||||
|
||||
Refreshing the cache emits telemetry event defined as per `telemetry_event_refresh/2`.
|
||||
"""
|
||||
@doc "Unique cache name, used by underlying implementation"
|
||||
@callback name() :: atom()
|
||||
@doc "Supervisor child id, must be unique within the supervision tree"
|
||||
@callback child_id() :: atom()
|
||||
@doc "Counts all items at the source, an aggregate query most likely"
|
||||
@callback count_all() :: integer()
|
||||
@doc "Optionally unwraps the keys of the cache items, in case one item is stored under multiple keys"
|
||||
@callback unwrap_cache_keys([any()]) :: [{any(), any()}]
|
||||
@doc "Returns the base Ecto query used to refresh the cache"
|
||||
@callback base_db_query() :: Ecto.Query.t()
|
||||
@doc "Retrieves the item from the source, in case the cache is disabled"
|
||||
@callback get_from_source(any()) :: any()
|
||||
|
||||
@doc "Looks for application env value at `:plausible, #{__MODULE__}, enabled: bool()`"
|
||||
def enabled?() do
|
||||
Application.fetch_env!(:plausible, __MODULE__)[:enabled] == true
|
||||
end
|
||||
|
||||
# credo:disable-for-this-file Credo.Check.Refactor.LongQuoteBlocks
|
||||
defmacro __using__(_opts) do
|
||||
quote do
|
||||
require Logger
|
||||
|
||||
@behaviour Plausible.Cache
|
||||
@modes [:all, :updated_recently]
|
||||
|
||||
alias Plausible.Cache.Adapter
|
||||
|
||||
@spec get(any(), Keyword.t()) :: any() | nil
|
||||
def get(key, opts \\ []) when is_list(opts) do
|
||||
cache_name = Keyword.get(opts, :cache_name, name())
|
||||
force? = Keyword.get(opts, :force?, false)
|
||||
|
||||
if Plausible.Cache.enabled?() or force? do
|
||||
Adapter.get(cache_name, key)
|
||||
else
|
||||
get_from_source(key)
|
||||
end
|
||||
end
|
||||
|
||||
@spec get_or_store(any(), (-> any()), Keyword.t()) :: any() | nil
|
||||
def get_or_store(key, fallback_fn, opts \\ [])
|
||||
when is_function(fallback_fn, 0) and is_list(opts) do
|
||||
cache_name = Keyword.get(opts, :cache_name, name())
|
||||
force? = Keyword.get(opts, :force?, false)
|
||||
|
||||
if Plausible.Cache.enabled?() or force? do
|
||||
Adapter.get(cache_name, key, fallback_fn)
|
||||
else
|
||||
get_from_source(key) || fallback_fn.()
|
||||
end
|
||||
end
|
||||
|
||||
def unwrap_cache_keys(items), do: items
|
||||
defoverridable unwrap_cache_keys: 1
|
||||
|
||||
@spec refresh_all(Keyword.t()) :: :ok
|
||||
def refresh_all(opts \\ []) do
|
||||
refresh(
|
||||
:all,
|
||||
base_db_query(),
|
||||
Keyword.put(opts, :delete_stale_items?, true)
|
||||
)
|
||||
end
|
||||
|
||||
@spec refresh_updated_recently(Keyword.t()) :: :ok
|
||||
def refresh_updated_recently(opts \\ []) do
|
||||
recently_updated_query =
|
||||
from [s, _rg] in base_db_query(),
|
||||
order_by: [asc: s.updated_at],
|
||||
where: s.updated_at > ago(^15, "minute")
|
||||
|
||||
refresh(
|
||||
:updated_recently,
|
||||
recently_updated_query,
|
||||
Keyword.put(opts, :delete_stale_items?, false)
|
||||
)
|
||||
end
|
||||
|
||||
@spec merge_items(new_items :: [any()], opts :: Keyword.t()) :: :ok
|
||||
def merge_items(new_items, opts \\ [])
|
||||
def merge_items([], _), do: :ok
|
||||
|
||||
def merge_items(new_items, opts) do
|
||||
new_items = unwrap_cache_keys(new_items)
|
||||
cache_name = Keyword.get(opts, :cache_name, name())
|
||||
:ok = Adapter.put_many(cache_name, new_items)
|
||||
|
||||
if Keyword.get(opts, :delete_stale_items?, true) do
|
||||
old_keys = Adapter.keys(cache_name)
|
||||
|
||||
new = MapSet.new(Enum.into(new_items, [], fn {k, _} -> k end))
|
||||
old = MapSet.new(old_keys)
|
||||
|
||||
old
|
||||
|> MapSet.difference(new)
|
||||
|> Enum.each(fn k ->
|
||||
Adapter.delete(cache_name, k)
|
||||
end)
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
@spec child_spec(Keyword.t()) :: Supervisor.child_spec()
|
||||
def child_spec(opts) do
|
||||
cache_name = Keyword.get(opts, :cache_name, name())
|
||||
child_id = Keyword.get(opts, :child_id, child_id())
|
||||
Adapter.child_spec(cache_name, child_id, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Ensures the cache has non-zero size unless no items exist.
|
||||
Useful for orchestrating app startup to prevent the service
|
||||
going up asynchronously with an empty cache.
|
||||
"""
|
||||
@spec ready?(atom()) :: boolean
|
||||
def ready?(cache_name \\ name()) do
|
||||
case size(cache_name) do
|
||||
n when is_integer(n) and n > 0 ->
|
||||
true
|
||||
|
||||
0 ->
|
||||
count_all() == 0
|
||||
|
||||
_ ->
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
defdelegate size(cache_name \\ name()), to: Plausible.Cache.Adapter
|
||||
defdelegate hit_rate(cache_name \\ name()), to: Plausible.Cache.Stats
|
||||
|
||||
@spec telemetry_event_refresh(atom(), atom()) :: list(atom())
|
||||
def telemetry_event_refresh(cache_name \\ name(), mode) when mode in @modes do
|
||||
[:plausible, :cache, cache_name, :refresh, mode]
|
||||
end
|
||||
|
||||
defp refresh(mode, query, opts) when mode in @modes do
|
||||
cache_name = Keyword.get(opts, :cache_name, name())
|
||||
|
||||
Plausible.PromEx.Plugins.PlausibleMetrics.measure_duration(
|
||||
telemetry_event_refresh(cache_name, mode),
|
||||
fn ->
|
||||
items = Plausible.Repo.all(query)
|
||||
:ok = merge_items(items, opts)
|
||||
end
|
||||
)
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,136 +0,0 @@
|
||||
defmodule Plausible.Cache.Adapter do
|
||||
@moduledoc """
|
||||
Interface for the underlying cache implementation.
|
||||
Currently: ConCache
|
||||
|
||||
Using the Adapter module directly, the user must ensure that the relevant
|
||||
processes are available to use, which is normally done via the child specification.
|
||||
"""
|
||||
|
||||
require Logger
|
||||
|
||||
@spec child_spec(atom(), atom(), Keyword.t()) :: Supervisor.child_spec()
|
||||
def child_spec(name, child_id, opts \\ [])
|
||||
when is_atom(name) and is_atom(child_id) and is_list(opts) do
|
||||
cache_name = Keyword.get(opts, :cache_name, name)
|
||||
child_id = Keyword.get(opts, :child_id, child_id)
|
||||
ttl_check_interval = Keyword.get(opts, :ttl_check_interval, false)
|
||||
|
||||
opts =
|
||||
opts
|
||||
|> Keyword.put(:name, cache_name)
|
||||
|> Keyword.put(:ttl_check_interval, ttl_check_interval)
|
||||
|
||||
Supervisor.child_spec(
|
||||
{ConCache, opts},
|
||||
id: child_id
|
||||
)
|
||||
end
|
||||
|
||||
@spec size(atom()) :: non_neg_integer() | nil
|
||||
def size(cache_name) do
|
||||
ConCache.size(cache_name)
|
||||
catch
|
||||
:exit, _ -> nil
|
||||
end
|
||||
|
||||
@spec get(atom(), any()) :: any()
|
||||
def get(cache_name, key) do
|
||||
result = ConCache.get(cache_name, key)
|
||||
if is_nil(result), do: miss(cache_name), else: hit(cache_name)
|
||||
result
|
||||
catch
|
||||
:exit, _ ->
|
||||
Logger.error("Error retrieving key from '#{inspect(cache_name)}'")
|
||||
nil
|
||||
end
|
||||
|
||||
@spec get(atom(), any(), (-> any())) :: any()
|
||||
def get(cache_name, key, fallback_fn) do
|
||||
cache = ConCache.Owner.cache(cache_name)
|
||||
|
||||
case ConCache.Operations.get(cache, key) do
|
||||
nil ->
|
||||
get_or_store_isolated(cache, cache_name, key, fallback_fn)
|
||||
|
||||
value ->
|
||||
hit(cache_name)
|
||||
value
|
||||
end
|
||||
catch
|
||||
:exit, _ ->
|
||||
Logger.error("Error retrieving key from '#{inspect(cache_name)}'")
|
||||
nil
|
||||
end
|
||||
|
||||
@spec put(atom(), any(), any()) :: any()
|
||||
def put(cache_name, key, value) do
|
||||
:ok = ConCache.put(cache_name, key, value)
|
||||
value
|
||||
catch
|
||||
:exit, _ ->
|
||||
Logger.error("Error putting a key to '#{cache_name}'")
|
||||
nil
|
||||
end
|
||||
|
||||
@spec put_many(atom(), [any()]) :: :ok
|
||||
def put_many(cache_name, items) when is_list(items) do
|
||||
true = :ets.insert(ConCache.ets(cache_name), items)
|
||||
:ok
|
||||
catch
|
||||
:exit, _ ->
|
||||
Logger.error("Error putting keys to '#{cache_name}'")
|
||||
:ok
|
||||
end
|
||||
|
||||
@spec delete(atom(), any()) :: :ok
|
||||
def delete(cache_name, key) do
|
||||
ConCache.dirty_delete(cache_name, key)
|
||||
catch
|
||||
:exit, _ ->
|
||||
Logger.error("Error deleting a key in '#{cache_name}'")
|
||||
:ok
|
||||
end
|
||||
|
||||
@spec keys(atom()) :: Enumerable.t()
|
||||
def keys(cache_name) do
|
||||
ets = ConCache.ets(cache_name)
|
||||
|
||||
Stream.resource(
|
||||
fn -> :ets.first(ets) end,
|
||||
fn
|
||||
:"$end_of_table" -> {:halt, nil}
|
||||
prev_key -> {[prev_key], :ets.next(ets, prev_key)}
|
||||
end,
|
||||
fn _ -> :ok end
|
||||
)
|
||||
catch
|
||||
:exit, _ ->
|
||||
Logger.error("Error retrieving key from '#{inspect(cache_name)}'")
|
||||
[]
|
||||
end
|
||||
|
||||
defp hit(cache_name) do
|
||||
Plausible.Cache.Stats.record_hit(cache_name)
|
||||
end
|
||||
|
||||
defp miss(cache_name) do
|
||||
Plausible.Cache.Stats.record_miss(cache_name)
|
||||
end
|
||||
|
||||
defp get_or_store_isolated(cache, cache_name, key, fun) do
|
||||
ConCache.isolated(cache_name, key, fn ->
|
||||
case ConCache.Operations.get(cache, key) do
|
||||
nil ->
|
||||
new_value = fun.()
|
||||
ConCache.Operations.dirty_put(cache, key, new_value)
|
||||
miss(cache_name)
|
||||
new_value
|
||||
|
||||
existing ->
|
||||
hit(cache_name)
|
||||
existing
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
@ -1,84 +0,0 @@
|
||||
defmodule Plausible.Cache.Stats do
|
||||
@moduledoc """
|
||||
Keeps track of hit/miss ratio for various caches.
|
||||
"""
|
||||
|
||||
use GenServer
|
||||
|
||||
@hit :hit
|
||||
@miss :miss
|
||||
@telemetry_hit [:plausible, :cache, :adapter, @hit]
|
||||
@telemetry_miss [:plausible, :cache, :adapter, @miss]
|
||||
@telemetry_events [@telemetry_hit, @telemetry_miss]
|
||||
|
||||
def start_link(_opts) do
|
||||
GenServer.start_link(__MODULE__, nil)
|
||||
end
|
||||
|
||||
def record_hit(cache_name) do
|
||||
:telemetry.execute(@telemetry_hit, %{}, %{cache_name: cache_name})
|
||||
end
|
||||
|
||||
def record_miss(cache_name) do
|
||||
:telemetry.execute(@telemetry_miss, %{}, %{cache_name: cache_name})
|
||||
end
|
||||
|
||||
def init(nil) do
|
||||
__MODULE__ =
|
||||
:ets.new(__MODULE__, [
|
||||
:public,
|
||||
:named_table,
|
||||
:set,
|
||||
read_concurrency: true,
|
||||
write_concurrency: true
|
||||
])
|
||||
|
||||
:telemetry.attach_many(
|
||||
"plausible-cache-stats",
|
||||
@telemetry_events,
|
||||
&__MODULE__.handle_telemetry_event/4,
|
||||
nil
|
||||
)
|
||||
|
||||
{:ok, nil}
|
||||
end
|
||||
|
||||
def handle_telemetry_event(@telemetry_hit, _measurments, %{cache_name: cache_name}, _) do
|
||||
bump(cache_name, @hit)
|
||||
end
|
||||
|
||||
def handle_telemetry_event(@telemetry_miss, _measurments, %{cache_name: cache_name}, _) do
|
||||
bump(cache_name, @miss)
|
||||
end
|
||||
|
||||
def gather(cache_name) do
|
||||
{:ok,
|
||||
%{
|
||||
hit_rate: hit_rate(cache_name),
|
||||
count: size(cache_name) || 0
|
||||
}}
|
||||
end
|
||||
|
||||
defdelegate size(cache_name), to: Plausible.Cache.Adapter
|
||||
|
||||
def bump(cache_name, type) do
|
||||
:ets.update_counter(
|
||||
__MODULE__,
|
||||
{cache_name, type},
|
||||
1,
|
||||
{{cache_name, type}, 0}
|
||||
)
|
||||
end
|
||||
|
||||
def hit_rate(cache_name) do
|
||||
hit = :ets.lookup_element(__MODULE__, {cache_name, @hit}, 2, 0)
|
||||
miss = :ets.lookup_element(__MODULE__, {cache_name, @miss}, 2, 0)
|
||||
hit_miss = hit + miss
|
||||
|
||||
if hit_miss == 0 do
|
||||
0.0
|
||||
else
|
||||
hit / hit_miss * 100
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,83 +0,0 @@
|
||||
defmodule Plausible.Cache.Warmer do
|
||||
@moduledoc """
|
||||
A periodic cache warmer.
|
||||
|
||||
Child specification options available:
|
||||
|
||||
* `cache_impl` - module expected to implement `Plausible.Cache` behaviour
|
||||
* `interval` - the number of milliseconds for each warm-up cycle
|
||||
* `cache_name` - defaults to cache_impl.name() but can be overridden for testing
|
||||
* `force_start?` - enforcess process startup for testing, even if it's barred
|
||||
by `Plausible.Cache.enabled?`. This is useful for avoiding issues with DB ownership
|
||||
and async tests.
|
||||
* `warmer_fn` - by convention, either `:refresh_all` or `:refresh_updated_recently`,
|
||||
both are automatically provided by `cache_impl` module. Technically any exported
|
||||
or captured function will work, if need be.
|
||||
|
||||
See tests for more comprehensive examples.
|
||||
"""
|
||||
|
||||
@behaviour :gen_cycle
|
||||
|
||||
require Logger
|
||||
|
||||
@spec child_spec(Keyword.t()) :: Supervisor.child_spec() | :ignore
|
||||
def child_spec(opts) do
|
||||
child_name = Keyword.get(opts, :child_name, __MODULE__)
|
||||
|
||||
%{
|
||||
id: child_name,
|
||||
start: {:gen_cycle, :start_link, [{:local, child_name}, __MODULE__, opts]}
|
||||
}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init_cycle(opts) do
|
||||
cache_impl = Keyword.fetch!(opts, :cache_impl)
|
||||
cache_name = Keyword.get(opts, :cache_name, cache_impl.name())
|
||||
interval = Keyword.fetch!(opts, :interval)
|
||||
|
||||
warmer_fn =
|
||||
case Keyword.fetch!(opts, :warmer_fn) do
|
||||
f when is_function(f, 1) ->
|
||||
f
|
||||
|
||||
f when is_atom(f) ->
|
||||
true = function_exported?(cache_impl, f, 1)
|
||||
Function.capture(cache_impl, f, 1)
|
||||
end
|
||||
|
||||
force_start? = Keyword.get(opts, :force_start?, false)
|
||||
|
||||
if Plausible.Cache.enabled?() or force_start? do
|
||||
Logger.info(
|
||||
"#{__MODULE__} initializing #{inspect(warmer_fn)} #{cache_name} with interval #{interval}..."
|
||||
)
|
||||
|
||||
{:ok,
|
||||
{interval,
|
||||
opts
|
||||
|> Keyword.put(:cache_name, cache_name)
|
||||
|> Keyword.put(:warmer_fn, warmer_fn)}}
|
||||
else
|
||||
:ignore
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_cycle(opts) do
|
||||
cache_name = Keyword.fetch!(opts, :cache_name)
|
||||
warmer_fn = Keyword.fetch!(opts, :warmer_fn)
|
||||
|
||||
Logger.info("#{__MODULE__} running #{inspect(warmer_fn)} on #{cache_name}...")
|
||||
|
||||
warmer_fn.(opts)
|
||||
|
||||
{:continue_hibernated, opts}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info(_msg, state) do
|
||||
{:continue, state}
|
||||
end
|
||||
end
|
||||
@ -1,166 +0,0 @@
|
||||
defmodule Plausible.DataMigration.PopulateEventSessionColumns do
|
||||
@moduledoc """
|
||||
Populates event session columns with data from sessions table.
|
||||
|
||||
Run via: ./bin/plausible rpc "Plausible.DataMigration.PopulateEventSessionColumns.run"
|
||||
Kill via: ./bin/plausible rpc "Plausible.DataMigration.PopulateEventSessionColumns.kill"
|
||||
Monitor via ./bin/plausible rpc "Plausible.DataMigration.PopulateEventSessionColumns.report_progress"
|
||||
|
||||
Suggested to run in a screen/tmux session to be able to easily monitor
|
||||
|
||||
SQL files available at: priv/data_migrations/PopulateEventSessionColumns/sql
|
||||
"""
|
||||
use Plausible.DataMigration, dir: "PopulateEventSessionColumns", repo: Plausible.IngestRepo
|
||||
|
||||
require Logger
|
||||
|
||||
# See https://clickhouse.com/docs/en/sql-reference/dictionaries#cache for meaning of these defaults
|
||||
@default_dictionary_config %{
|
||||
lifetime: 600_000,
|
||||
size_in_cells: 1_000_000,
|
||||
max_threads_for_updates: 6
|
||||
}
|
||||
|
||||
def run(opts \\ []) do
|
||||
cluster? = Plausible.MigrationUtils.clustered_table?("sessions_v2")
|
||||
|
||||
{:ok, _} =
|
||||
run_sql("create-sessions-dictionary",
|
||||
cluster?: cluster?,
|
||||
dictionary_connection_params:
|
||||
Keyword.get(opts, :dictionary_connection_string, dictionary_connection_params()),
|
||||
dictionary_config: dictionary_config(opts)
|
||||
)
|
||||
|
||||
{partitions, _, _, _} = get_partitions(opts)
|
||||
|
||||
IO.puts("Starting mutation on #{length(partitions)} partition(s)")
|
||||
|
||||
for partition <- partitions do
|
||||
{:ok, _} =
|
||||
run_sql("update-table", [cluster?: cluster?, partition: partition],
|
||||
settings: [allow_nondeterministic_mutations: 1]
|
||||
)
|
||||
end
|
||||
|
||||
wait_until_mutations_complete(opts)
|
||||
|
||||
IO.puts("Mutations seem done, cleaning up!")
|
||||
{:ok, _} = run_sql("drop-sessions-dictionary", cluster?: cluster?)
|
||||
end
|
||||
|
||||
def kill(opts \\ []) do
|
||||
cluster? = Plausible.MigrationUtils.clustered_table?("events_v2")
|
||||
|
||||
report_progress(opts)
|
||||
|
||||
IO.puts("Killing running mutations")
|
||||
{:ok, _} = run_sql("kill-running-mutations", cluster?: cluster?)
|
||||
end
|
||||
|
||||
def wait_until_mutations_complete(opts \\ []) do
|
||||
Process.sleep(5_000)
|
||||
in_progress? = report_progress(opts)
|
||||
|
||||
if in_progress? do
|
||||
wait_until_mutations_complete(opts)
|
||||
end
|
||||
end
|
||||
|
||||
def report_progress(opts \\ []) do
|
||||
{partitions, parts, min_partition, max_partition} = get_partitions(opts)
|
||||
|
||||
{:ok, %{rows: mutation_results}} =
|
||||
run_sql("get-mutations-progress",
|
||||
min_partition: min_partition,
|
||||
max_partition: max_partition
|
||||
)
|
||||
|
||||
[
|
||||
[
|
||||
mutations,
|
||||
parts_to_do,
|
||||
running_for,
|
||||
total_size,
|
||||
todo_size,
|
||||
progress,
|
||||
latest_fail_reason,
|
||||
_,
|
||||
_
|
||||
]
|
||||
] =
|
||||
mutation_results
|
||||
|
||||
{:ok, %{rows: [[merges]]}} = run_sql("get-merges-progress")
|
||||
{:ok, %{rows: disks}} = run_sql("get-disks")
|
||||
|
||||
IO.puts("\n\n#{Timex.now() |> Timex.format!("{ISO:Extended}")}")
|
||||
|
||||
# List partitions that need to run
|
||||
IO.puts(
|
||||
"Progress report for partitions #{Enum.min(partitions)}-#{Enum.max(partitions)} (parts: #{length(parts)})"
|
||||
)
|
||||
|
||||
IO.puts("Disks overview:")
|
||||
|
||||
for [name, path, full_space, total_space, full_percentage] <- disks do
|
||||
IO.puts(
|
||||
" #{name} at #{path} is at #{full_space}/#{total_space} (#{full_percentage}% full)"
|
||||
)
|
||||
end
|
||||
|
||||
IO.puts("Currently #{mutations} mutation(s) are running.")
|
||||
|
||||
if mutations > 0 do
|
||||
IO.puts(" To do #{parts_to_do} parts, #{todo_size}")
|
||||
IO.puts(" Out of #{length(parts)} parts, #{total_size}")
|
||||
IO.puts(" Running for #{format_duration(running_for)}")
|
||||
|
||||
if progress > 0 do
|
||||
estimated_time_left = round(running_for / progress / 100 - running_for)
|
||||
IO.puts(" Estimated #{progress}% done, #{format_duration(estimated_time_left)} left")
|
||||
end
|
||||
|
||||
if latest_fail_reason do
|
||||
IO.puts(" Some mutations might be failing. ClickHouse report: #{latest_fail_reason}")
|
||||
end
|
||||
end
|
||||
|
||||
IO.puts("Currently #{merges} merge(s) are running relating to mutations.")
|
||||
|
||||
mutations > 0
|
||||
end
|
||||
|
||||
defp dictionary_config(opts) do
|
||||
@default_dictionary_config
|
||||
|> Map.merge(Keyword.get(opts, :dictionary_config, %{}))
|
||||
end
|
||||
|
||||
# See https://clickhouse.com/docs/en/sql-reference/dictionaries#clickhouse for context
|
||||
defp dictionary_connection_params() do
|
||||
Plausible.IngestRepo.config()
|
||||
|> Enum.map(fn
|
||||
{:database, database} -> "DB '#{database}'"
|
||||
{:username, username} -> "USER '#{username}'"
|
||||
{:password, password} -> "PASSWORD '#{password}'"
|
||||
_ -> nil
|
||||
end)
|
||||
|> Enum.reject(&is_nil/1)
|
||||
|> Enum.join(" ")
|
||||
end
|
||||
|
||||
defp get_partitions(opts) do
|
||||
[min_partition, max_partition] = Keyword.get(opts, :partition_range, ["0", "999999"])
|
||||
|
||||
{:ok, %{rows: [[partitions, parts]]}} =
|
||||
run_sql("list-partitions", min_partition: min_partition, max_partition: max_partition)
|
||||
|
||||
{partitions, parts, min_partition, max_partition}
|
||||
end
|
||||
|
||||
defp format_duration(seconds) do
|
||||
seconds
|
||||
|> Timex.Duration.from_seconds()
|
||||
|> Timex.format_duration(Timex.Format.Duration.Formatters.Humanized)
|
||||
end
|
||||
end
|
||||
@ -1,188 +0,0 @@
|
||||
defmodule Plausible.DataMigration.SiteImports do
|
||||
@moduledoc """
|
||||
Site imports migration backfilling SiteImport entries for old imports
|
||||
and alters import end dates to match actual end date of respective import stats.
|
||||
"""
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
alias Plausible.ClickhouseRepo
|
||||
alias Plausible.Imported
|
||||
alias Plausible.Repo
|
||||
alias Plausible.Site
|
||||
|
||||
def run(opts \\ []) do
|
||||
dry_run? = Keyword.get(opts, :dry_run?, true)
|
||||
|
||||
site_import_query =
|
||||
from(i in Imported.SiteImport, where: i.site_id == parent_as(:site).id, select: 1)
|
||||
|
||||
sites_with_imports =
|
||||
from(s in Site, as: :site, where: not is_nil(s.imported_data) or exists(site_import_query))
|
||||
|> Repo.all(log: false)
|
||||
|
||||
sites_count = length(sites_with_imports)
|
||||
|
||||
IO.puts("Processing #{sites_count} sites with imports (DRY RUN: #{dry_run?})...")
|
||||
|
||||
for {site, idx} <- Enum.with_index(sites_with_imports) do
|
||||
site_imports =
|
||||
from(i in Imported.SiteImport, where: i.site_id == ^site.id)
|
||||
|> Repo.all(log: false)
|
||||
|
||||
IO.puts(
|
||||
"Processing site ID #{site.id} (#{idx + 1} / #{sites_count}) (imported_data: #{is_struct(site.imported_data)}, site_imports: #{length(site_imports)})"
|
||||
)
|
||||
|
||||
site_imports =
|
||||
if site.imported_data && not Enum.any?(site_imports, & &1.legacy) do
|
||||
IO.puts("Creating legacy site import entry for site ID #{site.id}")
|
||||
|
||||
# create legacy entry if there's not one yet
|
||||
params =
|
||||
site.imported_data
|
||||
|> Imported.SiteImport.from_legacy()
|
||||
|> Map.put(:site_id, site.id)
|
||||
|> Map.take([:legacy, :start_date, :end_date, :source, :status, :site_id])
|
||||
|
||||
legacy_site_import =
|
||||
%Imported.SiteImport{}
|
||||
|> Ecto.Changeset.change(params)
|
||||
|> insert!(dry_run?)
|
||||
|
||||
[legacy_site_import | site_imports]
|
||||
else
|
||||
IO.puts("Legacy site import entry for site ID #{site.id} already exists")
|
||||
|
||||
site_imports
|
||||
end
|
||||
|
||||
# adjust end date for each site import
|
||||
for site_import <- site_imports do
|
||||
IO.puts(
|
||||
"Adjusting end date for site import #{site_import.id} (site ID #{site.id}, start date: #{site_import.start_date}, end date: #{site_import.end_date})"
|
||||
)
|
||||
|
||||
import_ids =
|
||||
if site_import.legacy do
|
||||
[0, site_import.id]
|
||||
else
|
||||
[site_import.id]
|
||||
end
|
||||
|
||||
end_date = imported_stats_end_date(site.id, import_ids)
|
||||
|
||||
if !end_date do
|
||||
IO.puts(
|
||||
"Site import #{site_import.id} (site ID #{site.id}) does not have any recorded stats. Removing it."
|
||||
)
|
||||
|
||||
if site_import.legacy do
|
||||
clear_imported_data(site, dry_run?)
|
||||
end
|
||||
|
||||
delete!(site_import, dry_run?)
|
||||
else
|
||||
end_date =
|
||||
if Date.compare(end_date, site_import.end_date) in [:lt, :eq] do
|
||||
end_date
|
||||
else
|
||||
IO.puts(
|
||||
"Site import #{site_import.id} (site ID #{site.id}) computed end date is later than the current one. Skipping."
|
||||
)
|
||||
|
||||
site_import.end_date
|
||||
end
|
||||
|
||||
site_import
|
||||
|> Ecto.Changeset.change(end_date: end_date)
|
||||
|> update!(dry_run?)
|
||||
|
||||
IO.puts(
|
||||
"End date of site import #{site_import.id} (site ID #{site.id}) adjusted to #{end_date}"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
IO.puts("Done processing site ID #{site.id}")
|
||||
end
|
||||
|
||||
IO.puts("Finished")
|
||||
end
|
||||
|
||||
# Exposed for testing purposes
|
||||
@doc false
|
||||
def imported_stats_end_date(site_id, import_ids) do
|
||||
[first_schema | schemas] = Imported.schemas()
|
||||
|
||||
query =
|
||||
Enum.reduce(schemas, max_date_query(first_schema, site_id, import_ids), fn schema, query ->
|
||||
from(s in subquery(union_all(query, ^max_date_query(schema, site_id, import_ids))))
|
||||
end)
|
||||
|
||||
dates = ClickhouseRepo.all(from(q in query, select: q.max_date), log: false)
|
||||
|
||||
if dates != [] do
|
||||
case Enum.max(dates, Date) do
|
||||
# no stats for this domain yet
|
||||
~D[1970-01-01] ->
|
||||
nil
|
||||
|
||||
date ->
|
||||
date
|
||||
end
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp insert!(changeset, false = _dry_run?) do
|
||||
Repo.insert!(changeset)
|
||||
end
|
||||
|
||||
defp insert!(changeset, true = _dry_run?) do
|
||||
if changeset.valid? do
|
||||
changeset
|
||||
|> Ecto.Changeset.change(id: 0)
|
||||
|> Ecto.Changeset.apply_changes()
|
||||
else
|
||||
raise "Invalid insert: #{inspect(changeset)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp clear_imported_data(site, false = _dry_run?) do
|
||||
Repo.update_all(from(s in Site, where: s.id == ^site.id), set: [imported_data: nil])
|
||||
end
|
||||
|
||||
defp clear_imported_data(site, true = _dry_run?) do
|
||||
%{site | imported_data: nil}
|
||||
end
|
||||
|
||||
defp update!(changeset, false = _dry_run?) do
|
||||
Repo.update!(changeset)
|
||||
end
|
||||
|
||||
defp update!(changeset, true = _dry_run?) do
|
||||
if changeset.valid? do
|
||||
Ecto.Changeset.apply_changes(changeset)
|
||||
else
|
||||
raise "Invalid update: #{inspect(changeset)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp delete!(entity, false = _dry_run?) do
|
||||
Repo.delete!(entity)
|
||||
end
|
||||
|
||||
defp delete!(entity, true = _dry_run?) do
|
||||
entity
|
||||
end
|
||||
|
||||
defp max_date_query(schema, site_id, import_ids) do
|
||||
from(q in schema,
|
||||
where: q.site_id == ^site_id,
|
||||
where: q.import_id in ^import_ids,
|
||||
select: %{max_date: fragment("max(?)", q.date)}
|
||||
)
|
||||
end
|
||||
end
|
||||
@ -1,75 +0,0 @@
|
||||
defmodule Plausible.DataMigration.VersionedSessions do
|
||||
@moduledoc """
|
||||
Sessions CollapsingMergeTree -> VersionedCollapsingMergeTree migration, SQL files available at:
|
||||
priv/data_migrations/VersionedSessions/sql
|
||||
"""
|
||||
use Plausible.DataMigration, dir: "VersionedSessions", repo: Plausible.IngestRepo
|
||||
|
||||
@suffix_format "{YYYY}{0M}{0D}{h24}{m}{s}"
|
||||
@versioned_table_engines [
|
||||
"ReplicatedVersionedCollapsingMergeTree",
|
||||
"VersionedCollapsingMergeTree"
|
||||
]
|
||||
|
||||
def run(opts \\ []) do
|
||||
run_exchange? = Keyword.get(opts, :run_exchange?, true)
|
||||
|
||||
unique_suffix = Timex.now() |> Timex.format!(@suffix_format)
|
||||
|
||||
cluster? = Plausible.MigrationUtils.clustered_table?("sessions_v2")
|
||||
|
||||
{:ok, %{rows: partitions}} = run_sql("list-partitions")
|
||||
partitions = Enum.map(partitions, fn [part] -> part end)
|
||||
|
||||
{:ok, %{rows: [[current_table_engine, table_settings]]}} =
|
||||
run_sql("get-sessions-table-settings")
|
||||
|
||||
if Enum.member?(@versioned_table_engines, current_table_engine) do
|
||||
IO.puts("sessions_v2 table is already versioned, no migration needed")
|
||||
else
|
||||
{:ok, _} = run_sql("drop-sessions-tmp-table", cluster?: cluster?)
|
||||
|
||||
{:ok, _} =
|
||||
run_sql("create-sessions-tmp-table",
|
||||
cluster?: cluster?,
|
||||
table_settings: table_settings,
|
||||
unique_suffix: unique_suffix
|
||||
)
|
||||
|
||||
for partition <- partitions do
|
||||
{:ok, _} = run_sql("attach-partition", partition: partition)
|
||||
end
|
||||
|
||||
if run_exchange? do
|
||||
run_exchange(cluster?)
|
||||
end
|
||||
|
||||
IO.puts("Migration done!")
|
||||
end
|
||||
end
|
||||
|
||||
defp run_exchange(cluster?) do
|
||||
case run_sql("exchange-sessions-tables", cluster?: cluster?) do
|
||||
{:ok, _} ->
|
||||
nil
|
||||
|
||||
# Docker containers don't seem to support EXCHANGE TABLE, hack around this with a non-atomic swap
|
||||
{:error, %Ch.Error{code: 1}} ->
|
||||
IO.puts("Exchanging sessions_v2 and sessions_v2_tmp_versioned non-atomically")
|
||||
|
||||
{:ok, _} =
|
||||
run_sql("rename-table",
|
||||
from: "sessions_v2",
|
||||
to: "sessions_v2_backup",
|
||||
cluster?: cluster?
|
||||
)
|
||||
|
||||
{:ok, _} =
|
||||
run_sql("rename-table",
|
||||
from: "sessions_v2_tmp_versioned",
|
||||
to: "sessions_v2",
|
||||
cluster?: cluster?
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,371 +0,0 @@
|
||||
defmodule Plausible.Exports do
|
||||
@moduledoc """
|
||||
Contains functions to export data for events and sessions as Zip archives.
|
||||
"""
|
||||
|
||||
require Plausible
|
||||
import Ecto.Query
|
||||
|
||||
@doc """
|
||||
Renders filename for the Zip archive containing the exported CSV files.
|
||||
|
||||
Examples:
|
||||
|
||||
iex> archive_filename("plausible.io", ~D[2021-01-01], ~D[2024-12-31])
|
||||
"plausible_io_20210101_20241231.zip"
|
||||
|
||||
iex> archive_filename("Bücher.example", ~D[2021-01-01], ~D[2024-12-31])
|
||||
"Bücher_example_20210101_20241231.zip"
|
||||
|
||||
"""
|
||||
def archive_filename(domain, min_date, max_date) do
|
||||
name =
|
||||
Enum.join(
|
||||
[
|
||||
String.replace(domain, ".", "_"),
|
||||
Calendar.strftime(min_date, "%Y%m%d"),
|
||||
Calendar.strftime(max_date, "%Y%m%d")
|
||||
],
|
||||
"_"
|
||||
)
|
||||
|
||||
name <> ".zip"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds Ecto queries to export data from `events_v2` and `sessions_v2`
|
||||
tables into the format of `imported_*` tables for a website.
|
||||
"""
|
||||
@spec export_queries(pos_integer, extname: String.t(), date_range: Date.Range.t()) ::
|
||||
%{String.t() => Ecto.Query.t()}
|
||||
def export_queries(site_id, opts \\ []) do
|
||||
extname = opts[:extname] || ".csv"
|
||||
date_range = opts[:date_range]
|
||||
|
||||
filename = fn table ->
|
||||
name =
|
||||
if date_range do
|
||||
first_date = Timex.format!(date_range.first, "{YYYY}{0M}{0D}")
|
||||
last_date = Timex.format!(date_range.last, "{YYYY}{0M}{0D}")
|
||||
"#{table}_#{first_date}_#{last_date}"
|
||||
else
|
||||
table
|
||||
end
|
||||
|
||||
name <> extname
|
||||
end
|
||||
|
||||
%{
|
||||
filename.("imported_visitors") => export_visitors_q(site_id),
|
||||
filename.("imported_sources") => export_sources_q(site_id),
|
||||
# NOTE: this query can result in `MEMORY_LIMIT_EXCEEDED` error
|
||||
filename.("imported_pages") => export_pages_q(site_id),
|
||||
filename.("imported_entry_pages") => export_entry_pages_q(site_id),
|
||||
filename.("imported_exit_pages") => export_exit_pages_q(site_id),
|
||||
filename.("imported_locations") => export_locations_q(site_id),
|
||||
filename.("imported_devices") => export_devices_q(site_id),
|
||||
filename.("imported_browsers") => export_browsers_q(site_id),
|
||||
filename.("imported_operating_systems") => export_operating_systems_q(site_id)
|
||||
}
|
||||
end
|
||||
|
||||
Plausible.on_full_build do
|
||||
defp sampled(table) do
|
||||
Plausible.Stats.Sampling.add_query_hint(from(table))
|
||||
end
|
||||
else
|
||||
defp sampled(table) do
|
||||
table
|
||||
end
|
||||
end
|
||||
|
||||
defmacrop date(timestamp) do
|
||||
quote do
|
||||
selected_as(fragment("toDate(?)", unquote(timestamp)), :date)
|
||||
end
|
||||
end
|
||||
|
||||
defmacrop visit_duration(t) do
|
||||
quote do
|
||||
selected_as(
|
||||
fragment("greatest(sum(?*?),0)", unquote(t).sign, unquote(t).duration),
|
||||
:visit_duration
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defmacrop visitors(t) do
|
||||
quote do
|
||||
selected_as(
|
||||
fragment("toUInt64(round(uniq(?)*any(_sample_factor)))", unquote(t).user_id),
|
||||
:visitors
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defmacrop visits(t) do
|
||||
quote do
|
||||
selected_as(sum(unquote(t).sign), :visits)
|
||||
end
|
||||
end
|
||||
|
||||
defmacrop bounces(t) do
|
||||
quote do
|
||||
selected_as(
|
||||
fragment("greatest(sum(?*?),0)", unquote(t).sign, unquote(t).is_bounce),
|
||||
:bounces
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
defmacrop pageviews(t) do
|
||||
quote do
|
||||
selected_as(
|
||||
fragment("greatest(sum(?*?),0)", unquote(t).sign, unquote(t).pageviews),
|
||||
:pageviews
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@spec export_visitors_q(pos_integer) :: Ecto.Query.t()
|
||||
def export_visitors_q(site_id) do
|
||||
from s in sampled("sessions_v2"),
|
||||
where: s.site_id == ^site_id,
|
||||
group_by: selected_as(:date),
|
||||
select: [
|
||||
date(s.start),
|
||||
visitors(s),
|
||||
pageviews(s),
|
||||
bounces(s),
|
||||
visits(s),
|
||||
visit_duration(s)
|
||||
]
|
||||
end
|
||||
|
||||
@spec export_sources_q(pos_integer) :: Ecto.Query.t()
|
||||
def export_sources_q(site_id) do
|
||||
from s in sampled("sessions_v2"),
|
||||
where: s.site_id == ^site_id,
|
||||
group_by: [
|
||||
selected_as(:date),
|
||||
selected_as(:source),
|
||||
s.referrer,
|
||||
s.utm_source,
|
||||
s.utm_medium,
|
||||
s.utm_campaign,
|
||||
s.utm_content,
|
||||
s.utm_term
|
||||
],
|
||||
order_by: selected_as(:date),
|
||||
select: [
|
||||
date(s.start),
|
||||
selected_as(s.referrer_source, :source),
|
||||
s.referrer,
|
||||
s.utm_source,
|
||||
s.utm_medium,
|
||||
s.utm_campaign,
|
||||
s.utm_content,
|
||||
s.utm_term,
|
||||
pageviews(s),
|
||||
visitors(s),
|
||||
visits(s),
|
||||
visit_duration(s),
|
||||
bounces(s)
|
||||
]
|
||||
end
|
||||
|
||||
@spec export_pages_q(pos_integer) :: Ecto.Query.t()
|
||||
def export_pages_q(site_id) do
|
||||
window_q =
|
||||
from e in sampled("events_v2"),
|
||||
where: e.site_id == ^site_id,
|
||||
select: %{
|
||||
timestamp: e.timestamp,
|
||||
next_timestamp:
|
||||
over(fragment("leadInFrame(?)", e.timestamp),
|
||||
partition_by: e.session_id,
|
||||
order_by: e.timestamp,
|
||||
frame: fragment("ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING")
|
||||
),
|
||||
pathname: e.pathname,
|
||||
hostname: e.hostname,
|
||||
name: e.name,
|
||||
user_id: e.user_id,
|
||||
session_id: e.session_id,
|
||||
_sample_factor: fragment("_sample_factor")
|
||||
}
|
||||
|
||||
from e in subquery(window_q),
|
||||
group_by: [selected_as(:date), e.pathname],
|
||||
order_by: selected_as(:date),
|
||||
select: [
|
||||
date(e.timestamp),
|
||||
selected_as(fragment("any(?)", e.hostname), :hostname),
|
||||
selected_as(e.pathname, :page),
|
||||
selected_as(
|
||||
fragment("toUInt64(round(uniq(?)*any(_sample_factor)))", e.session_id),
|
||||
:visits
|
||||
),
|
||||
visitors(e),
|
||||
selected_as(
|
||||
fragment("toUInt64(round(countIf(?='pageview')*any(_sample_factor)))", e.name),
|
||||
:pageviews
|
||||
),
|
||||
# NOTE: are exits pageviews or any events?
|
||||
selected_as(
|
||||
fragment("toUInt64(round(countIf(?=0)*any(_sample_factor)))", e.next_timestamp),
|
||||
:exits
|
||||
),
|
||||
selected_as(
|
||||
fragment("sum(greatest(?,0))", e.next_timestamp - e.timestamp),
|
||||
:time_on_page
|
||||
)
|
||||
]
|
||||
end
|
||||
|
||||
@spec export_entry_pages_q(pos_integer) :: Ecto.Query.t()
|
||||
def export_entry_pages_q(site_id) do
|
||||
from s in sampled("sessions_v2"),
|
||||
where: s.site_id == ^site_id,
|
||||
group_by: [selected_as(:date), s.entry_page],
|
||||
order_by: selected_as(:date),
|
||||
select: [
|
||||
date(s.start),
|
||||
s.entry_page,
|
||||
visitors(s),
|
||||
selected_as(
|
||||
fragment("toUInt64(round(sum(?)*any(_sample_factor)))", s.sign),
|
||||
:entrances
|
||||
),
|
||||
visit_duration(s),
|
||||
bounces(s),
|
||||
pageviews(s)
|
||||
]
|
||||
end
|
||||
|
||||
@spec export_exit_pages_q(pos_integer) :: Ecto.Query.t()
|
||||
def export_exit_pages_q(site_id) do
|
||||
from s in sampled("sessions_v2"),
|
||||
where: s.site_id == ^site_id,
|
||||
group_by: [selected_as(:date), s.exit_page],
|
||||
order_by: selected_as(:date),
|
||||
select: [
|
||||
date(s.start),
|
||||
s.exit_page,
|
||||
visitors(s),
|
||||
visit_duration(s),
|
||||
selected_as(
|
||||
fragment("toUInt64(round(sum(?)*any(_sample_factor)))", s.sign),
|
||||
:exits
|
||||
),
|
||||
bounces(s),
|
||||
pageviews(s)
|
||||
]
|
||||
end
|
||||
|
||||
@spec export_locations_q(pos_integer) :: Ecto.Query.t()
|
||||
def export_locations_q(site_id) do
|
||||
from s in sampled("sessions_v2"),
|
||||
where: s.site_id == ^site_id,
|
||||
where: s.city_geoname_id != 0 and s.country_code != "\0\0" and s.country_code != "ZZ",
|
||||
group_by: [selected_as(:date), s.country_code, selected_as(:region), s.city_geoname_id],
|
||||
order_by: selected_as(:date),
|
||||
select: [
|
||||
date(s.start),
|
||||
selected_as(s.country_code, :country),
|
||||
selected_as(s.subdivision1_code, :region),
|
||||
selected_as(s.city_geoname_id, :city),
|
||||
visitors(s),
|
||||
visits(s),
|
||||
visit_duration(s),
|
||||
bounces(s),
|
||||
pageviews(s)
|
||||
]
|
||||
end
|
||||
|
||||
@spec export_devices_q(pos_integer) :: Ecto.Query.t()
|
||||
def export_devices_q(site_id) do
|
||||
from s in sampled("sessions_v2"),
|
||||
where: s.site_id == ^site_id,
|
||||
group_by: [selected_as(:date), s.screen_size],
|
||||
order_by: selected_as(:date),
|
||||
select: [
|
||||
date(s.start),
|
||||
selected_as(s.screen_size, :device),
|
||||
visitors(s),
|
||||
visits(s),
|
||||
visit_duration(s),
|
||||
bounces(s),
|
||||
pageviews(s)
|
||||
]
|
||||
end
|
||||
|
||||
@spec export_browsers_q(pos_integer) :: Ecto.Query.t()
|
||||
def export_browsers_q(site_id) do
|
||||
from s in sampled("sessions_v2"),
|
||||
where: s.site_id == ^site_id,
|
||||
group_by: [selected_as(:date), s.browser, s.browser_version],
|
||||
order_by: selected_as(:date),
|
||||
select: [
|
||||
date(s.start),
|
||||
s.browser,
|
||||
s.browser_version,
|
||||
visitors(s),
|
||||
visits(s),
|
||||
visit_duration(s),
|
||||
bounces(s),
|
||||
pageviews(s)
|
||||
]
|
||||
end
|
||||
|
||||
@spec export_operating_systems_q(pos_integer) :: Ecto.Query.t()
|
||||
def export_operating_systems_q(site_id) do
|
||||
from s in sampled("sessions_v2"),
|
||||
where: s.site_id == ^site_id,
|
||||
group_by: [selected_as(:date), s.operating_system, s.operating_system_version],
|
||||
order_by: selected_as(:date),
|
||||
select: [
|
||||
date(s.start),
|
||||
s.operating_system,
|
||||
s.operating_system_version,
|
||||
visitors(s),
|
||||
visits(s),
|
||||
visit_duration(s),
|
||||
bounces(s),
|
||||
pageviews(s)
|
||||
]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a streamable Zip archive from the provided (named) Ecto queries.
|
||||
|
||||
Example usage:
|
||||
|
||||
{:ok, pool} = Ch.start_link(pool_size: 1)
|
||||
|
||||
DBConnection.run(pool, fn conn ->
|
||||
conn
|
||||
|> stream_archive(export_queries(_site_id = 1), format: "CSVWithNames")
|
||||
|> Stream.into(File.stream!("export.zip"))
|
||||
|> Stream.run()
|
||||
end)
|
||||
|
||||
"""
|
||||
@spec stream_archive(DBConnection.t(), %{String.t() => Ecto.Query.t()}, [Ch.query_option()]) ::
|
||||
Enumerable.t()
|
||||
def stream_archive(conn, named_queries, opts \\ []) do
|
||||
entries =
|
||||
Enum.map(named_queries, fn {name, query} ->
|
||||
{sql, params} = Plausible.ClickhouseRepo.to_sql(:all, query)
|
||||
|
||||
datastream =
|
||||
conn
|
||||
|> Ch.stream(sql, params, opts)
|
||||
|> Stream.map(fn %Ch.Result{data: data} -> data end)
|
||||
|
||||
Zstream.entry(name, datastream, coder: Zstream.Coder.Stored)
|
||||
end)
|
||||
|
||||
Zstream.zip(entries)
|
||||
end
|
||||
end
|
||||
@ -1,152 +0,0 @@
|
||||
defmodule Plausible.Google.GA4.API do
|
||||
@moduledoc """
|
||||
API for Google Analytics 4.
|
||||
"""
|
||||
|
||||
alias Plausible.Google
|
||||
alias Plausible.Google.GA4
|
||||
|
||||
require Logger
|
||||
|
||||
@type import_auth :: {
|
||||
access_token :: String.t(),
|
||||
refresh_token :: String.t(),
|
||||
expires_at :: String.t()
|
||||
}
|
||||
|
||||
@per_page 50_000
|
||||
@backoff_factor :timer.seconds(10)
|
||||
@max_attempts 5
|
||||
|
||||
def list_properties(access_token) do
|
||||
case GA4.HTTP.list_accounts_for_user(access_token) do
|
||||
{:ok, %{"accountSummaries" => accounts}} ->
|
||||
accounts =
|
||||
accounts
|
||||
|> Enum.filter(& &1["propertySummaries"])
|
||||
|> Enum.map(fn account ->
|
||||
%{"account" => account_id, "displayName" => account_name} = account
|
||||
|
||||
{"#{account_name} (#{account_id})",
|
||||
Enum.map(account["propertySummaries"], fn property ->
|
||||
%{"displayName" => property_name, "property" => property_id} = property
|
||||
|
||||
{"#{property_name} (#{property_id})", property_id}
|
||||
end)}
|
||||
end)
|
||||
|
||||
{:ok, accounts}
|
||||
|
||||
{:error, cause} ->
|
||||
{:error, cause}
|
||||
end
|
||||
end
|
||||
|
||||
def get_property(access_token, lookup_property) do
|
||||
case GA4.HTTP.get_property(access_token, lookup_property) do
|
||||
{:ok, property} ->
|
||||
%{"displayName" => property_name, "name" => property_id, "account" => account_id} =
|
||||
property
|
||||
|
||||
{:ok,
|
||||
%{
|
||||
id: property_id,
|
||||
name: "#{property_name} (#{property_id})",
|
||||
account_id: account_id
|
||||
}}
|
||||
|
||||
{:error, cause} ->
|
||||
{:error, cause}
|
||||
end
|
||||
end
|
||||
|
||||
def get_analytics_start_date(access_token, property) do
|
||||
GA4.HTTP.get_analytics_start_date(access_token, property)
|
||||
end
|
||||
|
||||
def get_analytics_end_date(access_token, property) do
|
||||
GA4.HTTP.get_analytics_end_date(access_token, property)
|
||||
end
|
||||
|
||||
def import_analytics(date_range, property, auth, persist_fn) do
|
||||
Logger.debug(
|
||||
"[#{inspect(__MODULE__)}:#{property}] Starting import from #{date_range.first} to #{date_range.last}"
|
||||
)
|
||||
|
||||
with {:ok, access_token} <- Google.API.maybe_refresh_token(auth) do
|
||||
do_import_analytics(date_range, property, access_token, persist_fn)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_import_analytics(date_range, property, access_token, persist_fn) do
|
||||
Enum.reduce_while(GA4.ReportRequest.full_report(), :ok, fn report_request, :ok ->
|
||||
Logger.debug(
|
||||
"[#{inspect(__MODULE__)}:#{property}] Starting to import #{report_request.dataset}"
|
||||
)
|
||||
|
||||
report_request = prepare_request(report_request, date_range, property, access_token)
|
||||
|
||||
case fetch_and_persist(report_request, persist_fn: persist_fn) do
|
||||
:ok -> {:cont, :ok}
|
||||
{:error, _} = error -> {:halt, error}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@spec fetch_and_persist(GA4.ReportRequest.t(), Keyword.t()) ::
|
||||
:ok | {:error, term()}
|
||||
def fetch_and_persist(%GA4.ReportRequest{} = report_request, opts \\ []) do
|
||||
persist_fn = Keyword.fetch!(opts, :persist_fn)
|
||||
attempt = Keyword.get(opts, :attempt, 1)
|
||||
sleep_time = Keyword.get(opts, :sleep_time, @backoff_factor)
|
||||
|
||||
case GA4.HTTP.get_report(report_request) do
|
||||
{:ok, {rows, row_count}} ->
|
||||
Logger.debug(
|
||||
"[#{inspect(__MODULE__)}:#{report_request.property}] Fetched #{length(rows)} rows of total #{row_count} with offset #{report_request.offset} for #{report_request.dataset}"
|
||||
)
|
||||
|
||||
:ok = persist_fn.(report_request.dataset, rows)
|
||||
|
||||
Logger.debug(
|
||||
"[#{inspect(__MODULE__)}:#{report_request.property}] Persisted #{length(rows)} for #{report_request.dataset}"
|
||||
)
|
||||
|
||||
if report_request.offset + @per_page < row_count do
|
||||
fetch_and_persist(
|
||||
%GA4.ReportRequest{report_request | offset: report_request.offset + @per_page},
|
||||
opts
|
||||
)
|
||||
else
|
||||
:ok
|
||||
end
|
||||
|
||||
{:error, cause} ->
|
||||
if attempt >= @max_attempts do
|
||||
Logger.debug(
|
||||
"[#{inspect(__MODULE__)}:#{report_request.property}] Request failed for #{report_request.dataset}. Terminating."
|
||||
)
|
||||
|
||||
{:error, cause}
|
||||
else
|
||||
Logger.debug(
|
||||
"[#{inspect(__MODULE__)}:#{report_request.property}] Request failed for #{report_request.dataset}. Will retry."
|
||||
)
|
||||
|
||||
Process.sleep(attempt * sleep_time)
|
||||
fetch_and_persist(report_request, Keyword.merge(opts, attempt: attempt + 1))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp prepare_request(report_request, date_range, property, access_token) do
|
||||
%GA4.ReportRequest{
|
||||
report_request
|
||||
| date_range: date_range,
|
||||
property: property,
|
||||
access_token: access_token,
|
||||
offset: 0,
|
||||
limit: @per_page
|
||||
}
|
||||
end
|
||||
end
|
||||
@ -1,244 +0,0 @@
|
||||
defmodule Plausible.Google.GA4.HTTP do
|
||||
@moduledoc """
|
||||
HTTP client implementation for Google Analytics 4 API.
|
||||
"""
|
||||
|
||||
alias Plausible.HTTPClient
|
||||
|
||||
require Logger
|
||||
|
||||
@spec get_report(Plausible.Google.GA4.ReportRequest.t()) ::
|
||||
{:ok, {[map()], non_neg_integer()}} | {:error, any()}
|
||||
def get_report(%Plausible.Google.GA4.ReportRequest{} = report_request) do
|
||||
params = %{
|
||||
requests: [
|
||||
%{
|
||||
property: report_request.property,
|
||||
dateRanges: [
|
||||
%{
|
||||
startDate: report_request.date_range.first,
|
||||
endDate: report_request.date_range.last
|
||||
}
|
||||
],
|
||||
dimensions: Enum.map(report_request.dimensions, &%{name: &1}),
|
||||
metrics: Enum.map(report_request.metrics, &build_metric/1),
|
||||
orderBys: [
|
||||
%{
|
||||
dimension: %{
|
||||
dimensionName: "date",
|
||||
orderType: "ALPHANUMERIC"
|
||||
},
|
||||
desc: true
|
||||
}
|
||||
],
|
||||
limit: report_request.limit,
|
||||
offset: report_request.offset
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
url =
|
||||
"#{reporting_api_url()}/v1beta/#{report_request.property}:batchRunReports"
|
||||
|
||||
response =
|
||||
HTTPClient.impl().post(
|
||||
url,
|
||||
[{"Authorization", "Bearer #{report_request.access_token}"}],
|
||||
params,
|
||||
receive_timeout: 60_000
|
||||
)
|
||||
|
||||
with {:ok, %{body: body}} <- response,
|
||||
{:ok, report} <- parse_report_from_response(body),
|
||||
row_count <- Map.fetch!(report, "rowCount"),
|
||||
{:ok, report} <- convert_to_maps(report) do
|
||||
{:ok, {report, row_count}}
|
||||
else
|
||||
{:error, %{reason: %{status: status, body: body}}} ->
|
||||
Logger.debug(
|
||||
"[#{inspect(__MODULE__)}:#{report_request.property}] Request failed for #{report_request.dataset} with code #{status}: #{inspect(body)}"
|
||||
)
|
||||
|
||||
Sentry.Context.set_extra_context(%{ga_response: %{body: body, status: status}})
|
||||
{:error, :request_failed}
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.debug(
|
||||
"[#{inspect(__MODULE__)}:#{report_request.property}] Request failed for #{report_request.dataset}: #{inspect(reason)}"
|
||||
)
|
||||
|
||||
{:error, :request_failed}
|
||||
end
|
||||
end
|
||||
|
||||
defp build_metric(expression) do
|
||||
case String.split(expression, " = ") do
|
||||
[name, expression] ->
|
||||
%{
|
||||
name: name,
|
||||
expression: expression
|
||||
}
|
||||
|
||||
[name] ->
|
||||
%{name: name}
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_report_from_response(%{"reports" => [report | _]}) do
|
||||
{:ok, report}
|
||||
end
|
||||
|
||||
defp parse_report_from_response(body) do
|
||||
Sentry.Context.set_extra_context(%{google_analytics4_response: body})
|
||||
|
||||
Logger.error(
|
||||
"Google Analytics 4: Failed to find report in response. Reason: #{inspect(body)}"
|
||||
)
|
||||
|
||||
{:error, {:invalid_response, body}}
|
||||
end
|
||||
|
||||
defp convert_to_maps(%{
|
||||
"rows" => rows,
|
||||
"dimensionHeaders" => dimension_headers,
|
||||
"metricHeaders" => metric_headers
|
||||
})
|
||||
when is_list(rows) do
|
||||
dimension_headers = Enum.map(dimension_headers, & &1["name"])
|
||||
metric_headers = Enum.map(metric_headers, & &1["name"])
|
||||
|
||||
report =
|
||||
Enum.map(rows, fn %{"dimensionValues" => dimensions, "metricValues" => metrics} ->
|
||||
dimension_values = Enum.map(dimensions, & &1["value"])
|
||||
metric_values = Enum.map(metrics, & &1["value"])
|
||||
metrics = Enum.zip(metric_headers, metric_values)
|
||||
dimensions = Enum.zip(dimension_headers, dimension_values)
|
||||
%{metrics: Map.new(metrics), dimensions: Map.new(dimensions)}
|
||||
end)
|
||||
|
||||
{:ok, report}
|
||||
end
|
||||
|
||||
defp convert_to_maps(response) do
|
||||
Logger.error(
|
||||
"Google Analytics 4: Failed to read report in response. Reason: #{inspect(response)}"
|
||||
)
|
||||
|
||||
Sentry.Context.set_extra_context(%{google_analytics4_response: response})
|
||||
{:error, {:invalid_response, response}}
|
||||
end
|
||||
|
||||
def list_accounts_for_user(access_token) do
|
||||
url = "#{admin_api_url()}/v1beta/accountSummaries?pageSize=200"
|
||||
|
||||
headers = [{"Authorization", "Bearer #{access_token}"}]
|
||||
|
||||
case HTTPClient.impl().get(url, headers) do
|
||||
{:ok, %Finch.Response{body: body, status: 200}} ->
|
||||
{:ok, body}
|
||||
|
||||
{:error, %HTTPClient.Non200Error{} = error} when error.reason.status in [401, 403] ->
|
||||
{:error, :authentication_failed}
|
||||
|
||||
{:error, %{reason: :timeout}} ->
|
||||
{:error, :timeout}
|
||||
|
||||
{:error, error} ->
|
||||
Sentry.capture_message("Error listing GA4 accounts for user", extra: %{error: error})
|
||||
{:error, :unknown}
|
||||
end
|
||||
end
|
||||
|
||||
def get_property(access_token, property) do
|
||||
url = "#{admin_api_url()}/v1beta/#{property}"
|
||||
|
||||
headers = [{"Authorization", "Bearer #{access_token}"}]
|
||||
|
||||
case HTTPClient.impl().get(url, headers) do
|
||||
{:ok, %Finch.Response{body: body, status: 200}} ->
|
||||
{:ok, body}
|
||||
|
||||
{:error, %HTTPClient.Non200Error{} = error} when error.reason.status in [401, 403] ->
|
||||
{:error, :authentication_failed}
|
||||
|
||||
{:error, %HTTPClient.Non200Error{} = error} when error.reason.status in [404] ->
|
||||
{:error, :not_found}
|
||||
|
||||
{:error, %{reason: :timeout}} ->
|
||||
{:error, :timeout}
|
||||
|
||||
{:error, error} ->
|
||||
Sentry.capture_message("Error retrieving GA4 property #{property}",
|
||||
extra: %{error: error}
|
||||
)
|
||||
|
||||
{:error, :unknown}
|
||||
end
|
||||
end
|
||||
|
||||
@earliest_valid_date "2015-08-14"
|
||||
|
||||
def get_analytics_start_date(access_token, property) do
|
||||
get_analytics_boundary_date(access_token, property, :start)
|
||||
end
|
||||
|
||||
def get_analytics_end_date(access_token, property) do
|
||||
get_analytics_boundary_date(access_token, property, :end)
|
||||
end
|
||||
|
||||
defp get_analytics_boundary_date(access_token, property, edge) do
|
||||
descending? = edge == :end
|
||||
|
||||
params = %{
|
||||
requests: [
|
||||
%{
|
||||
property: "#{property}",
|
||||
dateRanges: [
|
||||
%{startDate: @earliest_valid_date, endDate: Date.to_iso8601(Timex.today())}
|
||||
],
|
||||
dimensions: [%{name: "date"}],
|
||||
metrics: [%{name: "screenPageViews"}],
|
||||
orderBys: [
|
||||
%{dimension: %{dimensionName: "date", orderType: "ALPHANUMERIC"}, desc: descending?}
|
||||
],
|
||||
limit: 1
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
url = "#{reporting_api_url()}/v1beta/#{property}:batchRunReports"
|
||||
headers = [{"Authorization", "Bearer #{access_token}"}]
|
||||
|
||||
case HTTPClient.impl().post(url, headers, params) do
|
||||
{:ok, %Finch.Response{body: body, status: 200}} ->
|
||||
report = List.first(body["reports"])
|
||||
|
||||
date =
|
||||
case report["rows"] do
|
||||
[%{"dimensionValues" => [%{"value" => date_str}]}] ->
|
||||
Timex.parse!(date_str, "%Y%m%d", :strftime) |> NaiveDateTime.to_date()
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
|
||||
{:ok, date}
|
||||
|
||||
{:error, %HTTPClient.Non200Error{} = error} when error.reason.status in [401, 403] ->
|
||||
{:error, :authentication_failed}
|
||||
|
||||
{:error, %{reason: :timeout}} ->
|
||||
{:error, :timeout}
|
||||
|
||||
{:error, error} ->
|
||||
Sentry.capture_message("Error retrieving GA4 #{edge} date",
|
||||
extra: %{error: error}
|
||||
)
|
||||
|
||||
{:error, :unknown}
|
||||
end
|
||||
end
|
||||
|
||||
defp reporting_api_url, do: "https://analyticsdata.googleapis.com"
|
||||
defp admin_api_url, do: "https://analyticsadmin.googleapis.com"
|
||||
end
|
||||
@ -1,135 +0,0 @@
|
||||
defmodule Plausible.Google.GA4.ReportRequest do
|
||||
@moduledoc """
|
||||
Report request struct for Google Analytics 4 API
|
||||
"""
|
||||
|
||||
defstruct [
|
||||
:dataset,
|
||||
:dimensions,
|
||||
:metrics,
|
||||
:date_range,
|
||||
:property,
|
||||
:access_token,
|
||||
:offset,
|
||||
:limit
|
||||
]
|
||||
|
||||
@type t() :: %__MODULE__{
|
||||
dataset: String.t(),
|
||||
dimensions: [String.t()],
|
||||
metrics: [String.t()],
|
||||
date_range: Date.Range.t(),
|
||||
property: term(),
|
||||
access_token: String.t(),
|
||||
offset: non_neg_integer(),
|
||||
limit: non_neg_integer()
|
||||
}
|
||||
|
||||
def full_report do
|
||||
[
|
||||
%__MODULE__{
|
||||
dataset: "imported_visitors",
|
||||
dimensions: ["date"],
|
||||
metrics: [
|
||||
"totalUsers",
|
||||
"screenPageViews",
|
||||
"bounces = sessions - engagedSessions",
|
||||
"sessions",
|
||||
"userEngagementDuration"
|
||||
]
|
||||
},
|
||||
%__MODULE__{
|
||||
dataset: "imported_sources",
|
||||
dimensions: [
|
||||
"date",
|
||||
"sessionSource",
|
||||
"sessionMedium",
|
||||
"sessionCampaignName",
|
||||
"sessionManualAdContent",
|
||||
"sessionGoogleAdsKeyword",
|
||||
"pageReferrer"
|
||||
],
|
||||
metrics: [
|
||||
"screenPageViews",
|
||||
"totalUsers",
|
||||
"sessions",
|
||||
"bounces = sessions - engagedSessions",
|
||||
"userEngagementDuration"
|
||||
]
|
||||
},
|
||||
%__MODULE__{
|
||||
dataset: "imported_pages",
|
||||
dimensions: ["date", "hostName", "pagePath"],
|
||||
# NOTE: no exits as GA4 DATA API does not provide that metric
|
||||
metrics: ["totalUsers", "screenPageViews", "sessions", "userEngagementDuration"]
|
||||
},
|
||||
%__MODULE__{
|
||||
dataset: "imported_entry_pages",
|
||||
dimensions: ["date", "landingPage"],
|
||||
metrics: [
|
||||
"screenPageViews",
|
||||
"totalUsers",
|
||||
"sessions",
|
||||
"userEngagementDuration",
|
||||
"bounces = sessions - engagedSessions"
|
||||
]
|
||||
},
|
||||
# NOTE: Skipping for now as there's no dimension directly mapping to exit page path
|
||||
# %__MODULE__{
|
||||
# dataset: "imported_exit_pages",
|
||||
# dimensions: ["date", "ga:exitPagePath"],
|
||||
# metrics: [
|
||||
# "totalUsers",
|
||||
# "sessions",
|
||||
# "screenPageViews",
|
||||
# "userEngagementDuration",
|
||||
# "bounces = sessions - engagedSessions"
|
||||
# ]
|
||||
# },
|
||||
%__MODULE__{
|
||||
dataset: "imported_locations",
|
||||
dimensions: ["date", "countryId", "region", "city"],
|
||||
metrics: [
|
||||
"screenPageViews",
|
||||
"totalUsers",
|
||||
"sessions",
|
||||
"bounces = sessions - engagedSessions",
|
||||
"userEngagementDuration"
|
||||
]
|
||||
},
|
||||
%__MODULE__{
|
||||
dataset: "imported_devices",
|
||||
dimensions: ["date", "deviceCategory"],
|
||||
metrics: [
|
||||
"screenPageViews",
|
||||
"totalUsers",
|
||||
"sessions",
|
||||
"bounces = sessions - engagedSessions",
|
||||
"userEngagementDuration"
|
||||
]
|
||||
},
|
||||
%__MODULE__{
|
||||
dataset: "imported_browsers",
|
||||
dimensions: ["date", "browser"],
|
||||
metrics: [
|
||||
"screenPageViews",
|
||||
"totalUsers",
|
||||
"sessions",
|
||||
"bounces = sessions - engagedSessions",
|
||||
"userEngagementDuration"
|
||||
]
|
||||
},
|
||||
%__MODULE__{
|
||||
dataset: "imported_operating_systems",
|
||||
dimensions: ["date", "operatingSystem", "operatingSystemVersion"],
|
||||
metrics: [
|
||||
"screenPageViews",
|
||||
"totalUsers",
|
||||
"sessions",
|
||||
"bounces = sessions - engagedSessions",
|
||||
"userEngagementDuration"
|
||||
]
|
||||
}
|
||||
]
|
||||
end
|
||||
end
|
||||
@ -1,8 +1,4 @@
|
||||
defmodule Plausible.Google.UA.ReportRequest do
|
||||
@moduledoc """
|
||||
Report request struct for Universal Analytics API
|
||||
"""
|
||||
|
||||
defmodule Plausible.Google.ReportRequest do
|
||||
defstruct [
|
||||
:dataset,
|
||||
:dimensions,
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue