Skip to content

Commit

Permalink
Add site analytics example
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffail committed Jan 26, 2022
1 parent d0baa7f commit 060e57c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
31 changes: 31 additions & 0 deletions config/examples/site_analytics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
input:
http_server:
address: 0.0.0.0:4196
path: /poke
allowed_verbs: [ POST, HEAD ]
cors:
enabled: true
allowed_origins:
- '*'
processors:
- metric:
type: counter
name: site_visit
labels:
host: ${! meta("h") }
path: ${! meta("p") }
referrer: ${! meta("r") }
- bloblang: 'root = deleted()'

metrics:
prometheus:
prefix: ""
path_mapping: |
# Undo wonky '_' to '__' replace that's done automatically.
# TODO: V4 this won't be necessary
let path = this.replace("__", "_")
# Only emit our custom metric, and no internal Benthos metrics.
root = if ![
"site_visit",
].contains($path) { deleted() } else { $path }
2 changes: 1 addition & 1 deletion lib/metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func NewPrometheus(config Config, opts ...func(Type)) (Type, error) {
opt(p)
}

// TODO: Maybe disable this with a config flag.
// TODO: V4 Maybe disable this with a config flag.
if err := p.reg.Register(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})); err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ module.exports = {
],
],
plugins: [
path.resolve(__dirname, './src/plugins/analytics'),
path.resolve(__dirname, './src/plugins/cookbooks'),
path.resolve(__dirname, './src/plugins/redirects'),
],
Expand Down
16 changes: 16 additions & 0 deletions website/src/plugins/analytics/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const host = window.location.hostname;

function poke(path) {
// TODO: Allow this to be configured
fetch(`https://poke.benthos.dev/poke?h=${encodeURIComponent(host)}&p=${encodeURIComponent(path)}`, { method: "POST" })
.catch((error) => console.error(error))
}

module.exports = (() => {
poke(window.location.pathname);
return {
onRouteUpdate({location}) {
poke(location.pathname);
},
};
})();
12 changes: 12 additions & 0 deletions website/src/plugins/analytics/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const path = require('path');

const isProd = process.env.NODE_ENV === 'production';

module.exports = function(context, options) {
return {
name: 'blob-analytics',
getClientModules() {
return isProd ? [path.resolve(__dirname, './client')] : [];
},
};
};

0 comments on commit 060e57c

Please sign in to comment.