@vidovo/pixel-js 0.0.0-stage → 0.1.0
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.
- package/LICENSE +21 -0
- package/README.md +71 -2
- package/dist/index.d.ts +94 -0
- package/dist/index.js +100 -0
- package/package.json +30 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vidovo
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Vidovo pixel for Node.js
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Send conversions, refunds and cancellations from your server to the Vidovo pixel.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @vidovo/pixel-js
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Node.js 20.19 or newer, with `import` or `require`. No dependencies.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import { Pixel } from '@vidovo/pixel-js';
|
|
15
|
+
|
|
16
|
+
const pixel = new Pixel(process.env.VIDOVO_PIXEL_SECRET_KEY);
|
|
17
|
+
|
|
18
|
+
await pixel.conversion({
|
|
19
|
+
event_name: 'purchase',
|
|
20
|
+
order_id: '5001',
|
|
21
|
+
value: 80.0,
|
|
22
|
+
currency: 'USD',
|
|
23
|
+
vidovo: order.vidovo, // the text vidovo('checkout') gave your page
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Everything refunded on the order so far
|
|
27
|
+
await pixel.refund({ order_id: '5001', refunded_total: 30.0, currency: 'USD' });
|
|
28
|
+
|
|
29
|
+
await pixel.cancellation({ order_id: '5001' });
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The `vidovo` field can be read on your server instead. The pixel keeps it in cookies on your site, so read it off a request the visitor's browser makes to your site, like your checkout, and save it with the order:
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { vidovoFromCookies } from '@vidovo/pixel-js';
|
|
36
|
+
|
|
37
|
+
order.vidovo = vidovoFromCookies(req.headers.cookie); // Express
|
|
38
|
+
order.vidovo = vidovoFromCookies(request.headers.get('cookie')); // Next.js, Hono, Workers
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
It is undefined when the visitor is not tracked. A webhook or a background job has no cookies, so read it while the visitor is there.
|
|
42
|
+
|
|
43
|
+
Several events in one request:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
await pixel.send([
|
|
47
|
+
{ type: 'conversion', event_name: 'sign_up', event_id: 'signup-7', customer_id: '7', vidovo },
|
|
48
|
+
{ type: 'cancellation', order_id: '5002' },
|
|
49
|
+
]);
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Errors
|
|
53
|
+
|
|
54
|
+
An answer other than success throws a `PixelError`, with the message Vidovo sent. A request that gets no answer throws `fetch`'s own error.
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
import { PixelError } from '@vidovo/pixel-js';
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
await pixel.conversion(conversion);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (error instanceof PixelError && error.retryAfter) {
|
|
63
|
+
// 429 or 503: send it again after error.retryAfter seconds
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Options
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
new Pixel(secretKey, { baseUrl: 'https://px.vidovo.com', timeout: 10_000 });
|
|
72
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/** Where the Vidovo pixel takes events from your server. */
|
|
2
|
+
export declare const DEFAULT_BASE_URL = "https://px.vidovo.com";
|
|
3
|
+
/** Your own ID for something: text, or a whole number of up to 15 digits. */
|
|
4
|
+
export type Id = string | number;
|
|
5
|
+
/** A field that can be left out. `null` and `undefined` both leave it out. */
|
|
6
|
+
type Maybe<T> = T | null | undefined;
|
|
7
|
+
/** A sale, a sign-up, a booked call: any event your pixel lists. */
|
|
8
|
+
export interface Conversion {
|
|
9
|
+
/** The event's name as your pixel lists it, like `purchase` or `booked_call`. */
|
|
10
|
+
event_name: string;
|
|
11
|
+
/** Your order number. A conversion needs this or `event_id`. */
|
|
12
|
+
order_id?: Maybe<Id>;
|
|
13
|
+
/** Your own ID for this event, the same one your page passes to `track`. */
|
|
14
|
+
event_id?: Maybe<Id>;
|
|
15
|
+
/** The amount before tax and shipping, like `80.00`. Sent with `currency`. */
|
|
16
|
+
value?: Maybe<number | string>;
|
|
17
|
+
/** The three-letter currency code, like `USD`. */
|
|
18
|
+
currency?: Maybe<string>;
|
|
19
|
+
/** Your own ID for the customer's account. Never an email address. */
|
|
20
|
+
customer_id?: Maybe<Id>;
|
|
21
|
+
/** The discount codes used, up to 10. */
|
|
22
|
+
discount_codes?: Maybe<string[]>;
|
|
23
|
+
/** The text `vidovo('checkout')` gave your page, unchanged. */
|
|
24
|
+
vidovo?: Maybe<string>;
|
|
25
|
+
/** The visitor ID from `vidovo('visitor', ...)`, in place of `vidovo`. */
|
|
26
|
+
visitor_id?: Maybe<string>;
|
|
27
|
+
/** The visit ID from `vidovo('visitor', ...)`, sent with `visitor_id`. */
|
|
28
|
+
visit_id?: Maybe<string>;
|
|
29
|
+
/** When it happened, up to 7 days ago. Left out, it is when Vidovo receives it. */
|
|
30
|
+
happened_at?: Maybe<Date | string>;
|
|
31
|
+
}
|
|
32
|
+
/** Money that went back to the buyer of an order you reported. */
|
|
33
|
+
export interface Refund {
|
|
34
|
+
/** The order, written the way you sent it with the sale. */
|
|
35
|
+
order_id: Id;
|
|
36
|
+
/** Everything refunded on the order so far, not the amount of this one refund. */
|
|
37
|
+
refunded_total: number | string;
|
|
38
|
+
/** The currency of `refunded_total`. Left out, it is the order's own. */
|
|
39
|
+
currency?: Maybe<string>;
|
|
40
|
+
/** The event the order was sent as, like `purchase`. Left out, it is every event with this order ID. */
|
|
41
|
+
event_name?: Maybe<string>;
|
|
42
|
+
}
|
|
43
|
+
/** An order you reported that was cancelled. */
|
|
44
|
+
export interface Cancellation {
|
|
45
|
+
/** The order, written the way you sent it with the sale. */
|
|
46
|
+
order_id: Id;
|
|
47
|
+
/** The event the order was sent as, like `purchase`. Left out, it is every event with this order ID. */
|
|
48
|
+
event_name?: Maybe<string>;
|
|
49
|
+
}
|
|
50
|
+
/** One event of a request that can hold several. */
|
|
51
|
+
export type PixelEvent = ({
|
|
52
|
+
type: 'conversion';
|
|
53
|
+
} & Conversion) | ({
|
|
54
|
+
type: 'refund';
|
|
55
|
+
} & Refund) | ({
|
|
56
|
+
type: 'cancellation';
|
|
57
|
+
} & Cancellation);
|
|
58
|
+
export interface PixelOptions {
|
|
59
|
+
/** Another address than `DEFAULT_BASE_URL`. */
|
|
60
|
+
baseUrl?: string;
|
|
61
|
+
/** How long to wait for an answer, in milliseconds. 10 seconds when left out. */
|
|
62
|
+
timeout?: number;
|
|
63
|
+
}
|
|
64
|
+
/** An answer from Vidovo other than success. */
|
|
65
|
+
export declare class PixelError extends Error {
|
|
66
|
+
name: string;
|
|
67
|
+
/** The HTTP status Vidovo answered with. */
|
|
68
|
+
readonly status: number;
|
|
69
|
+
/** On a 429 or a 503, the seconds to wait before sending the same request again. */
|
|
70
|
+
readonly retryAfter: number | undefined;
|
|
71
|
+
constructor(message: string, status: number, retryAfter?: number);
|
|
72
|
+
}
|
|
73
|
+
/** Sends events to the Vidovo pixel with the pixel's secret key. */
|
|
74
|
+
export declare class Pixel {
|
|
75
|
+
#private;
|
|
76
|
+
/** @param secretKey The pixel's secret key, the one that starts with `vdv_sk_`. */
|
|
77
|
+
constructor(secretKey: string, options?: PixelOptions);
|
|
78
|
+
conversion(conversion: Conversion): Promise<void>;
|
|
79
|
+
refund(refund: Refund): Promise<void>;
|
|
80
|
+
cancellation(cancellation: Cancellation): Promise<void>;
|
|
81
|
+
/** Sends events in one request. Vidovo takes all of them or none. */
|
|
82
|
+
send(events: PixelEvent[]): Promise<void>;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The `vidovo` field of a conversion, read from the cookies the pixel keeps on
|
|
86
|
+
* your site. Pass the Cookie header of a request the visitor's browser made to
|
|
87
|
+
* your site, like your checkout. Undefined when the browser has no visitor, or
|
|
88
|
+
* tracking is off for it.
|
|
89
|
+
*
|
|
90
|
+
* It is the text `vidovo('checkout')` gives the page, so a page needs no code
|
|
91
|
+
* to send it.
|
|
92
|
+
*/
|
|
93
|
+
export declare function vidovoFromCookies(cookieHeader: string | null | undefined): string | undefined;
|
|
94
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/** Where the Vidovo pixel takes events from your server. */
|
|
2
|
+
export const DEFAULT_BASE_URL = 'https://px.vidovo.com';
|
|
3
|
+
/** An answer from Vidovo other than success. */
|
|
4
|
+
export class PixelError extends Error {
|
|
5
|
+
name = 'PixelError';
|
|
6
|
+
/** The HTTP status Vidovo answered with. */
|
|
7
|
+
status;
|
|
8
|
+
/** On a 429 or a 503, the seconds to wait before sending the same request again. */
|
|
9
|
+
retryAfter;
|
|
10
|
+
constructor(message, status, retryAfter) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.status = status;
|
|
13
|
+
this.retryAfter = retryAfter;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Sends events to the Vidovo pixel with the pixel's secret key. */
|
|
17
|
+
export class Pixel {
|
|
18
|
+
#secretKey;
|
|
19
|
+
#url;
|
|
20
|
+
#timeout;
|
|
21
|
+
/** @param secretKey The pixel's secret key, the one that starts with `vdv_sk_`. */
|
|
22
|
+
constructor(secretKey, options = {}) {
|
|
23
|
+
this.#secretKey = secretKey;
|
|
24
|
+
this.#url = `${(options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, '')}/v1/server/events`;
|
|
25
|
+
this.#timeout = options.timeout ?? 10_000;
|
|
26
|
+
}
|
|
27
|
+
conversion(conversion) {
|
|
28
|
+
return this.send([{ ...conversion, type: 'conversion' }]);
|
|
29
|
+
}
|
|
30
|
+
refund(refund) {
|
|
31
|
+
return this.send([{ ...refund, type: 'refund' }]);
|
|
32
|
+
}
|
|
33
|
+
cancellation(cancellation) {
|
|
34
|
+
return this.send([{ ...cancellation, type: 'cancellation' }]);
|
|
35
|
+
}
|
|
36
|
+
/** Sends events in one request. Vidovo takes all of them or none. */
|
|
37
|
+
async send(events) {
|
|
38
|
+
const response = await fetch(this.#url, {
|
|
39
|
+
method: 'POST',
|
|
40
|
+
headers: {
|
|
41
|
+
Authorization: `Bearer ${this.#secretKey}`,
|
|
42
|
+
'Content-Type': 'application/json',
|
|
43
|
+
},
|
|
44
|
+
body: JSON.stringify({ events }),
|
|
45
|
+
signal: AbortSignal.timeout(this.#timeout),
|
|
46
|
+
});
|
|
47
|
+
if (response.ok)
|
|
48
|
+
return;
|
|
49
|
+
const answer = await response.json().catch(() => null);
|
|
50
|
+
const message = answer && typeof answer === 'object' && 'error' in answer && typeof answer.error === 'string'
|
|
51
|
+
? answer.error
|
|
52
|
+
: `Vidovo answered ${response.status}`;
|
|
53
|
+
const retryAfter = Number(response.headers.get('Retry-After')) || undefined;
|
|
54
|
+
throw new PixelError(message, response.status, retryAfter);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const UUID = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
|
|
58
|
+
/**
|
|
59
|
+
* A visitor as the pixel keeps them in a cookie: who they are and their visit,
|
|
60
|
+
* ending in when they were last seen (group 2), then the last Vidovo link they
|
|
61
|
+
* followed with when (group 3, its time in group 4).
|
|
62
|
+
*/
|
|
63
|
+
const VISITOR_COOKIE = new RegExp(`^(1\\.${UUID}\\.[1-9]\\d{0,8}\\.${UUID}\\.\\d{1,9}\\.(\\d{1,15}))` +
|
|
64
|
+
`(?:\\.((?:[mta]\\d{1,40}|c[\\w-]{1,64})\\.(\\d{1,15})))?$`);
|
|
65
|
+
/**
|
|
66
|
+
* The `vidovo` field of a conversion, read from the cookies the pixel keeps on
|
|
67
|
+
* your site. Pass the Cookie header of a request the visitor's browser made to
|
|
68
|
+
* your site, like your checkout. Undefined when the browser has no visitor, or
|
|
69
|
+
* tracking is off for it.
|
|
70
|
+
*
|
|
71
|
+
* It is the text `vidovo('checkout')` gives the page, so a page needs no code
|
|
72
|
+
* to send it.
|
|
73
|
+
*/
|
|
74
|
+
export function vidovoFromCookies(cookieHeader) {
|
|
75
|
+
const values = (name) => (cookieHeader ?? '')
|
|
76
|
+
.split(';')
|
|
77
|
+
.map((pair) => pair.trim())
|
|
78
|
+
.filter((pair) => pair.startsWith(`${name}=`))
|
|
79
|
+
.map((pair) => pair.slice(name.length + 1));
|
|
80
|
+
if (values('_vdv_off').some(Boolean))
|
|
81
|
+
return undefined;
|
|
82
|
+
// The pixel keeps the visitor twice, and the second copy can be a page
|
|
83
|
+
// behind. It takes the copy seen last, and the link followed last.
|
|
84
|
+
let newest;
|
|
85
|
+
let link;
|
|
86
|
+
for (const value of [...values('_vdv_visitor'), ...values('_vdv_kept')]) {
|
|
87
|
+
const copy = VISITOR_COOKIE.exec(value);
|
|
88
|
+
if (!copy)
|
|
89
|
+
continue;
|
|
90
|
+
if (!newest || Number(copy[2]) > Number(newest[2]))
|
|
91
|
+
newest = copy;
|
|
92
|
+
if (copy[3] && (!link || Number(copy[4]) > Number(link[4])))
|
|
93
|
+
link = copy;
|
|
94
|
+
}
|
|
95
|
+
if (!newest)
|
|
96
|
+
return undefined;
|
|
97
|
+
// vidovo('checkout') puts the time it was written in front, by the
|
|
98
|
+
// visitor's clock. The time they were last seen is that time here.
|
|
99
|
+
return `${newest[2]}.${newest[1]}${link ? `.${link[3]}` : ''}`;
|
|
100
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vidovo/pixel-js",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Send conversions, refunds and cancellations from your server to the Vidovo pixel.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"publishConfig": {
|
|
14
|
+
"access": "public"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc --project tsconfig.build.json",
|
|
24
|
+
"typecheck": "tsc",
|
|
25
|
+
"test": "node --test \"test/*.test.ts\"",
|
|
26
|
+
"prepublishOnly": "npm run typecheck && npm test && npm run build"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "22.20.5",
|
|
30
|
+
"typescript": "7.0.2"
|
|
31
|
+
}
|
|
32
|
+
}
|