Backends
Nginx and Apache
Nginx analytics with Stickybeak starts by adding its script to your rendered pages; this guide shows you where it belongs and how to track page views, goals and purchase revenue. It is a lighter alternative to Google Analytics for Nginx, with live visitors, goals and revenue attribution in one script.
Add the script
This uses sub_filter from ngx_http_sub_module. It's included in the nginx.org packages and most distribution builds; nginx -V 2>&1 | grep http_sub_module tells you for sure.
server {
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
# sub_filter can't edit compressed responses, so ask the app for plain ones.
proxy_set_header Accept-Encoding "";
sub_filter '</head>' '<script defer data-site="YOUR_SITE_ID" data-domain="example.com" src="https://app.usestickybeak.com/_cfw/s.js"></script></head>';
sub_filter_once on;
}
}
sudo nginx -t && sudo systemctl reload nginx
Serving static files straight from disk?
Apache
This uses mod_substitute. Enable it (and mod_headers if Apache is proxying):
sudo a2enmod substitute headers && sudo systemctl reload apache2
Then add this to the site's virtual host:
<Location "/">
# Only if Apache proxies to an app: ask it for uncompressed HTML.
RequestHeader unset Accept-Encoding
AddOutputFilterByType SUBSTITUTE text/html
Substitute 's|</head>|<script defer data-site="YOUR_SITE_ID" data-domain="example.com" src="https://app.usestickybeak.com/_cfw/s.js"></script></head>|i'
</Location>
Caddy
Caddy can't rewrite response bodies out of the box.
Check it's working
Reload the server, then run curl -s https://example.com/ | grep _cfw.
See Verify your install.
Track goals
- Production only. Put the rule in the production server block or virtual host, not a shared include, so staging servers stay out of your numbers.
- Goals need markup. Click goals are a
data-goalattribute on the element, which is awkward to add with a find-and-replace.
See Custom goals.
Track purchases
Render window.stickybeak("purchase", { revenue, currency, order_id }) on the checkout confirmation page.
See Revenue.
If it isn't working
- Response rewriting cannot edit compressed upstream or pre-compressed HTML. Caddy needs the
replace-responseplugin.
See Troubleshooting.