Analytics Integration

Integrate analytics to measure activation, retention, and quality.

PostHog example

<script>
  !(function (t, e) {
    var o, n, p, r;
    e.__SV ||
      ((window.posthog = e),
      (e._i = []),
      (e.init = function (i, s, a) {
        function g(t, e) {
          var o = e.split(".");
          2 == o.length && ((t = t[o[0]]), (e = o[1])),
            (t[e] = function () {
              t.push([e].concat(Array.prototype.slice.call(arguments, 0)));
            });
        }
        ((p = t.createElement("script")).type = "text/javascript"),
          (p.async = !0),
          (p.src = s.api_host + "/static/array.js"),
          (r = t.getElementsByTagName("script")[0]).parentNode.insertBefore(
            p,
            r
          );
        var u = e;
        for (
          void 0 !== a ? (u = e[a] = []) : (a = "posthog"),
            u.people = u.people || [],
            u.toString = function (t) {
              var e = "posthog";
              return (
                "posthog" !== a && (e += "." + a), t || (e += " (stub)"), e
              );
            },
            u.people.toString = function () {
              return u.toString(1) + ".people (stub)";
            },
            o =
              "capture identify alias people.set people.set_once reset group.set register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing debug set_config get_property".split(
                " "
              ),
            n = 0;
          n < o.length;
          n++
        )
          g(u, o[n]);
        e._i.push([i, s, a]);
      }),
      (e.__SV = 1));
  })(document, window.posthog || []);
  posthog.init("ph_project_key", { api_host: "https://app.posthog.com" });
</script>
posthog.capture("MINI_APP_OPEN", { source: "featured" });

Segment example

<script>
  !(function () {
    var analytics = (window.analytics = window.analytics || []);
    if (!analytics.initialize)
      if (analytics.invoked)
        window.console &&
          console.error &&
          console.error("Segment snippet included twice.");
      else {
        analytics.invoked = !0;
        (analytics.methods = [
          "trackSubmit",
          "trackClick",
          "trackLink",
          "trackForm",
          "pageview",
          "identify",
          "reset",
          "group",
          "track",
          "ready",
          "alias",
          "debug",
          "page",
          "once",
          "off",
          "on",
          "addSourceMiddleware",
          "addIntegrationMiddleware",
          "setAnonymousId",
          "addDestinationMiddleware",
        ]),
          (analytics.factory = function (e) {
            return function () {
              var t = Array.prototype.slice.call(arguments);
              t.unshift(e);
              analytics.push(t);
              return analytics;
            };
          }),
          analytics.methods.forEach(function (e) {
            analytics[e] = analytics.factory(e);
          }),
          (analytics.load = function (e, t) {
            var n = document.createElement("script");
            n.type = "text/javascript";
            n.async = !0;
            n.src =
              ("https:" === document.location.protocol
                ? "https://"
                : "http://") +
              "cdn.segment.com/analytics.js/v1/" +
              e +
              "/analytics.min.js";
            var a = document.getElementsByTagName("script")[0];
            a.parentNode.insertBefore(n, a);
            analytics._loadOptions = t;
          }),
          (analytics._writeKey = "SEGMENT_WRITE_KEY"),
          (analytics.SNIPPET_VERSION = "4.13.2"),
          analytics.load("SEGMENT_WRITE_KEY");
      }
  })();
</script>
analytics.track("MINI_APP_OPEN", { source: "featured" });

Best practices

  • Use the Event Schema names
  • Include surface, latency_ms, and error_code where relevant
  • Batch and retry on flaky networks