---
updatedAt: 2025-06-17T21:39:51.000Z
agentTools:
  projectIndex: https://support.kissmetrics.io/llms.txt
---

# Ruby - Sending Data With Cron

Because the default mode of Kissmetrics sends a request to our tracking servers every time you record data, any slow connection between our servers and your servers could result in your site being adversely affected. Therefore we have provided a mode that runs in the background via cron. You might also consider looking at our [3rd-Party Libraries](https://support.kissmetrics.io/reference/other-3rd-party-libraries).

# Usage

The cron mode writes all requests to a log for later processing instead. It does require a little more set up on your end, but protects your site in the unlikely event that the Kissmetrics tracking servers are down, unreachable or slow to respond. To use this mode first call init with the appropriate args.

```ruby Here is an example using the Ruby Library:
KM.init("this is your key",
  :log_dir => '/var/logs/km/',
  :use_cron => true,
  :to_stderr => true)
```

```php PHP
<?php
 KM::init("this is your key", array(
  'log_dir' => '/var/logs/km/',
  'use_cron' => true,
  'to_stderr' => true))
?>
```

# Enabling the Cron Job

As logs will not automatically be uploaded to Kissmetrics, it needs to be handled by some external service. A suitable method may be a simple cronjob. The crontab line below shows a cronjob which will upload all the events every five minutes. It is OK to have multiple processes uploading files at the same time. The Ruby Library and PHP Library takes care of not uploading duplicate data.

> 🚧 Note:
>
> We are passing in the same `log_dir` that we set above.

```ruby Ruby
*/5 * * * * /usr/bin/ruby /km-install-dir/bin/km_send
```

```php PHP
*/5 * * * * /usr/bin/php /km-install-dir/km.php "your key" /var/logs/km
```

# Timestamps with Cron

Since data sent to Kissmetrics via Cron is sent asynchronously, a timestamp needs to be attached to each “record” or “set” call to ensure that the data comes in in the right order. Our libraries automatically attach these timestamps to each event; however, there’s an additional **`_d=1`** flag that you need to set to tell our server to “pay attention” to these custom timestamps.

When using Cron, you need to make sure that all of your `record` and `set` commands are recorded with that flag – for Ruby, that would look something like this:

```ruby
KMTS.record('bob@bob.com', 'Signed Up', {'Plan' => 'Pro', 'Amount' => 99.95, '_d' => 1})
```