Analytics for Clojure
Segment doesn't manage or update community libraries. These libraries are available on GitHub under the MIT License for the open-source community to fork or contribute.
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. You can check it out on GitHub. The clojure library is a wrapper around Segment’s Java library.
The Clojure library (like Segment’s other server side libraries) is built for high-performance, so you can use them 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
If you’re using Maven add this repository definition to your pom.xml
:
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
Then, if you’re using Leiningen:
[circleci/analytics-clj "0.8.0"]
or with Maven
<dependency>
<groupId>circleci</groupId>
<artifactId>analytics-clj</artifactId>
<version>0.8.0</version>
</dependency>
You only need to initialize once at the start of your program. You can then keep
using the Analytics
singleton anywhere in your code.
(use '[circleci.analytics-clj.core])
(def analytics (initialize "<writeKey>"))
The default initialization settings are production-ready.
Regional configuration
For Business plans with access to Regional Segment, you can use the host
configuration parameter to send data to the desired region:
- Oregon (Default) —
api.segment.io/
- Dublin —
events.eu1.segmentapis.com/
Identify
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 are change.
Example Identify call:
(identify analytics "user-id" {:email "bob@acme.com"})
This call is identifying the user by his unique User ID (the one you know him by in your database) and labeling him with an email
trait.
The Identify call has the following fields:
user-id String |
The ID for this user in your database. |
traits Map, optional |
A 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
Track calls let you record the actions your users perform. Every action triggers what Segment calls 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"})
(track analytics (:id user) "signup" {:company "Acme Inc."} {:context {:language "en-us"}
:integrations {"AdRoll" false}
: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, plan type.
The Track call has the following fields:
user-id String |
The ID for this user in your database. |
event String |
The name of the event you’re tracking. Segment recommends human-readable names like Song Played or Status Updated. |
properties Map, optional |
A 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 as well as the Track method payload in the Segment Spec.
Group
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:
userId String |
The ID for this user in your database. |
groupId String |
The ID for this group in your database. |
traits Traits, optional |
A 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.
Screen
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 an event whenever the 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:
userId String |
The ID for this user in your database. |
name String |
The webpage name you’re tracking. Segment recommends human-readable names like Login or Register. |
properties Properties, optional |
A dictionary of properties for the webpage visit. If the event was Login, it might have properties like path or title . |
Alias
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.
Builder
If the above methods don’t meet your needs, you can use the builder types directly.
(enqueue analytics (doto (YourMessageType/builder)
(.userId "user-id")
(.properties {"company" "Acme Inc."})))
Logging
You can set a custom logger on the client using:
(defn logger []
(reify com.segment.analytics.Log
(print [this level format args]
(println (str (java.util.Date.) "\t" level "\t" args)))
(print [this level error format args]
(println error))))
(def analytics (initialize "<writeKey>" {:log (logger)}))
Troubleshooting
The following tips often help resolve common issues.
No events in my debugger
-
Double check that you’ve set up the library correctly.
-
Make sure that you’re calling one of Segment’s API methods once the library is successfully installed, like Identify or Track.
Other common errors
If you are experiencing data loss from your Clojure 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/oranonymousId
. If you send events without either theuserId
oranonymousId
, Segment’s tracking API responds with anno_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 amessageId
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.
This page was last modified: 30 May 2024
Need support?
Questions? Problems? Need more info? Contact Segment Support for assistance!