Skip to content

Getting Started with Event Tracking

In this guide, you'll learn how to send events to our analytics API endpoint /events/track from various programming languages. The backend service is built with NestJS, and we've set up an API endpoint to track events. We'll cover how to interact with this API using cURL, Python, JavaScript (Fetch API), Java, PHP, Ruby, and TypeScript.

API Endpoint

  • Endpoint: POST /events/track
  • URL: https://events.shopcircle.co/events/track
  • Headers:
    • Content-Type: application/json
    • shopcircle-api-key: YOUR_API_KEY (replace YOUR_API_KEY with a valid key)

Note: To get an API key, please reach out to either Adel or Admir.

Request Body Example

json
{
  "myshopify_domain": "example.com",
  "merchant_email": "merchant@example.com",
  "event_name": "signup",
  "event_type": "track",
  "application": "My Application",
  "properties": {
    "plan": "Pro",
    "referrer": "email-campaign"
  }
}

Sending Events

Using cURL

sh
curl -X POST https://events.shopcircle.co/events/track \
     -H "Content-Type: application/json" \
     -H "shopcircle-api-key: YOUR_API_KEY" \
     -d '{
           "myshopify_domain": "example.com",
           "merchant_email": "merchant@example.com",
           "event_name": "signup",
           "event_type": "track",
           "application": "My Application",
           "properties": {
             "plan": "Pro",
             "referrer": "email-campaign"
           }
         }'

Using Python (requests library)

python
import requests
import json

url = 'https://events.shopcircle.co/events/track'
headers = {
    'Content-Type': 'application/json',
    'shopcircle-api-key': 'YOUR_API_KEY'
}
data = {
    "myshopify_domain": "example.com",
    "merchant_email": "merchant@example.com",
    "event_name": "signup",
    "event_type": "track",
    "application": "My Application",
    "properties": {
        "plan": "Pro",
        "referrer": "email-campaign"
    }
}

response = requests.post(url, headers=headers, data=json.dumps(data))

if response.status_code == 200:
    print('Event sent successfully')
else:
    print(f'Failed to send event: {response.status_code} {response.text}')

Using JavaScript (Fetch API)

javascript
fetch('https://events.shopcircle.co/events/track', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'shopcircle-api-key': 'YOUR_API_KEY'
  },
  body: JSON.stringify({
    myshopify_domain: 'example.com',
    merchant_email: 'merchant@example.com',
    event_name: 'signup',
    event_type: 'track',
    application: 'My Application',
    properties: {
      plan: 'Pro',
      referrer: 'email-campaign'
    }
  })
})
.then(response => response.json())
.then(data => {
  console.log('Event sent successfully:', data);
})
.catch(error => {
  console.error('Error sending event:', error);
});

Using Java (HttpURLConnection)

java
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class SendEvent {

    public static void main(String[] args) {
        try {
            URL url = new URL("https://events.shopcircle.co/events/track");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("shopcircle-api-key", "YOUR_API_KEY");
            conn.setDoOutput(true);

            String jsonInputString = "{"
                + "\"myshopify_domain\": \"example.com\","
                + "\"merchant_email\": \"merchant@example.com\","
                + "\"event_name\": \"signup\","
                + "\"event_type\": \"track\","
                + "\"application\": \"My Application\","
                + "\"properties\": {"
                + "\"plan\": \"Pro\","
                + "\"referrer\": \"email-campaign\""
                + "}"
                + "}";

            try (OutputStream os = conn.getOutputStream()) {
                byte[] input = jsonInputString.getBytes("utf-8");
                os.write(input, 0, input.length);
            }

            int code = conn.getResponseCode();
            if (code == HttpURLConnection.HTTP_OK) {
                System.out.println("Event sent successfully");
            } else {
                System.out.println("Failed to send event: " + code);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Using PHP (cURL)

php
<?php

$url = 'https://events.shopcircle.co/events/track';
$data = array(
    'myshopify_domain' => 'example.com',
    'merchant_email' => 'merchant@example.com',
    'event_name' => 'signup',
    'event_type' => 'track',
    'application' => 'My Application',
    'properties' => array(
        'plan' => 'Pro',
        'referrer' => 'email-campaign'
    )
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'shopcircle-api-key: YOUR_API_KEY'
));

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    echo 'Response: ' . $response;
}

curl_close($ch);
?>

Using Ruby (Net::HTTP)

ruby
require 'net/http'
require 'json'
require 'uri'

url = URI.parse('https://events.shopcircle.co/events/track')
header = {
  'Content-Type': 'application/json',
  'shopcircle-api-key': 'YOUR_API_KEY'
}
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Post.new(url.request_uri, header)
request.body = {
  myshopify_domain: 'example.com',
  merchant_email: 'merchant@example.com',
  event_name: 'signup',
  event_type: 'track',
  application: 'My Application',
  properties: {
    plan: 'Pro',
    referrer: 'email-campaign'
  }
}.to_json

response = http.request(request)
puts "Response #{response.code} #{response.message}: #{response.body}"

Using TypeScript (Node.js with Fetch)

typescript
import fetch from 'node-fetch';

const url = 'https://events.shopcircle.co/events/track';
const headers = { 
  'Content-Type': 'application/json',
  'shopcircle-api-key': 'YOUR_API_KEY'
};

const data = {
  myshopify_domain: 'example.com',
  merchant_email: 'merchant@example.com',
  event_name: 'signup',
  event_type: 'track',
  application: 'My Application',
  properties: {
    plan: 'Pro',
    referrer: 'email-campaign'
  }
};

fetch(url, {
  method: 'POST',
  headers: headers,
  body: JSON.stringify(data)
})
  .then(response => response.json())
  .then(data => {
    console.log('Event sent successfully:', data);
  })
  .catch(error => {
    console.error('Error sending event:', error);
  });

Conclusion

You now have a variety of ways to send events to our analytics API, regardless of the language or environment you are working in. Whether you're using cURL, Python, JavaScript, Java, PHP, Ruby, or TypeScript, integrating with our event tracking system is straightforward and easy.

If you have any questions or need further assistance, feel free to reach out to our support team. Happy tracking!