Backends
Drupal
Drupal 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 Drupal, with live visitors, goals and revenue attribution in one script.
Add the script
Set the site ID in production
Copy the ID from Settings in the dashboard and add it to the production site's
settings.php(or thesettings.local.phponly production loads):$settings['stickybeak_site_id'] = 'YOUR_SITE_ID';Create the module
name: Stickybeak type: module description: Adds the Stickybeak analytics tag. core_version_requirement: ^10 || ^11<?php use Drupal\Core\Render\Markup; use Drupal\Core\Site\Settings; /** * Implements hook_page_attachments(). */ function stickybeak_page_attachments(array &$attachments) { // Leave admin pages out. if (\Drupal::service('router.admin_context')->isAdminRoute()) { return; } // Queues goal calls until the script loads; harmless without it. $attachments['#attached']['html_head'][] = [[ '#type' => 'html_tag', '#tag' => 'script', '#value' => Markup::create('window.stickybeak = window.stickybeak || function () { (window.stickybeak.q = window.stickybeak.q || []).push(arguments); };'), ], 'stickybeak_queue']; $site_id = Settings::get('stickybeak_site_id'); if (!$site_id) { return; } $attachments['#attached']['html_head'][] = [[ '#type' => 'html_tag', '#tag' => 'script', '#attributes' => [ 'defer' => TRUE, 'data-site' => $site_id, 'data-domain' => 'example.com', 'src' => 'https://app.usestickybeak.com/_cfw/s.js', ], ], 'stickybeak']; }Enable it
drush en stickybeak && drush cr
Option 2: a theme library
If you have your own theme and always want the tag, declare it as a library:
stickybeak:
header: true
js:
https://app.usestickybeak.com/_cfw/s.js:
type: external
attributes:
defer: true
data-site: YOUR_SITE_ID
data-domain: example.com
and attach it to every page from the theme's info file, then clear caches:
libraries:
- mytheme/stickybeak
Check it's working
Check the live site while logged out.
See Verify your install.
Track goals
- Clicks: add a
data-goalattribute to links and buttons in your Twig templates. See Custom goals. - Webform: set the form's confirmation to Page or URL under Settings → Confirmation, then add a goal for a page visit to that path under Manage goals in Stickybeak.
Track purchases
Fire a purchase on the checkout's completion step. Add this to the module's stickybeak_page_attachments(), before the $site_id check:
$route = \Drupal::routeMatch();
if ($route->getRouteName() === 'commerce_checkout.form' && $route->getParameter('step') === 'complete') {
$order = $route->getParameter('commerce_order');
$total = $order->getTotalPrice();
$props = [
'revenue' => $total->getNumber(),
'currency' => $total->getCurrencyCode(),
'order_id' => (string) $order->getOrderNumber(),
];
$flags = JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT;
$attachments['#attached']['html_head'][] = [[
'#type' => 'html_tag',
'#tag' => 'script',
'#value' => Markup::create('window.stickybeak("purchase", ' . json_encode($props, $flags) . ');'),
], 'stickybeak_purchase'];
$attachments['#cache']['max-age'] = 0;
}
See Revenue.
If it isn't working
- Clear Drupal caches after module, setting or theme-library changes.
See Troubleshooting.