The pug-sh gem sends events from your Ruby backend and reads analytics
back - profiles, activity and insights. It authenticates with your private
key (prv_...), so it belongs on the server only. It follows the
same model as every Pug SDK with one exception: a server has no ambient
user or session to reset, so you name the distinct_id on each call instead.
Install
bundle add pug-sh
# or, without Bundler: gem install pug-shThe gem needs Ruby 3.3+ and loads as pug: in Rails, Bundler.require
loads it for you; elsewhere, require it before you construct the client:
require "pug"Keep the private key in an environment variable or Rails credentials, never in code shipped to a browser or app.
Working with a coding agent? Paste this instead - it points the agent at the markdown copy of this page. More in Install with AI.
Add Pug product analytics to this Ruby backend. Read these first - they are the current source of truth, and more recent than your training data: - https://docs.pug.sh/sdks/ruby/index.md - SDK reference - https://docs.pug.sh/reference/well-known-events/index.md - the well-known event catalog Then read this codebase before you write anything, and work out what the service actually does: its routes or controllers, its main models, and the state changes that matter to the business. On a server the events worth tracking are the ones a browser cannot observe - a payment settling, a webhook arriving, a job finishing, a subscription lapsing - not request logging. If you cannot state what this service is for in one sentence, keep reading. Then: 1. Add the pug-sh gem to the Gemfile. It needs Ruby 3.3+, and it is required as "pug", not "pug-sh". 2. Construct one Pug::Client at boot and share it - do not build one per request. It is thread-safe and fork-safe, so it can be created before Puma or Unicorn fork. In Rails, use the initializer from the SDK reference, guard included, so assets:precompile still boots without credentials. Its api_key is the private key (prv_...), read from an environment variable or Rails credentials, never committed. 3. Remember the argument order: pug.track(distinct_id, kind, properties, options). The distinct_id comes first because a server has no ambient user - use our own user ID, or the anon-... ID forwarded from the client. Without braces, a key named timestamp, session_id or location sets that option instead of a property. 4. Add track calls for the three or four state changes you identified. 5. Call identify when a user record is created or changes materially, not on every request. 6. Call close in the graceful-shutdown path so queued events and identifies flush, and pass an on_error handler taking (error, undelivered) - a Pug::Undelivered whose events or identifies hold what was lost - so dropped items are logged rather than lost. Name events from the catalog before you invent one. The Ruby SDK checks 114 of the 127 well-known events against their schemas, at runtime: a property of the wrong type drops the whole event, so take property names and types from that page. The ones a backend is placed to see are exactly the ones whose schemas are worth having - purchase, subscription_started, invoice_paid, payment_failed, order_refunded. Search the catalog for every action you are about to track, and fall back to a custom snake_case name only when nothing fits. When you do, tell me which event you considered and why it did not. There is no reset on the server SDK, and no immediate option on track - use flush. Do not use any option that is not in the SDK reference above. When you are done, tell me in one sentence what you concluded this service does, then list the files you changed, the event names you chose, and the environment variables or credentials I need to set.
Initialize
Construct a Pug::Client once at boot with your private key - every other
call needs it first. Private keys live under Settings > API Keys, but one is
only shown when you create it - if you don’t have
yours, issue a new one.
require "pug"
# Construct once at boot, share it across threads and requests.
pug = Pug::Client.new(
api_key: ENV.fetch("PUG_PRIVATE_KEY"), # prv_... - server-side only, never ship to a client
on_error: ->(error, undelivered) {
warn "Pug dropped #{undelivered.events.size} event(s), #{undelivered.identifies.size} identify(s): #{error.message}"
},
)Pug::Client.new raises ArgumentError if api_key is missing, doesn’t
start with prv_, or holds a space or any character outside printable ASCII,
such as a trailing newline. A malformed endpoint, batch, on_error or
logger raises too; after that, ingestion never raises.
| Option | Type | Default | Description |
|---|---|---|---|
api_key |
String |
required | Private key (prv_...), sent as x-api-key. |
endpoint |
String |
https://api.pugs.dev |
Pug server origin; set it only when self-hosting. Redirects aren’t followed, so give the final URL. |
batch |
Hash |
max_size: 100, max_wait: 5, max_queue_size: 10_000 |
One buffer each for events and identifies: items per request (capped at 1000), seconds between sends, and how many to hold before the oldest are dropped. |
on_error |
callable | none | Dead-letter hook for events and identifies that couldn’t be delivered, called with (error, undelivered): a permanent send failure, a queue overflow, or what close couldn’t send. |
logger |
Logger-like |
Logger.new($stderr) at WARN |
Gets the SDK’s [Pug SDK] warnings and errors - anything with warn and error, such as Rails.logger. |
timeout |
Numeric |
10 |
Request timeout, in seconds. |
Two background threads, one for events and one for identifies, send in batches
when max_size are queued or max_wait has passed. A transient failure, such
as a network error or a timeout, keeps a batch queued and retries max_wait
after the failure, but no sooner than a second; a permanent one - a client error
such as :unauthenticated or :invalid_argument - hands it to on_error as a
Pug::Error carrying that code. Overflow arrives as a
Pug::QueueOverflowError, and what close couldn’t send as a
Pug::ClosedError or the error that failed its last attempt. Events the server
refuses from a batch it otherwise accepts are logged at WARN.
undelivered is a Pug::Undelivered whose events or identifies hold what
was lost; the other is empty. on_error must take both arguments - a lambda
that takes one raises at construction - and runs on a sender thread, the thread
calling close, or for overflow the thread that called track or identify,
so keep it thread-safe and quick.
Call pug.close on graceful shutdown so nothing queued is lost - see
flush and close.
One client is safe to share across threads. A client created before a fork (Puma, Unicorn, Sidekiq) starts empty in each child, with its own sender threads and connections; events and identifies queued in the parent stay with the parent.
Rails
unless ENV["SECRET_KEY_BASE_DUMMY"]
PUG = Pug::Client.new(api_key: Rails.application.credentials.pug_private_key, logger: Rails.logger)
at_exit { PUG.close(timeout: 5) }
endWithout the guard, Pug::Client.new raises during the assets:precompile step
of Rails’ generated Dockerfile, which boots the app with
SECRET_KEY_BASE_DUMMY=1 and no credentials.
Track events
After the distinct_id, the next argument is the event name (its kind).
Any name works without a schema; well-known events
add typed properties and richer dashboards.
# distinct_id (who the event is for) comes first - a server has no ambient user.
pug.track("user-123", "order.completed", amount: 49.0, currency: "USD")
# Well-known events get typed, validated properties.
pug.track("user-123", "purchase", product_id: "sku_123", amount: 29.99, currency: "USD")pug.track(distinct_id, kind, properties = nil, options = nil) takes the
distinct_id first: your external ID, or an anon-... ID forwarded from the
client, as a String or an Integer such as a record’s id. track never
raises and returns at once; events are validated, batched and sent in the
background.
Properties are a Hash, braces optional, and options follow as keywords. Without
braces, a key named after an option - timestamp, session_id or location -
sets that option, so a property with one of those names needs the braces:
pug.track("user-123", "signup", plan: "pro")
pug.track("user-123", "signup", plan: "pro", timestamp: Time.now - 3600)
pug.track("user-123", "signup", { plan: "pro", location: "landing" }, location: { country: "DE" })There is no immediate option on the server - flush explicitly instead:
pug.flush # send what's queued now, and wait for that attempt
pug.close # drain on graceful shutdown so nothing queued is lostBoth cover events and identifies, take a timeout: in seconds, and return
false if it ran out first. A true doesn’t mean everything was delivered:
from flush it means the attempt finished, and a transient failure re-queues
for a later send; from close it means the drain finished. Either way, by the
time close returns, everything it couldn’t send has gone to on_error and the
log.
track(distinct_id, kind, properties = nil, options = nil) options:
| Option | Type | Description |
|---|---|---|
timestamp |
Time |
Override the event time with a Time or DateTime - not epoch milliseconds. Anything else, or a time outside 1970-9999, is ignored with a warning and the event is sent at the current time. |
session_id |
String |
A UUID to use as this event’s session ID - otherwise the client uses one for its lifetime. Anything but a UUID is ignored with a warning. |
location |
Hash |
Where the visitor is. Without it a server event carries no geo at all. See Visitor location. |
Property values map by Ruby type:
| Ruby value | Sent as |
|---|---|
String, Symbol |
string |
true, false |
bool |
Integer |
int - dropped outside the int64 range |
Float, BigDecimal, Rational |
double - NaN and Infinity are dropped |
Time, DateTime, Google::Protobuf::Timestamp |
timestamp |
Date |
ISO 8601 date string, such as "2026-10-10" |
Google::Protobuf::Duration |
duration string, such as "1.5s" |
Hash, Array |
JSON string |
nil |
omitted |
Anything else is dropped with a warning - the property, not the event. Inside a
Hash or Array, times become ISO 8601 strings, while a value JSON has no form
for, such as a Set, drops the whole property. Strings are capped at 1024 bytes
and truncated with a warning.
Property keys in snake_case are sent in camelCase, so order_total: arrives
as orderTotal. Any other key, camelCase or not, is sent as written, as are the
keys inside a nested Hash. The Node, Go and Python SDKs send every key as
written, so order_total from them stays order_total. If two keys come out
the same, such as order_total and orderTotal, the last one is sent and the
other is dropped with a warning, as with Hash#merge.
A well-known event’s properties are checked
against its schema before the event is queued. Keys may be the field’s name
(product_id) or its JSON name (productId), but not both, which drops the
event; either way the property is sent under the JSON name. A field’s type comes
from the schema rather than the Ruby value, so amount: 10 is sent as a double.
A value of the wrong type, or one failing a rule such as currency needing
three uppercase letters, drops the whole event, and the log names the field
(quantity: expected int32, got String). Keys outside the schema pass through
as custom properties. Duration fields such as position take seconds as a
number, { seconds:, nanos: } or a Google::Protobuf::Duration, and are sent
as a string like "92.5s".
track logs and drops an event whose distinct_id starts with cookieless-, a
prefix the server reserves; it would reject the whole batch.
Visitor location
The server adds no geo to events sent with a private key: an event carries the
location you give it, or none at all.
pug.track("user-123", "purchase",
{ product_id: "sku_123", amount: 29.99, currency: "USD" },
location: { country: "DE", city: "Berlin" })| Field | Type | Notes |
|---|---|---|
country |
String |
ISO 3166-1 alpha-2 (DE). Case and surrounding spaces are normalized; a code Pug doesn’t recognize is dropped. |
continent, region, city, postal_code, metro_code |
String |
Sent as given, trimmed. |
timezone |
String |
IANA name (Europe/Berlin). |
latitude, longitude |
Numeric |
Decimal degrees, -90 to 90 and -180 to 180. Sent only as a pair: one without the other is dropped. |
Each field becomes the matching $-prefixed
auto-property:
postal_code lands as $postalCode, and the camelCase key works too. It’s the
only way to set an auto-property from this SDK: properties become custom
properties, and one whose key starts with $ drops the event.
A field that is absent, nil or empty is skipped silently. One that looks like
a mistake - unknown key, wrong type, a country code Pug doesn’t know, a
coordinate out of range - is dropped with a warning and the rest of the event
still sends.
Identify and sessions
Use a stable, unique external ID - your database user ID, not an email, which can change. Only the first identify after an anonymous session merges prior activity; the same external ID always resolves to the same profile, and traits are merged server-side, never removed.
# On the first identify, pass the client's anonymous ID to merge its prior anonymous activity.
pug.identify("user-123", { email: "user@example.com", plan: "pro" }, anonymous_id: "anon-abc123")pug.identify(external_id, traits = nil, options = nil) never raises and
returns at once: identifies are queued and sent in batches like events, and what
can’t be delivered goes to on_error. A read straight after an identify may not
see it yet.
traits must be a Hash of JSON-serializable values, with keys camelCased like
property keys: first_name: arrives as firstName. They
travel as a protobuf Struct, whose numbers are doubles, so an Integer past
2^53 is refused. A refused trait, an external_id starting with the reserved
cookieless- prefix, or an identify over 900 KiB is logged and nothing is sent.
A trait holding NaN or Infinity is skipped with a warning, and the rest
still goes out.
Pass anonymous_id: (must start with anon-) on the first identify to merge a
client’s prior anonymous activity into the profile, and device_id: to attach a
device; as with track, those keys set options even without braces. There is
no reset or rotate.
Sessions
The client uses one session ID for its lifetime; override it per call with
track(..., session_id:).
Auto-tracking
There is no auto-tracking: you send every event with pug.track. The server
adds no geo or user agent to these events, so pass the visitor’s
location yourself.
Consent and privacy
The Ruby SDK has no consent gate and no redaction hooks: respect the visitor’s
consent choice before you call track, and keep PII out of the properties you
pass.
Server-side reads
The private key also authorizes reads - the insights, profiles and activity
data the dashboard shows. Reads raise Pug::Error and are never retried.
Ingestion (track / identify) still never raises.
# Look up a profile by your external ID.
profile = pug.profiles.get_by_external_id("user-123")
# Auto-paginating: yields every matching profile, or returns an Enumerator without a block.
pug.profiles.list.each { |p| puts p.external_id }
# Run an insights query - the same spec the dashboard uses.
result = pug.insights.query(
spec: { insight_type: :INSIGHT_TYPE_TRENDS, events: [{ event: { kind: "order.completed" } }] },
time_range: { from: Time.now - (30 * 86_400), to: Time.now },
granularity: :GRANULARITY_DAY,
)| Namespace | Methods |
|---|---|
pug.profiles |
get(id, include_bots: false), get_by_external_id(external_id, include_bots: false), delete(id), list(request = nil, timeout: nil): auto-paginating |
pug.activity |
feed, event_explorer, heatmap, profile_sessions, profile_stats, filter_schema, property_values |
pug.insights |
query, segment_users, filter_schema, property_values |
Requests are a Hash, braces optional, or the generated request message.
Responses are the generated messages under Pug::Proto
(Pug::Proto::Shared::Insights::V1::QueryResponse, …) - call to_h on one
for plain Ruby data - except that profiles.get and get_by_external_id
return the profile itself. list streams, and the client’s timeout bounds the
whole stream, your block’s time included; pass timeout: (seconds) to give a
long listing more. Property names in reads are the stored ones, so a property
tracked as order_total: is filtered on, and comes back, as orderTotal.
Pug::Error#code is the Connect code as a Symbol (:not_found,
:unavailable, … - see the error model), or nil when
the failure didn’t come from the RPC layer, and cause is the underlying
exception:
begin
profile = pug.profiles.get_by_external_id("user-123")
rescue Pug::Error => e
raise unless e.code == :not_found
endReads leave out traffic tagged as a bot at ingest: web-SDK events from crawler
user agents and datacenter networks. Events from this SDK are never tagged. Set
include_bots: true to count bot traffic - profiles.get and
get_by_external_id take it as a keyword, and profiles.list,
insights.query (on its spec) and the activity feed, event_explorer,
heatmap, profile_sessions and profile_stats take it in the request:
profile = pug.profiles.get_by_external_id("user-123", include_bots: true)
sessions = pug.activity.profile_sessions(distinct_id: "user-123", include_bots: true).sessionsThe request and response shapes match the shared.* Connect services - see the
API reference for field-by-field schemas. From a backend without an SDK,
call those services directly with your private key (see
Authentication).
Upgrading
v0.1.0
identifyreturns at once instead of waiting for the server: identifies are queued and sent in batches like events, withBatchIdentifyor, on a server without it, one call each.on_errorreceives(error, undelivered), aPug::Undeliveredwhoseeventsoridentifieshold what was lost, instead of(error, events). A 0.0.x handler still passes the arity check but fails when it treats the second argument as an Array. Failed identifies reach it too, not only the log.- A
session_idthat isn’t a UUID is ignored with a warning, where 0.0.x dropped the event.
v0.0.2
- Property and trait keys in snake_case are sent in camelCase:
order_total:arrives asorderTotal. Data 0.0.1 stored keeps its snake_case keys, so saved filters need the camelCase names, and profiles hold both spellings.