From 61a4a6ae15009333f5e075bd48c6492ccea22887 Mon Sep 17 00:00:00 2001 From: "Vincent (Wen Yu) Ge" Date: Tue, 21 Oct 2025 20:56:34 -0400 Subject: [PATCH] add scripts --- api-integration/capture-api/basic-alias.js | 26 ++++++ api-integration/capture-api/basic-alias.py | 25 ++++++ api-integration/capture-api/basic-alias.sh | 15 ++++ api-integration/capture-api/basic-batch.js | 37 ++++++++ api-integration/capture-api/basic-batch.py | 36 ++++++++ api-integration/capture-api/basic-batch.sh | 26 ++++++ api-integration/capture-api/basic-capture.js | 25 ++++++ api-integration/capture-api/basic-capture.py | 24 ++++++ api-integration/capture-api/basic-capture.sh | 12 +++ .../capture-api/basic-exception.js | 82 ++++++++++++++++++ .../capture-api/basic-exception.py | 85 +++++++++++++++++++ .../capture-api/basic-exception.sh | 63 ++++++++++++++ api-integration/capture-api/basic-identify.js | 27 ++++++ api-integration/capture-api/basic-identify.py | 26 ++++++ api-integration/capture-api/basic-identify.sh | 16 ++++ .../capture-properties-timestamp.js | 28 ++++++ .../capture-properties-timestamp.py | 28 ++++++ .../capture-properties-timestamp.sh | 17 ++++ 18 files changed, 598 insertions(+) create mode 100644 api-integration/capture-api/basic-alias.js create mode 100644 api-integration/capture-api/basic-alias.py create mode 100755 api-integration/capture-api/basic-alias.sh create mode 100644 api-integration/capture-api/basic-batch.js create mode 100644 api-integration/capture-api/basic-batch.py create mode 100755 api-integration/capture-api/basic-batch.sh create mode 100644 api-integration/capture-api/basic-capture.js create mode 100644 api-integration/capture-api/basic-capture.py create mode 100755 api-integration/capture-api/basic-capture.sh create mode 100644 api-integration/capture-api/basic-exception.js create mode 100644 api-integration/capture-api/basic-exception.py create mode 100755 api-integration/capture-api/basic-exception.sh create mode 100644 api-integration/capture-api/basic-identify.js create mode 100644 api-integration/capture-api/basic-identify.py create mode 100755 api-integration/capture-api/basic-identify.sh create mode 100644 api-integration/capture-api/capture-properties-timestamp.js create mode 100644 api-integration/capture-api/capture-properties-timestamp.py create mode 100755 api-integration/capture-api/capture-properties-timestamp.sh diff --git a/api-integration/capture-api/basic-alias.js b/api-integration/capture-api/basic-alias.js new file mode 100644 index 0000000..5cb059c --- /dev/null +++ b/api-integration/capture-api/basic-alias.js @@ -0,0 +1,26 @@ +// Load environment variables from .env file +require('dotenv').config(); + +const headers = { + "Content-Type": "application/json", +}; + +const body = { + "api_key": process.env.POSTHOG_API_KEY, + "properties": { + "distinct_id": "test-user-javascript", + "alias": "test-user-javascript-alias" + }, + "event": "$create_alias" +}; + +const url = `${process.env.POSTHOG_API_HOST}/i/v0/e/`; + +fetch(url, { + method: 'POST', + headers: headers, + body: JSON.stringify(body) +}) +.then(response => response.json()) +.then(data => console.log(data)) +.catch(error => console.error('Error:', error)); diff --git a/api-integration/capture-api/basic-alias.py b/api-integration/capture-api/basic-alias.py new file mode 100644 index 0000000..03a702e --- /dev/null +++ b/api-integration/capture-api/basic-alias.py @@ -0,0 +1,25 @@ +import requests +import os +from dotenv import load_dotenv + +# Load environment variables from .env file +load_dotenv() + +url = f"{os.getenv('POSTHOG_API_HOST')}/i/v0/e/" + +headers = { + "Content-Type": "application/json", +} + +body = { + "api_key": os.getenv('POSTHOG_API_KEY'), + "properties": { + "distinct_id": "test-user-python", + "alias": "test-user-python-alias" + }, + "event": "$create_alias" +} + +response = requests.post(url, headers=headers, json=body) + +print(response.json()) diff --git a/api-integration/capture-api/basic-alias.sh b/api-integration/capture-api/basic-alias.sh new file mode 100755 index 0000000..78cf1d6 --- /dev/null +++ b/api-integration/capture-api/basic-alias.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Load environment variables from .env file +if [ -f .env ]; then + export $(cat .env | grep -v '^#' | xargs) +fi + +curl -v -L --header "Content-Type: application/json" -d "{ + \"api_key\": \"$POSTHOG_API_KEY\", + \"properties\": { + \"distinct_id\": \"test-user-bash\", + \"alias\": \"test-user-bash-alias\" + }, + \"event\": \"\$create_alias\" +}" $POSTHOG_API_HOST/i/v0/e/ diff --git a/api-integration/capture-api/basic-batch.js b/api-integration/capture-api/basic-batch.js new file mode 100644 index 0000000..8e98c69 --- /dev/null +++ b/api-integration/capture-api/basic-batch.js @@ -0,0 +1,37 @@ +// Load environment variables from .env file +require('dotenv').config(); + +const headers = { + "Content-Type": "application/json", +}; + +const body = { + "api_key": process.env.POSTHOG_API_KEY, + "batch": [ + { + "event": "batched_event", + "properties" : { + "distinct_id": "test-user-javascript", + "number_in_batch": 1 + } + }, + { + "event": "batched_event", + "properties" : { + "distinct_id": "test-user-javascript", + "number_in_batch": 2 + } + } + ] +}; + +const url = `${process.env.POSTHOG_API_HOST}/batch/`; + +fetch(url, { + method: 'POST', + headers: headers, + body: JSON.stringify(body) +}) +.then(response => response.json()) +.then(data => console.log(data)) +.catch(error => console.error('Error:', error)); diff --git a/api-integration/capture-api/basic-batch.py b/api-integration/capture-api/basic-batch.py new file mode 100644 index 0000000..13f5c97 --- /dev/null +++ b/api-integration/capture-api/basic-batch.py @@ -0,0 +1,36 @@ +import requests +import os +from dotenv import load_dotenv + +# Load environment variables from .env file +load_dotenv() + +url = f"{os.getenv('POSTHOG_API_HOST')}/batch/" + +headers = { + "Content-Type": "application/json", +} + +body = { + "api_key": os.getenv('POSTHOG_API_KEY'), + "batch": [ + { + "event": "batched_event", + "properties" : { + "distinct_id": "test-user-python", + "number_in_batch": 1 + } + }, + { + "event": "batched_event", + "properties" : { + "distinct_id": "test-user-python", + "number_in_batch": 2 + } + } + ] +} + +response = requests.post(url, headers=headers, json=body) + +print(response.json()) diff --git a/api-integration/capture-api/basic-batch.sh b/api-integration/capture-api/basic-batch.sh new file mode 100755 index 0000000..393f3c7 --- /dev/null +++ b/api-integration/capture-api/basic-batch.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Load environment variables from .env file +if [ -f .env ]; then + export $(cat .env | grep -v '^#' | xargs) +fi + +curl -v -L --header "Content-Type: application/json" -d "{ + \"api_key\": \"$POSTHOG_API_KEY\", + \"batch\": [ + { + \"event\": \"batched_event\", + \"properties\" : { + \"distinct_id\": \"test-user-bash\", + \"number_in_batch\": 1 + } + }, + { + \"event\": \"batched_event\", + \"properties\" : { + \"distinct_id\": \"test-user-bash\", + \"number_in_batch\": 2 + } + } + ] +}" $POSTHOG_API_HOST/batch/ diff --git a/api-integration/capture-api/basic-capture.js b/api-integration/capture-api/basic-capture.js new file mode 100644 index 0000000..83b73fa --- /dev/null +++ b/api-integration/capture-api/basic-capture.js @@ -0,0 +1,25 @@ +// Load environment variables from .env file +require('dotenv').config(); + +const headers = { + "Content-Type": "application/json", +}; + +const body = { + "api_key": process.env.POSTHOG_API_KEY, + "event": "request", + "properties": { + "distinct_id": "test-user-javascript" + } +}; + +const url = `${process.env.POSTHOG_API_HOST}/i/v0/e/`; + +fetch(url, { + method: 'POST', + headers: headers, + body: JSON.stringify(body) +}) +.then(response => response.json()) +.then(data => console.log(data)) +.catch(error => console.error('Error:', error)); \ No newline at end of file diff --git a/api-integration/capture-api/basic-capture.py b/api-integration/capture-api/basic-capture.py new file mode 100644 index 0000000..73c56b8 --- /dev/null +++ b/api-integration/capture-api/basic-capture.py @@ -0,0 +1,24 @@ +import requests +import os +from dotenv import load_dotenv + +# Load environment variables from .env file +load_dotenv() + +headers = { + "Content-Type": "application/json", +} + +body = { + "api_key": os.getenv('POSTHOG_API_KEY'), + "event": "request", + "properties": { + "distinct_id": "test-user-python" + } +} + +url = f"{os.getenv('POSTHOG_API_HOST')}/i/v0/e/" + +response = requests.post(url, headers=headers, json=body) + +print(response.json()) \ No newline at end of file diff --git a/api-integration/capture-api/basic-capture.sh b/api-integration/capture-api/basic-capture.sh new file mode 100755 index 0000000..66461d2 --- /dev/null +++ b/api-integration/capture-api/basic-capture.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +# Load environment variables from .env file +if [ -f .env ]; then + export $(cat .env | grep -v '^#' | xargs) +fi + +curl -v -L --header "Content-Type: application/json" -d "{ + \"api_key\": \"$POSTHOG_API_KEY\", + \"event\": \"request\", + \"distinct_id\": \"test-user-bash\" +}" $POSTHOG_API_HOST/i/v0/e/ \ No newline at end of file diff --git a/api-integration/capture-api/basic-exception.js b/api-integration/capture-api/basic-exception.js new file mode 100644 index 0000000..66c04e2 --- /dev/null +++ b/api-integration/capture-api/basic-exception.js @@ -0,0 +1,82 @@ +// Load environment variables from .env file +require('dotenv').config(); +const crypto = require('crypto'); + +const headers = { + "Content-Type": "application/json", +}; + +// Create a fake exception for demonstration +try { + // Simulate an error_event_javascript + throw new Error('error_event_javascript: This is a simulated error for testing'); +} catch (error) { + // Create exception fingerprint based on exception message + const fingerprint = crypto.createHash('md5').update(error.message).digest('hex'); + + const body = { + "api_key": process.env.POSTHOG_API_KEY, + "event": "$exception", + "properties": { + "distinct_id": "test-user-javascript", + "$exception_list": [{ + "type": error.name, + "value": error.message, + "mechanism": { + "handled": true, + "synthetic": false + }, + "stacktrace": { + "type": "raw", + "frames": [ + { + "platform": "custom", + "lang": "javascript", + "function": "error_event_javascript", + "filename": "basic-exception.js", + "lineno": 8, + "colno": 1, + "module": "exception_handler", + "resolved": true, + "in_app": true + }, + { + "platform": "custom", + "lang": "javascript", + "function": "simulateError", + "filename": "error-simulator.js", + "lineno": 15, + "colno": 5, + "module": "testing", + "resolved": true, + "in_app": true + }, + { + "platform": "custom", + "lang": "javascript", + "function": "main", + "filename": "app.js", + "lineno": 42, + "colno": 12, + "module": "application", + "resolved": false, + "in_app": false + } + ] + } + }], + "$exception_fingerprint": fingerprint + } + }; + + const url = `${process.env.POSTHOG_API_HOST}/i/v0/e/`; + + fetch(url, { + method: 'POST', + headers: headers, + body: JSON.stringify(body) + }) + .then(response => response.json()) + .then(data => console.log(data)) + .catch(err => console.error('Error:', err)); +} diff --git a/api-integration/capture-api/basic-exception.py b/api-integration/capture-api/basic-exception.py new file mode 100644 index 0000000..8d6318b --- /dev/null +++ b/api-integration/capture-api/basic-exception.py @@ -0,0 +1,85 @@ +import requests +import os +import traceback +import hashlib +import time +from dotenv import load_dotenv + +# Load environment variables from .env file +load_dotenv() + +url = f"{os.getenv('POSTHOG_API_HOST')}/i/v0/e/" + +headers = { + "Content-Type": "application/json", +} + +# Create a fake exception for demonstration +try: + # Simulate an error_event_python + raise ValueError("error_event_python: This is a simulated error for testing") +except Exception as e: + # Get the current traceback + tb = traceback.format_exc() + + # Create exception fingerprint based on exception message + fingerprint = hashlib.md5(str(e).encode()).hexdigest() + + body = { + "api_key": os.getenv('POSTHOG_API_KEY'), + "event": "$exception", + "properties": { + "distinct_id": "test-user-python", + "$exception_list": [{ + "type": type(e).__name__, + "value": str(e), + "mechanism": { + "handled": True, + "synthetic": False + }, + "stacktrace": { + "type": "raw", + "frames": [ + { + "platform": "custom", + "lang": "python", + "function": "error_event_python", + "filename": "basic-exception.py", + "lineno": 15, + "colno": 1, + "module": "exception_handler", + "resolved": True, + "in_app": True + }, + { + "platform": "custom", + "lang": "python", + "function": "simulate_error", + "filename": "error_simulator.py", + "lineno": 8, + "colno": 5, + "module": "testing", + "resolved": True, + "in_app": True + }, + { + "platform": "custom", + "lang": "python", + "function": "main", + "filename": "app.py", + "lineno": 42, + "colno": 12, + "module": "application", + "resolved": False, + "in_app": False + } + ] + } + }], + "$exception_fingerprint": fingerprint + } + } + +response = requests.post(url, headers=headers, json=body) + +print(response.json()) diff --git a/api-integration/capture-api/basic-exception.sh b/api-integration/capture-api/basic-exception.sh new file mode 100755 index 0000000..0a9ab6f --- /dev/null +++ b/api-integration/capture-api/basic-exception.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +# Load environment variables from .env file +if [ -f .env ]; then + export $(cat .env | grep -v '^#' | xargs) +fi + +curl -X POST "$POSTHOG_API_HOST/i/v0/e/" \ + -H "Content-Type: application/json" \ + -d "{ + \"api_key\": \"$POSTHOG_API_KEY\", + \"event\": \"\$exception\", + \"properties\": { + \"distinct_id\": \"test-user-bash\", + \"\$exception_list\": [{ + \"type\": \"ScriptError\", + \"value\": \"Command not found: fake_command\", + \"mechanism\": { + \"handled\": true, + \"synthetic\": false + }, + \"stacktrace\": { + \"type\": \"raw\", + \"frames\": [ + { + \"platform\": \"custom\", + \"lang\": \"bash\", + \"function\": \"main\", + \"filename\": \"basic-exception.sh\", + \"lineno\": 15, + \"colno\": 1, + \"module\": \"script_execution\", + \"resolved\": true, + \"in_app\": true + }, + { + \"platform\": \"custom\", + \"lang\": \"bash\", + \"function\": \"execute_command\", + \"filename\": \"utils.sh\", + \"lineno\": 42, + \"colno\": 5, + \"module\": \"command_handler\", + \"resolved\": true, + \"in_app\": true + }, + { + \"platform\": \"custom\", + \"lang\": \"bash\", + \"function\": \"error_event_bash\", + \"filename\": \"error_handler.sh\", + \"lineno\": 8, + \"colno\": 12, + \"module\": \"error_tracking\", + \"resolved\": false, + \"in_app\": false + } + ] + } + }], + \"\$exception_fingerprint\": \"$(echo 'Command not found: fake_command' | md5sum | cut -c1-32)\" + } + }" diff --git a/api-integration/capture-api/basic-identify.js b/api-integration/capture-api/basic-identify.js new file mode 100644 index 0000000..c39ef0c --- /dev/null +++ b/api-integration/capture-api/basic-identify.js @@ -0,0 +1,27 @@ +// Load environment variables from .env file +require('dotenv').config(); + +const headers = { + "Content-Type": "application/json", +}; + +const body = { + "api_key": process.env.POSTHOG_API_KEY, + "distinct_id": "test-user-javascript", + "$set": { + "email": "test-user-javascript@example.com", + "is_cool": true + }, + "event": "$identify" +}; + +const url = `${process.env.POSTHOG_API_HOST}/i/v0/e/`; + +fetch(url, { + method: 'POST', + headers: headers, + body: JSON.stringify(body) +}) +.then(response => response.json()) +.then(data => console.log(data)) +.catch(error => console.error('Error:', error)); diff --git a/api-integration/capture-api/basic-identify.py b/api-integration/capture-api/basic-identify.py new file mode 100644 index 0000000..052d136 --- /dev/null +++ b/api-integration/capture-api/basic-identify.py @@ -0,0 +1,26 @@ +import requests +import os +from dotenv import load_dotenv + +# Load environment variables from .env file +load_dotenv() + +url = f"{os.getenv('POSTHOG_API_HOST')}/i/v0/e/" + +headers = { + "Content-Type": "application/json", +} + +body = { + "api_key": os.getenv('POSTHOG_API_KEY'), + "distinct_id": "test-user-python", + "$set": { + "email": "test-user-python@example.com", + "is_cool": False + }, + "event": "$identify" +} + +response = requests.post(url, headers=headers, json=body) + +print(response.json()) diff --git a/api-integration/capture-api/basic-identify.sh b/api-integration/capture-api/basic-identify.sh new file mode 100755 index 0000000..396ae65 --- /dev/null +++ b/api-integration/capture-api/basic-identify.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# Load environment variables from .env file +if [ -f .env ]; then + export $(cat .env | grep -v '^#' | xargs) +fi + +curl -v -L --header "Content-Type: application/json" -d "{ + \"api_key\": \"$POSTHOG_API_KEY\", + \"distinct_id\": \"test-user-bash\", + \"\$set\": { + \"email\": \"test-user-bash@example.com\", + \"is_cool\": true + }, + \"event\": \"\$identify\" +}" $POSTHOG_API_HOST/i/v0/e/ diff --git a/api-integration/capture-api/capture-properties-timestamp.js b/api-integration/capture-api/capture-properties-timestamp.js new file mode 100644 index 0000000..2103f88 --- /dev/null +++ b/api-integration/capture-api/capture-properties-timestamp.js @@ -0,0 +1,28 @@ +// Load environment variables from .env file +require('dotenv').config(); + +const headers = { + "Content-Type": "application/json", +}; + +const body = { + "api_key": process.env.POSTHOG_API_KEY, + "event": "big_request", + "timestamp": new Date().toISOString(), + "properties": { + "distinct_id": "test-user-javascript", + "request_size": "big", + "api_request": true + } +}; + +const url = `${process.env.POSTHOG_API_HOST}/i/v0/e/`; + +fetch(url, { + method: 'POST', + headers: headers, + body: JSON.stringify(body) +}) +.then(response => response.json()) +.then(data => console.log(data)) +.catch(error => console.error('Error:', error)); \ No newline at end of file diff --git a/api-integration/capture-api/capture-properties-timestamp.py b/api-integration/capture-api/capture-properties-timestamp.py new file mode 100644 index 0000000..d6658e4 --- /dev/null +++ b/api-integration/capture-api/capture-properties-timestamp.py @@ -0,0 +1,28 @@ +import requests +import os +from dotenv import load_dotenv +from datetime import datetime + +# Load environment variables from .env file +load_dotenv() + +headers = { + "Content-Type": "application/json", +} + +body = { + "api_key": os.getenv('POSTHOG_API_KEY'), + "event": "big_request", + "timestamp": datetime.now().isoformat(), + "properties": { + "distinct_id": "test-user-python", + "request_size": "big", + "api_request": True + } +} + +url = f"{os.getenv('POSTHOG_API_HOST')}/i/v0/e/" + +response = requests.post(url, headers=headers, json=body) + +print(response.json()) \ No newline at end of file diff --git a/api-integration/capture-api/capture-properties-timestamp.sh b/api-integration/capture-api/capture-properties-timestamp.sh new file mode 100755 index 0000000..599f58a --- /dev/null +++ b/api-integration/capture-api/capture-properties-timestamp.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# Load environment variables from .env file +if [ -f .env ]; then + export $(cat .env | grep -v '^#' | xargs) +fi + +curl -v -L --header "Content-Type: application/json" -d "{ + \"api_key\": \"$POSTHOG_API_KEY\", + \"properties\": { + \"request_size\": \"big\", + \"api_request\": true + }, + \"timestamp\": \"$(date -u +%Y-%m-%dT%H:%M:%S.%3NZ)\", + \"distinct_id\": \"test-user-bash\", + \"event\": \"big_request\" +}" $POSTHOG_API_HOST/i/v0/e/ \ No newline at end of file