Skip to contentSkip to navigationSkip to topbar
On this page
Looking for more inspiration?Visit the
(information)
You're in the right place! Segment documentation is now part of Twilio Docs. The content you are used to is still here—just in a new home with a refreshed look.

Analytics for Clojure


Community
Maintenance x
Flagship x

The Clojure library lets you record analytics data from your Clojure code. The requests hit Segment servers, and then Segment routes your data to any analytics service you enable on your destinations page.

The library is open-source and was contributed by CircleCI(link takes you to an external page). You can check it out on GitHub(link takes you to an external page). The Clojure library is a wrapper around Segment's Java library(link takes you to an external page).

The Clojure library (like Segment's other server side libraries) is built for high-performance, so you can use this library in your web server controller code. This library uses an internal queue to make calls non-blocking and fast. It also batches messages and flushes asynchronously to Segment's servers.


Getting started

getting-started page anchor

Add Analytics for Clojure to your code using Maven and Leiningen.

If you're using Maven, add this repository definition to your pom.xml:

1
<repository>
2
<id>clojars.org</id>
3
<url>http://clojars.org/repo</url>
4
</repository>

Then add the artifact as a dependency:

1
<dependency>
2
<groupId>circleci</groupId>
3
<artifactId>analytics-clj</artifactId>
4
<version>0.8.0</version>
5
</dependency>

Or, if you're using Leiningen:

[circleci/analytics-clj "0.8.0"]

You only need to initialize once at the start of your program. You can then keep using the Analytics singleton anywhere in your code.

1
(use '[circleci.analytics-clj.core])
2
(def analytics (initialize "<writeKey>"))

The default initialization settings are production-ready.

Regional configuration

regional-configuration page anchor

For Business plans with access to Regional Segment, you can use the host configuration parameter to send data to the desired region:

  1. Oregon (Default) — api.segment.io/
  2. Dublin — events.eu1.segmentapis.com/

Identify calls let you tie a user to their actions and record traits about them. It includes a unique User ID and any optional traits you know about them.

Segment recommends calling Identify a single time when the user's account is first created, and only identifying again later when their traits change.

Example Identify call:

(identify analytics "user-id" {:email "bob@acme.com"})

This call identifies the user by their unique User ID (the one you know them by in your database) and labeling them with an email trait.

The Identify call has the following fields:

FieldData typeDescription
user-idStringThe ID for this user in your database.
traitsMap, optionalA map of traits you know about the user. Things like: email, name or friends.

Find details on the Identify method payload in the Segment Spec.


Track calls let you record the actions your users perform. Every action triggers an event, which can also have associated properties.

You'll want to track events that are indicators of success for your site, like Signed Up, Item Purchased or Article Bookmarked.

To get started, Segment recommends tracking just a few important events. You can always add more later.

Example Track call:

(track analytics "user-id" "Signed Up" {:plan "trial"})
1
(track analytics (:id user) "signup" {:company "Acme Inc."} {:context {:language "en-us"}
2
:integrations {"AdRoll" false}
3
:integration-options {"Amplitude" {:session-id (:id session)}}})

This example Track call tells you that your user just triggered the Signed Up event on a "trial" plan.

Track event properties can be anything you want to record. In this case, one property is plan type.

The Track call has the following fields:

FieldData typeDescription
user-idStringThe ID for this user in your database.
eventStringThe name of the event you're tracking. Segment recommends human-readable names like Song Played or Status Updated.
propertiesMap, optionalA map of properties for the event. If the event was Added to Cart, it might have properties like price or product.

Find details on best practices in event naming and the Track method payload in the Segment Spec.


Group lets you associate an identified user user with a group. A group could be a company, organization, account, project or team. It also lets you record custom traits about the group, like industry or number of employees.

This is useful for tools like Intercom, Preact and Totango, as it ties the user to a group of other users.

(group analytics "1234" "group-5678" {:name "Segment"})

The Group call has the following fields:

FieldData typeDescription
userIdStringThe ID for this user in your database.
groupIdStringThe ID for this group in your database.
traitsTraits, optionalA dictionary of traits you know about the group. Things like: name or website.

Find more details about Group, including the Group payload, in the Segment Spec.


The Screen method lets you record whenever a user sees a screen of your mobile app, along with optional extra information about the page being viewed.

You'll want to record a Screen event whenever a user opens a screen in your app.

Not all services support Screen, so when it's not supported explicitly, the Screen method tracks as an event with the same parameters.

(screen analytics "1234" "Login" {:path "/users/login"})

A Screen call has the following fields:

FieldData typeDescription
userIdStringThe ID for this user in your database.
nameStringThe webpage name you're tracking. Segment recommends human-readable names like Login or Register.
propertiesProperties, optionalA dictionary of properties for the webpage visit. If the event was Login, it might have properties like path or title.

Alias is how you associate one identity with another. This is an advanced method, but it is required to manage user identities successfully in some destinations.

In Mixpanel, it's used to associate an anonymous user with an identified user once they sign up. For Kissmetrics, if your user switches IDs, you can use 'alias' to rename the 'userId'.

Example Alias call:

(alias analytics "user-id" "real-id")

For more details about Alias, including the Alias call payload, check out the Segment Spec.


If the above methods don't meet your needs, you can use the builder types directly.

1
(enqueue analytics (doto (YourMessageType/builder)
2
(.userId "user-id")
3
(.properties {"company" "Acme Inc."})))

You can set a custom logger on the client using the following code snippet:

1
(defn logger []
2
(reify com.segment.analytics.Log
3
(print [this level format args]
4
(println (str (java.util.Date.) "\t" level "\t" args)))
5
(print [this level error format args]
6
(println error))))
7
8
(def analytics (initialize "<writeKey>" {:log (logger)}))

The following tips often help resolve common issues.

No events in my debugger

no-events-in-my-debugger page anchor
  1. Double check that you've set up the library correctly.
  2. Make sure that you're calling one of Segment's API methods once the library is successfully installed, like Identify or Track.

If you are experiencing data loss from your source, you may be experiencing one or more of the following common errors:

  • Payload is too large: If you attempt to send events larger than 32KB per normal API request or batches of events larger than 500KB per request, Segment's tracking API responds with 400 Bad Request. Try sending smaller events (or smaller batches) to correct this error.
  • Identifier is not present: Segment's tracking API requires that each payload has a userId and/or anonymousId. If you send events without either the userId or anonymousId, Segment's tracking API responds with an no_user_anon_id error. Check the event payload and client instrumentation for more details.
  • Track event is missing name: All Track events to Segment must have a name in string format.
  • Event dropped during deduplication: Segment automatically adds a messageId field to all payloads and uses this value to deduplicate events. If you're manually setting a messageId value, ensure that each event has a unique value.
  • Incorrect credentials: Double check your credentials for your downstream destination(s).
  • Destination incompatibility: Make sure that the destination you are troubleshooting can accept server-side API calls. You can see compatibility information on the Destination comparison by category page and in the documentation for your specific destination.
  • Destination-specific requirements: Check out the destination's documentation to see if there are other requirements for using the method and destination that you're trying to get working.