scope-analytics 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scope_analytics-0.1.0/LICENSE +21 -0
- scope_analytics-0.1.0/MANIFEST.in +3 -0
- scope_analytics-0.1.0/PKG-INFO +253 -0
- scope_analytics-0.1.0/README.md +207 -0
- scope_analytics-0.1.0/scope_analytics/__init__.py +244 -0
- scope_analytics-0.1.0/scope_analytics/auto.py +335 -0
- scope_analytics-0.1.0/scope_analytics/cli.py +240 -0
- scope_analytics-0.1.0/scope_analytics/client.py +121 -0
- scope_analytics-0.1.0/scope_analytics/config.py +101 -0
- scope_analytics-0.1.0/scope_analytics/context.py +173 -0
- scope_analytics-0.1.0/scope_analytics/events.py +275 -0
- scope_analytics-0.1.0/scope_analytics/middleware.py +669 -0
- scope_analytics-0.1.0/scope_analytics/patches/__init__.py +9 -0
- scope_analytics-0.1.0/scope_analytics/patches/anthropic_patch.py +430 -0
- scope_analytics-0.1.0/scope_analytics/patches/gemini_patch.py +422 -0
- scope_analytics-0.1.0/scope_analytics/patches/openai_patch.py +483 -0
- scope_analytics-0.1.0/scope_analytics/queue.py +158 -0
- scope_analytics-0.1.0/scope_analytics.egg-info/PKG-INFO +253 -0
- scope_analytics-0.1.0/scope_analytics.egg-info/SOURCES.txt +23 -0
- scope_analytics-0.1.0/scope_analytics.egg-info/dependency_links.txt +1 -0
- scope_analytics-0.1.0/scope_analytics.egg-info/entry_points.txt +2 -0
- scope_analytics-0.1.0/scope_analytics.egg-info/requires.txt +18 -0
- scope_analytics-0.1.0/scope_analytics.egg-info/top_level.txt +1 -0
- scope_analytics-0.1.0/setup.cfg +4 -0
- scope_analytics-0.1.0/setup.py +56 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Scope AI
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: scope-analytics
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: AI-powered analytics SDK for backend applications with automatic LLM tracking
|
|
5
|
+
Home-page: https://github.com/scopeai/scope-analytics-python
|
|
6
|
+
Author: Scope AI
|
|
7
|
+
Author-email: support@scopeai.dev
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Requires-Python: >=3.8
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: httpx>=0.24.0
|
|
22
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
25
|
+
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
|
|
26
|
+
Requires-Dist: pytest-mock>=3.10.0; extra == "dev"
|
|
27
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
28
|
+
Requires-Dist: flake8>=6.0.0; extra == "dev"
|
|
29
|
+
Provides-Extra: openai
|
|
30
|
+
Requires-Dist: openai>=1.0.0; extra == "openai"
|
|
31
|
+
Provides-Extra: anthropic
|
|
32
|
+
Requires-Dist: anthropic>=0.18.0; extra == "anthropic"
|
|
33
|
+
Provides-Extra: langchain
|
|
34
|
+
Requires-Dist: langchain>=0.1.0; extra == "langchain"
|
|
35
|
+
Dynamic: author
|
|
36
|
+
Dynamic: author-email
|
|
37
|
+
Dynamic: classifier
|
|
38
|
+
Dynamic: description
|
|
39
|
+
Dynamic: description-content-type
|
|
40
|
+
Dynamic: home-page
|
|
41
|
+
Dynamic: license-file
|
|
42
|
+
Dynamic: provides-extra
|
|
43
|
+
Dynamic: requires-dist
|
|
44
|
+
Dynamic: requires-python
|
|
45
|
+
Dynamic: summary
|
|
46
|
+
|
|
47
|
+
# Scope Analytics - Backend SDK
|
|
48
|
+
|
|
49
|
+
AI-powered analytics for backend applications with **zero-code LLM conversation tracking**.
|
|
50
|
+
|
|
51
|
+
## Features
|
|
52
|
+
|
|
53
|
+
- **Automatic LLM Tracking**: Captures OpenAI, Anthropic, and Gemini calls automatically
|
|
54
|
+
- **Session Correlation**: Links backend events to frontend user sessions via `X-Scope-Session-ID` header
|
|
55
|
+
- **Conversation Intelligence**: Classifies LLM calls as user-facing vs background jobs
|
|
56
|
+
- **Zero Code Changes**: Drop-in integration with automatic monkey-patching
|
|
57
|
+
- **Async & Non-Blocking**: Events shipped in background without affecting performance
|
|
58
|
+
|
|
59
|
+
## Installation
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install scope-analytics
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Quick Start
|
|
66
|
+
|
|
67
|
+
Choose your installation method:
|
|
68
|
+
|
|
69
|
+
### Option A: No-Code Installation (Recommended)
|
|
70
|
+
|
|
71
|
+
Zero code changes required - just change how you run your app.
|
|
72
|
+
|
|
73
|
+
**Step 1: Set your API key**
|
|
74
|
+
```bash
|
|
75
|
+
export SCOPE_API_KEY="sk_live_..."
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Step 2: Run with scope-run**
|
|
79
|
+
```bash
|
|
80
|
+
# Instead of: python app.py
|
|
81
|
+
# Run: scope-run python app.py
|
|
82
|
+
|
|
83
|
+
# Instead of: uvicorn main:app --reload
|
|
84
|
+
# Run: scope-run uvicorn main:app --reload
|
|
85
|
+
|
|
86
|
+
# Instead of: gunicorn app:app -w 4
|
|
87
|
+
# Run: scope-run gunicorn app:app -w 4
|
|
88
|
+
|
|
89
|
+
# Instead of: flask run --port 5000
|
|
90
|
+
# Run: scope-run flask run --port 5000
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
That's it! Your LLM calls are now automatically tracked AND correlated
|
|
94
|
+
with frontend sessions (for FastAPI, Flask, Django).
|
|
95
|
+
|
|
96
|
+
### Option B: Code-Based Installation
|
|
97
|
+
|
|
98
|
+
Add 2 lines to your app for more control.
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from scope_analytics import ScopeAnalytics
|
|
102
|
+
scope = ScopeAnalytics(api_key="sk_live_...")
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
For session correlation with frontend, add middleware:
|
|
106
|
+
|
|
107
|
+
**FastAPI:**
|
|
108
|
+
```python
|
|
109
|
+
from fastapi import FastAPI
|
|
110
|
+
from scope_analytics import ScopeAnalytics, ScopeSessionMiddleware
|
|
111
|
+
|
|
112
|
+
app = FastAPI()
|
|
113
|
+
app.add_middleware(ScopeSessionMiddleware)
|
|
114
|
+
scope = ScopeAnalytics(api_key="sk_live_...")
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**Flask:**
|
|
118
|
+
```python
|
|
119
|
+
from flask import Flask
|
|
120
|
+
from scope_analytics import ScopeAnalytics, init_flask_session_tracking
|
|
121
|
+
|
|
122
|
+
app = Flask(__name__)
|
|
123
|
+
init_flask_session_tracking(app)
|
|
124
|
+
scope = ScopeAnalytics(api_key="sk_live_...")
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**Django:**
|
|
128
|
+
```python
|
|
129
|
+
# settings.py
|
|
130
|
+
MIDDLEWARE = [
|
|
131
|
+
'scope_analytics.middleware.DjangoScopeMiddleware',
|
|
132
|
+
# ... other middleware
|
|
133
|
+
]
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Installation Comparison
|
|
137
|
+
|
|
138
|
+
| Feature | No-Code (`scope-run`) | Code-Based |
|
|
139
|
+
|---------|----------------------|------------|
|
|
140
|
+
| Code changes required | **None** | 2 lines minimum |
|
|
141
|
+
| LLM tracking | ✅ Automatic | ✅ Automatic |
|
|
142
|
+
| Session correlation | ✅ Automatic (FastAPI/Flask/Django) | Manual middleware |
|
|
143
|
+
| Custom configuration | Via env vars | Full Python API |
|
|
144
|
+
| Best for | Quick start, CI/CD, ops teams | Developers wanting control |
|
|
145
|
+
|
|
146
|
+
## CLI Usage
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
# Show version
|
|
150
|
+
scope-run --version
|
|
151
|
+
|
|
152
|
+
# Show help
|
|
153
|
+
scope-run --help
|
|
154
|
+
|
|
155
|
+
# Run with debug logging
|
|
156
|
+
scope-run --debug python app.py
|
|
157
|
+
|
|
158
|
+
# Dry run (show what would be executed)
|
|
159
|
+
scope-run --dry-run uvicorn main:app
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Environment Variables
|
|
163
|
+
|
|
164
|
+
| Variable | Required | Description |
|
|
165
|
+
|----------|----------|-------------|
|
|
166
|
+
| `SCOPE_API_KEY` | Yes | Your Scope Analytics API key |
|
|
167
|
+
| `SCOPE_ENDPOINT` | No | Custom API endpoint |
|
|
168
|
+
| `SCOPE_DEBUG` | No | Set to 'true' for debug logging |
|
|
169
|
+
| `SCOPE_ENVIRONMENT` | No | Environment name (default: production) |
|
|
170
|
+
|
|
171
|
+
## Configuration (Code-Based)
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
scope = ScopeAnalytics(
|
|
175
|
+
api_key="sk_live_...", # Required: Your secret API key
|
|
176
|
+
endpoint="https://api.scopeai.dev", # Optional: API endpoint
|
|
177
|
+
auto_patch=True, # Optional: Auto-patch LLM libraries (default: True)
|
|
178
|
+
batch_size=10, # Optional: Events per batch (default: 10)
|
|
179
|
+
batch_timeout_seconds=5, # Optional: Max wait time (default: 5)
|
|
180
|
+
debug=False, # Optional: Enable debug logging
|
|
181
|
+
environment="production", # Optional: Environment name
|
|
182
|
+
)
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Framework Support Matrix
|
|
186
|
+
|
|
187
|
+
| Framework | Auto-Injection | Session Correlation | How |
|
|
188
|
+
|-----------|----------------|---------------------|-----|
|
|
189
|
+
| FastAPI | ✅ Automatic | ✅ Full | Patches `FastAPI.__init__` |
|
|
190
|
+
| Starlette | ✅ Automatic | ✅ Full | Patches `Starlette.__init__` |
|
|
191
|
+
| Flask | ✅ Automatic | ✅ Full | Patches `Flask.__init__` |
|
|
192
|
+
| Django | ✅ Automatic | ✅ Full | Modifies `settings.MIDDLEWARE` |
|
|
193
|
+
| Other ASGI | ⚠️ Manual | ✅ With 1 line | Add `ScopeSessionMiddleware` |
|
|
194
|
+
| Other WSGI | ⚠️ Manual | ✅ With 1 line | Call `ScopeContext.set_session_id()` |
|
|
195
|
+
| No framework | ⚠️ Manual | ✅ With 1 line | Call `ScopeContext.set_session_id()` |
|
|
196
|
+
|
|
197
|
+
## Manual Session Correlation (For Other Frameworks)
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
# Option A: ASGI middleware (for ASGI frameworks)
|
|
201
|
+
from scope_analytics import ScopeSessionMiddleware
|
|
202
|
+
app = ScopeSessionMiddleware(app)
|
|
203
|
+
|
|
204
|
+
# Option B: Manual context (for any framework)
|
|
205
|
+
from scope_analytics import ScopeContext
|
|
206
|
+
|
|
207
|
+
def my_request_handler(request):
|
|
208
|
+
# Extract session from header and set context
|
|
209
|
+
ScopeContext.set_session_id(request.headers.get('X-Scope-Session-ID'))
|
|
210
|
+
|
|
211
|
+
# Your code - LLM calls will now have session_id attached
|
|
212
|
+
response = openai.chat.completions.create(...)
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
**Key Point:** Even without middleware, LLM tracking STILL WORKS. The middleware is only needed for correlating backend events with frontend sessions via `X-Scope-Session-ID`.
|
|
216
|
+
|
|
217
|
+
## Frontend SDK
|
|
218
|
+
|
|
219
|
+
Add the frontend SDK to link user interactions with backend LLM calls:
|
|
220
|
+
|
|
221
|
+
```html
|
|
222
|
+
<script src="https://cdn.scopeai.dev/v1/sdk.js"
|
|
223
|
+
data-api-key="pk_live_your_public_key"></script>
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
The frontend SDK automatically:
|
|
227
|
+
- Tracks clicks, page views, form submissions
|
|
228
|
+
- Generates session IDs (stored in localStorage)
|
|
229
|
+
- Sends `X-Scope-Session-ID` header with API requests
|
|
230
|
+
|
|
231
|
+
## Comparison with Industry Tools
|
|
232
|
+
|
|
233
|
+
| Feature | Scope (`scope-run`) | DataDog (`ddtrace-run`) | New Relic |
|
|
234
|
+
|---------|---------------------|-------------------------|-----------|
|
|
235
|
+
| No code changes | ✅ | ✅ | ✅ |
|
|
236
|
+
| Env var config | ✅ `SCOPE_API_KEY` | ✅ `DD_API_KEY` | ✅ `NEW_RELIC_LICENSE_KEY` |
|
|
237
|
+
| Works with uvicorn | ✅ | ✅ | ✅ |
|
|
238
|
+
| Works with gunicorn | ✅ | ✅ | ✅ |
|
|
239
|
+
| LLM call capture | ✅ | ❌ | ❌ |
|
|
240
|
+
| Session correlation | ✅ | ❌ | ❌ |
|
|
241
|
+
| Debug mode | ✅ `--debug` | ✅ `--info` | ✅ |
|
|
242
|
+
|
|
243
|
+
## Full Documentation
|
|
244
|
+
|
|
245
|
+
See [SDK Installation Guide](../docs/sdk-installation-guide.md) for complete documentation including:
|
|
246
|
+
- Detailed configuration options
|
|
247
|
+
- Troubleshooting guide
|
|
248
|
+
- Complete example applications
|
|
249
|
+
- Verification steps
|
|
250
|
+
|
|
251
|
+
## License
|
|
252
|
+
|
|
253
|
+
MIT
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Scope Analytics - Backend SDK
|
|
2
|
+
|
|
3
|
+
AI-powered analytics for backend applications with **zero-code LLM conversation tracking**.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Automatic LLM Tracking**: Captures OpenAI, Anthropic, and Gemini calls automatically
|
|
8
|
+
- **Session Correlation**: Links backend events to frontend user sessions via `X-Scope-Session-ID` header
|
|
9
|
+
- **Conversation Intelligence**: Classifies LLM calls as user-facing vs background jobs
|
|
10
|
+
- **Zero Code Changes**: Drop-in integration with automatic monkey-patching
|
|
11
|
+
- **Async & Non-Blocking**: Events shipped in background without affecting performance
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install scope-analytics
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
Choose your installation method:
|
|
22
|
+
|
|
23
|
+
### Option A: No-Code Installation (Recommended)
|
|
24
|
+
|
|
25
|
+
Zero code changes required - just change how you run your app.
|
|
26
|
+
|
|
27
|
+
**Step 1: Set your API key**
|
|
28
|
+
```bash
|
|
29
|
+
export SCOPE_API_KEY="sk_live_..."
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
**Step 2: Run with scope-run**
|
|
33
|
+
```bash
|
|
34
|
+
# Instead of: python app.py
|
|
35
|
+
# Run: scope-run python app.py
|
|
36
|
+
|
|
37
|
+
# Instead of: uvicorn main:app --reload
|
|
38
|
+
# Run: scope-run uvicorn main:app --reload
|
|
39
|
+
|
|
40
|
+
# Instead of: gunicorn app:app -w 4
|
|
41
|
+
# Run: scope-run gunicorn app:app -w 4
|
|
42
|
+
|
|
43
|
+
# Instead of: flask run --port 5000
|
|
44
|
+
# Run: scope-run flask run --port 5000
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
That's it! Your LLM calls are now automatically tracked AND correlated
|
|
48
|
+
with frontend sessions (for FastAPI, Flask, Django).
|
|
49
|
+
|
|
50
|
+
### Option B: Code-Based Installation
|
|
51
|
+
|
|
52
|
+
Add 2 lines to your app for more control.
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from scope_analytics import ScopeAnalytics
|
|
56
|
+
scope = ScopeAnalytics(api_key="sk_live_...")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
For session correlation with frontend, add middleware:
|
|
60
|
+
|
|
61
|
+
**FastAPI:**
|
|
62
|
+
```python
|
|
63
|
+
from fastapi import FastAPI
|
|
64
|
+
from scope_analytics import ScopeAnalytics, ScopeSessionMiddleware
|
|
65
|
+
|
|
66
|
+
app = FastAPI()
|
|
67
|
+
app.add_middleware(ScopeSessionMiddleware)
|
|
68
|
+
scope = ScopeAnalytics(api_key="sk_live_...")
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**Flask:**
|
|
72
|
+
```python
|
|
73
|
+
from flask import Flask
|
|
74
|
+
from scope_analytics import ScopeAnalytics, init_flask_session_tracking
|
|
75
|
+
|
|
76
|
+
app = Flask(__name__)
|
|
77
|
+
init_flask_session_tracking(app)
|
|
78
|
+
scope = ScopeAnalytics(api_key="sk_live_...")
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Django:**
|
|
82
|
+
```python
|
|
83
|
+
# settings.py
|
|
84
|
+
MIDDLEWARE = [
|
|
85
|
+
'scope_analytics.middleware.DjangoScopeMiddleware',
|
|
86
|
+
# ... other middleware
|
|
87
|
+
]
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Installation Comparison
|
|
91
|
+
|
|
92
|
+
| Feature | No-Code (`scope-run`) | Code-Based |
|
|
93
|
+
|---------|----------------------|------------|
|
|
94
|
+
| Code changes required | **None** | 2 lines minimum |
|
|
95
|
+
| LLM tracking | ✅ Automatic | ✅ Automatic |
|
|
96
|
+
| Session correlation | ✅ Automatic (FastAPI/Flask/Django) | Manual middleware |
|
|
97
|
+
| Custom configuration | Via env vars | Full Python API |
|
|
98
|
+
| Best for | Quick start, CI/CD, ops teams | Developers wanting control |
|
|
99
|
+
|
|
100
|
+
## CLI Usage
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
# Show version
|
|
104
|
+
scope-run --version
|
|
105
|
+
|
|
106
|
+
# Show help
|
|
107
|
+
scope-run --help
|
|
108
|
+
|
|
109
|
+
# Run with debug logging
|
|
110
|
+
scope-run --debug python app.py
|
|
111
|
+
|
|
112
|
+
# Dry run (show what would be executed)
|
|
113
|
+
scope-run --dry-run uvicorn main:app
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Environment Variables
|
|
117
|
+
|
|
118
|
+
| Variable | Required | Description |
|
|
119
|
+
|----------|----------|-------------|
|
|
120
|
+
| `SCOPE_API_KEY` | Yes | Your Scope Analytics API key |
|
|
121
|
+
| `SCOPE_ENDPOINT` | No | Custom API endpoint |
|
|
122
|
+
| `SCOPE_DEBUG` | No | Set to 'true' for debug logging |
|
|
123
|
+
| `SCOPE_ENVIRONMENT` | No | Environment name (default: production) |
|
|
124
|
+
|
|
125
|
+
## Configuration (Code-Based)
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
scope = ScopeAnalytics(
|
|
129
|
+
api_key="sk_live_...", # Required: Your secret API key
|
|
130
|
+
endpoint="https://api.scopeai.dev", # Optional: API endpoint
|
|
131
|
+
auto_patch=True, # Optional: Auto-patch LLM libraries (default: True)
|
|
132
|
+
batch_size=10, # Optional: Events per batch (default: 10)
|
|
133
|
+
batch_timeout_seconds=5, # Optional: Max wait time (default: 5)
|
|
134
|
+
debug=False, # Optional: Enable debug logging
|
|
135
|
+
environment="production", # Optional: Environment name
|
|
136
|
+
)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Framework Support Matrix
|
|
140
|
+
|
|
141
|
+
| Framework | Auto-Injection | Session Correlation | How |
|
|
142
|
+
|-----------|----------------|---------------------|-----|
|
|
143
|
+
| FastAPI | ✅ Automatic | ✅ Full | Patches `FastAPI.__init__` |
|
|
144
|
+
| Starlette | ✅ Automatic | ✅ Full | Patches `Starlette.__init__` |
|
|
145
|
+
| Flask | ✅ Automatic | ✅ Full | Patches `Flask.__init__` |
|
|
146
|
+
| Django | ✅ Automatic | ✅ Full | Modifies `settings.MIDDLEWARE` |
|
|
147
|
+
| Other ASGI | ⚠️ Manual | ✅ With 1 line | Add `ScopeSessionMiddleware` |
|
|
148
|
+
| Other WSGI | ⚠️ Manual | ✅ With 1 line | Call `ScopeContext.set_session_id()` |
|
|
149
|
+
| No framework | ⚠️ Manual | ✅ With 1 line | Call `ScopeContext.set_session_id()` |
|
|
150
|
+
|
|
151
|
+
## Manual Session Correlation (For Other Frameworks)
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
# Option A: ASGI middleware (for ASGI frameworks)
|
|
155
|
+
from scope_analytics import ScopeSessionMiddleware
|
|
156
|
+
app = ScopeSessionMiddleware(app)
|
|
157
|
+
|
|
158
|
+
# Option B: Manual context (for any framework)
|
|
159
|
+
from scope_analytics import ScopeContext
|
|
160
|
+
|
|
161
|
+
def my_request_handler(request):
|
|
162
|
+
# Extract session from header and set context
|
|
163
|
+
ScopeContext.set_session_id(request.headers.get('X-Scope-Session-ID'))
|
|
164
|
+
|
|
165
|
+
# Your code - LLM calls will now have session_id attached
|
|
166
|
+
response = openai.chat.completions.create(...)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
**Key Point:** Even without middleware, LLM tracking STILL WORKS. The middleware is only needed for correlating backend events with frontend sessions via `X-Scope-Session-ID`.
|
|
170
|
+
|
|
171
|
+
## Frontend SDK
|
|
172
|
+
|
|
173
|
+
Add the frontend SDK to link user interactions with backend LLM calls:
|
|
174
|
+
|
|
175
|
+
```html
|
|
176
|
+
<script src="https://cdn.scopeai.dev/v1/sdk.js"
|
|
177
|
+
data-api-key="pk_live_your_public_key"></script>
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
The frontend SDK automatically:
|
|
181
|
+
- Tracks clicks, page views, form submissions
|
|
182
|
+
- Generates session IDs (stored in localStorage)
|
|
183
|
+
- Sends `X-Scope-Session-ID` header with API requests
|
|
184
|
+
|
|
185
|
+
## Comparison with Industry Tools
|
|
186
|
+
|
|
187
|
+
| Feature | Scope (`scope-run`) | DataDog (`ddtrace-run`) | New Relic |
|
|
188
|
+
|---------|---------------------|-------------------------|-----------|
|
|
189
|
+
| No code changes | ✅ | ✅ | ✅ |
|
|
190
|
+
| Env var config | ✅ `SCOPE_API_KEY` | ✅ `DD_API_KEY` | ✅ `NEW_RELIC_LICENSE_KEY` |
|
|
191
|
+
| Works with uvicorn | ✅ | ✅ | ✅ |
|
|
192
|
+
| Works with gunicorn | ✅ | ✅ | ✅ |
|
|
193
|
+
| LLM call capture | ✅ | ❌ | ❌ |
|
|
194
|
+
| Session correlation | ✅ | ❌ | ❌ |
|
|
195
|
+
| Debug mode | ✅ `--debug` | ✅ `--info` | ✅ |
|
|
196
|
+
|
|
197
|
+
## Full Documentation
|
|
198
|
+
|
|
199
|
+
See [SDK Installation Guide](../docs/sdk-installation-guide.md) for complete documentation including:
|
|
200
|
+
- Detailed configuration options
|
|
201
|
+
- Troubleshooting guide
|
|
202
|
+
- Complete example applications
|
|
203
|
+
- Verification steps
|
|
204
|
+
|
|
205
|
+
## License
|
|
206
|
+
|
|
207
|
+
MIT
|