Stickybeak Docs Dashboard Get early access

Backends

Spring Boot

Spring Boot 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 Spring Boot, with live visitors, goals and revenue attribution in one script.

Add the script

  1. Add a property for production

    Copy the ID from Settings in the dashboard, set it as STICKYBEAK_SITE_ID on your production host, and read it in the prod profile only:

    stickybeak.site-id=${STICKYBEAK_SITE_ID}

    Run production with SPRING_PROFILES_ACTIVE=prod. Other profiles leave the property empty.

  2. Share it with every view

    @ControllerAdvice
    public class StickybeakAdvice {
        @Value("${stickybeak.site-id:}")
        private String siteId;
    
        @ModelAttribute("stickybeakSite")
        public String stickybeakSite() {
            return siteId;
        }
    }
  3. Write the fragment

    <th:block th:fragment="head">
      <script>window.stickybeak = window.stickybeak || function () { (window.stickybeak.q = window.stickybeak.q || []).push(arguments); };</script>
      <script
        th:unless="${#strings.isEmpty(stickybeakSite)}"
        defer
        th:data-site="${stickybeakSite}"
        data-domain="example.com"
        src="https://app.usestickybeak.com/_cfw/s.js"
      ></script>
      <script th:if="${stickybeakGoal != null}" th:inline="javascript">
        window.stickybeak(/*[[${stickybeakGoal.name}]]*/ "goal", /*[[${stickybeakGoal.props}]]*/ {});
      </script>
    </th:block>
  4. Include it in your layout

    Before </head> in the template every page uses:

    <th:block th:replace="~{fragments/stickybeak :: head}"></th:block>

Check it's working

Publish first, then follow Verify your install.

Track goals

Give any button or link a data-goal attribute. See Custom goals.

<a th:href="@{/pricing}" data-goal="view_pricing">Pricing</a>

Fire a goal after a redirect

Add the goal as a flash attribute. Spring puts it in the model for the next request only.

@PostMapping("/signup")
public String signup(@Valid SignupForm form, RedirectAttributes redirect) {
    User user = accounts.register(form);
    redirect.addFlashAttribute("stickybeakGoal",
        Map.of("name", "signup", "props", Map.of("plan", user.getPlan())));
    return "redirect:/welcome";
}

The fragment's last script renders it.

Track purchases

redirect.addFlashAttribute("stickybeakGoal", Map.of("name", "purchase", "props", Map.of(
    "revenue", order.getTotal().toPlainString(),
    "currency", order.getCurrency(),
    "order_id", order.getNumber())));

See Revenue.

If it isn't working

  • Confirm the prod profile is active and STICKYBEAK_SITE_ID is set.

See Troubleshooting.