Skip to content

Commit f788eef

Browse files
committed
Initial commit
1 parent c62795d commit f788eef

38 files changed

+1573
-41
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[*.yml]
11+
indent_style = space
12+
indent_size = 2

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.gitignore

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,10 @@ pids
1515
*.seed
1616
*.pid.lock
1717

18-
# Directory for instrumented libs generated by jscoverage/JSCover
19-
lib-cov
20-
2118
# Coverage directory used by tools like istanbul
2219
coverage
2320
*.lcov
2421

25-
# nyc test coverage
26-
.nyc_output
27-
28-
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29-
.grunt
30-
31-
# Bower dependency directory (https://bower.io/)
32-
bower_components
33-
3422
# node-waf configuration
3523
.lock-wscript
3624

@@ -72,33 +60,15 @@ typings/
7260
.env
7361
.env.test
7462

75-
# parcel-bundler cache (https://parceljs.org/)
76-
.cache
77-
78-
# Next.js build output
79-
.next
63+
# Lock files
64+
package-lock.json
8065

81-
# Nuxt.js build / generate output
82-
.nuxt
66+
# Build
8367
dist
68+
publish.sh
8469

85-
# Gatsby files
86-
.cache/
87-
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88-
# https://nextjs.org/blog/next-9-1#public-directory-support
89-
# public
90-
91-
# vuepress build output
92-
.vuepress/dist
93-
94-
# Serverless directories
95-
.serverless/
96-
97-
# FuseBox cache
98-
.fusebox/
99-
100-
# DynamoDB Local files
101-
.dynamodb/
102-
103-
# TernJS port file
104-
.tern-port
70+
# Dev server
71+
*.key
72+
*.crt
73+
*.pem
74+
*localhost*

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.size-snapshot.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"dist/streamsql.esm.js": {
3+
"bundled": 7741,
4+
"minified": 3828,
5+
"gzipped": 1575,
6+
"treeshaked": {
7+
"rollup": {
8+
"code": 75,
9+
"import_statements": 0
10+
},
11+
"webpack": {
12+
"code": 4442
13+
}
14+
}
15+
},
16+
"dist/streamsql.cjs.js": {
17+
"bundled": 9122,
18+
"minified": 4326,
19+
"gzipped": 1641
20+
},
21+
"dist/streamsql.min.js": {
22+
"bundled": 10015,
23+
"minified": 4153,
24+
"gzipped": 1615
25+
}
26+
}

README.md

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,61 @@
1-
# streamsql-js
2-
The javascript ingestion API for streamsql.
1+
# StreamSQL Javascript Ingestion API
2+
3+
> Upload data on your StreamSQL event streams.
4+
5+
## Install
6+
7+
### npm
8+
9+
```sh
10+
npm install @streamsql/streamsql-js
11+
```
12+
13+
### CDN
14+
15+
If not using a bundler, the package is available via CDN. Include the script tag in all pages where `streamsql` is used.
16+
17+
```html
18+
<script crossorigin src="https://unpkg.com/@streamsql/streamsql-js"></script>
19+
```
20+
21+
## Usage
22+
23+
```javascript
24+
import streamsql from '@streamsql/streamsql-js'
25+
26+
// Initialize the client
27+
streamsql.init(process.env.APIKEY)
28+
29+
// Set custom user properties
30+
streamsql.identify('USER_ID')
31+
32+
// Send data on your streams
33+
streamsql.sendEvent(
34+
'my-event-stream',
35+
{ eventName: 'My Event' },
36+
)
37+
```
38+
39+
## API
40+
41+
### streamsql
42+
43+
#### `streamsql.init(APIKEY <string>)`
44+
45+
Initialize the streamsql client with your API key. If you are using the CDN script, this must be called first on every page streamsql is used.
46+
47+
#### `streamsql.identify(userId <string>)`
48+
49+
Set a user id to be tied with events. The user id is persisted throughout multiple sessions.Use `streamsql.unidentify()` to clear set user ids.
50+
51+
#### `streamsql.unidentify()`
52+
53+
Clears any stored user ids that were set by `streamsql.identify('userId')`.
54+
55+
#### `streamsql.sendEvent(streamName <string> [,data <object>, onSent? <function>])`
56+
57+
Send data to `streamName`. The stream name must be valid and registered prior to sending. If data is not provided, default options will be sent including page context, any set user ids, timestamps, and so on. `onSent` is a an optional callback that is called with the response status code and response text when sending is complete.
58+
59+
## Related
60+
61+
> streamsql-pixel: Simple starter script to gather some general visitor data.

integration/fixtures/alt.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<script type="text/javascript" src="./streamsql.min.js"></script>
7+
<title>StreamSQL JS Alt Test Page</title>
8+
</head>
9+
<body>
10+
<h1>Alt Page</h1>
11+
<button id="send-button">Send event</button>
12+
<button id="logout-button">Logout</button>
13+
14+
15+
<script type="application/javascript">
16+
streamsql.init('0000-0000-0000-0000'); // create client with apiKey
17+
18+
// use to check that cookie set on previous page is working
19+
document.getElementById('send-button').addEventListener('click', function () {
20+
streamsql.sendEvent('userstream')
21+
})
22+
23+
document.getElementById('logout-button').addEventListener('click', function () {
24+
streamsql.unidentify() // clear the userid
25+
streamsql.sendEvent('userstream') // should send without user id
26+
})
27+
</script>
28+
</body>
29+
</html>

integration/fixtures/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports.apiEndpoint = '/api/send/single'
2+
module.exports.apiEndpointBatch = '/api/send/batch'
3+
module.exports.port = 9000
4+
module.exports.httpsPort = 9001

integration/fixtures/index.html

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<script type="text/javascript" src="./streamsql.min.js"></script>
7+
<title>StreamSQL JS Test Page</title>
8+
</head>
9+
<body>
10+
<h1>Test Page</h1>
11+
<button id="count-button">Count</button>
12+
13+
<input id="user-input" type="text" id="userid" value="" />
14+
<button id="login-button">LOGIN</button>
15+
16+
<a href="/alt.html">Go to alt page</a>
17+
18+
<button id="callback-button">CALLBACK</button>
19+
20+
<script type="application/javascript">
21+
streamsql.init('0000-0000-0000-0000'); // create client with apiKey
22+
23+
// test setting a user id and persisting to next page
24+
document.getElementById('login-button').addEventListener('click', () => {
25+
streamsql.identify(
26+
document.getElementById('user-input').value
27+
); // tie a user id to events
28+
streamsql.sendEvent('mystream'); // send an event to check user id
29+
});
30+
31+
// test onSent callback
32+
document.getElementById('callback-button').addEventListener('click', () => {
33+
streamsql.sendEvent('mystream', {}, function (status, respTxt) {
34+
console.log(status)
35+
});
36+
});
37+
38+
// check general data being sent and multiple sends ok
39+
var count = 0;
40+
document.getElementById('count-button').addEventListener('click', () => {
41+
count += 1;
42+
streamsql.sendEvent(
43+
'clickstream',
44+
{event: 'clicked', count: count}
45+
);
46+
});
47+
</script>
48+
</body>
49+
</html>

integration/fixtures/server.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const express = require('express')
2+
const https = require('https')
3+
const fs = require('fs')
4+
const config = require('./config')
5+
6+
const { port, httpsPort } = config
7+
8+
const urlHttp = `http://localhost:${port}`
9+
const urlHttps = `https://wildcard.localhost.com:${httpsPort}`
10+
11+
const app = express()
12+
app.use(function(req, res, next) {
13+
res.header('Access-Control-Allow-Origin', urlHttp)
14+
res.header('Access-Control-Allow-Credentials', 'true')
15+
res.header(
16+
'Access-Control-Allow-Headers',
17+
'Origin, X-StreamSQL-Key, Content-Type, Accept'
18+
)
19+
next()
20+
})
21+
app.use(express.json()) // body parser
22+
app.use(express.static(__dirname)) // serve all files from ./
23+
app.get('/favicon.ico', function(req, res) {
24+
res.sendStatus(200)
25+
})
26+
app.post(config.apiEndpoint, function(req, res) {
27+
if (!req.header('x-streamsql-key')) {
28+
res.sendStatus(403)
29+
} else {
30+
res.status(200).send(req.body) // echo the request data
31+
}
32+
})
33+
app.post(config.apiEndpointBatch, function(req, res) {
34+
if (!req.header('x-streamsql-key')) {
35+
res.sendStatus(403)
36+
} else {
37+
res.status(200).send(req.body) // echo the request data
38+
}
39+
})
40+
app.listen(port)
41+
console.log(`Listening on ${urlHttp}`)
42+
43+
if (!!process.env.USE_SSL) {
44+
const options = {
45+
key: fs.readFileSync(`${__dirname}/wildcard.localhost.com/key.pem`),
46+
cert: fs.readFileSync(`${__dirname}/wildcard.localhost.com/cert.pem`),
47+
}
48+
https.createServer(options, app).listen(httpsPort)
49+
console.log(`Listening with https at ${urlHttps}`)
50+
}

integration/fixtures/streamsql.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)