Code samples
Umlogger is a simple and powerful service to help track, chart and analyze your data metrics.
Here are our collection of sample code to help you get started.
Your accounts secret API-key {apikey} is available on your account page when you're logged in.
Metric specific public key {metric_pubkey} is avaialble on each metric page once you're logged in.
cURL
curl -d "metric_name=response_time&value=21&apikey={apikey}" https://umlogger.com/p
HTML
<img src="https://umlogger.com/p?metric_pubkey={metric_pubkey}&value=1"/>
Javascript
<script type="text/javascript"> var _Umlogger = _Umlogger || []; (function() { var sh = document.createElement('script'); sh.type = 'text/javascript'; sh.async = true; sh.src = '//umlogger.com/assets/js/umlogger.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(sh, s); })(); document.addEventListener("DOMContentLoaded", function(){ _Umlogger.push(["value", "{metric_pubkey}", 3.14]); }); </script>
Go
package main import ( "net/url" "net/http" "strconv" "bytes" ) func main() { typeValue := "value" // or "count" metricName := "My Metric" // specifies what metric to log to. Needs to stay the same or a new metric will be created value := 3.14 apiKey := "{apikey}" data := url.Values{} data.Set("record_type", typeValue) data.Set("metric_name", metricName) data.Set("apikey", apiKey) data.Set("value", strconv.FormatFloat(value, 'f', -1, 64)) client := &http.Client{} req, _ := http.NewRequest("POST", "https://umlogger.com/p", bytes.NewBufferString(data.Encode())) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") resp, _ := client.Do(req) defer resp.Body.Close() }
PHP
$type = "value"; // or "count" $metric_name = "My Metric"; // specifies what metric to log to. Needs to stay the same or a new metric will be created $value = 3.14; $apikey = "{apikey}"; $payload = array( 'record_type' => $type, 'metric_name' => $metric_name, 'apikey' => $apikey, 'value' => $value, ); $curl = curl_init(); $options = array( CURLOPT_URL => "https://umlogger.com/p", CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => false, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => $payload, ); curl_setopt_array($curl, $options); curl_exec($curl);
C#
static async Task post_to_umlogger() {
string type = "value"; // or "count"
string metricName = "My Metric"; // specifies what metric to log to. Needs to stay the same or a new metric will be created
int value = 42;
string apiKey = "{apikey}";
Dictionary
Python
import urllib.parse import urllib.request type = "value" # or "count" metric_name = "My Metric" # specifies what metric to log to. Needs to stay the same or a new metric will be created value = 3.14 apikey = "{apikey}" payload = { 'record_type': type, 'metric_name': metric_name, 'apikey': apikey, 'value': value } data = urllib.parse.urlencode(payload).encode() req = urllib.request.Request("https://umlogger.com/p", data=data) with urllib.request.urlopen(req) as response: body = response.read()
Ruby
require 'uri' require 'net/http' type = "value" # or "count" metric_name = "My Metric" # specifies what metric to log to. Needs to stay the same or a new metric will be created value = 3.14 apikey = "{apikey}" payload = { record_type: type, metric_name: metric_name, apikey: apikey, value: value } uri = URI("https://umlogger.com/p") http = Net::HTTP.new(uri.host, 443) http.use_ssl = true request = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/x-www-form-urlencoded') request.body = URI.encode_www_form(payload) response = http.request(request)