@vidovo/pixel-js 0.1.0 → 0.2.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/README.md +49 -17
- package/dist/index.d.ts +4 -1
- package/dist/index.js +12 -1
- package/package.json +16 -1
package/README.md
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/vidovo/pixel-js/main/docs/image.png" alt="Vidovo pixel for Node.js" width="100%">
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://www.npmjs.com/package/@vidovo/pixel-js"><img src="https://img.shields.io/npm/v/@vidovo/pixel-js?color=2e9a6b" alt="npm"></a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/@vidovo/pixel-js"><img src="https://img.shields.io/node/v/@vidovo/pixel-js?color=5FA04E" alt="Node.js"></a>
|
|
8
|
+
<a href="https://www.npmjs.com/package/@vidovo/pixel-js"><img src="https://img.shields.io/npm/types/@vidovo/pixel-js?color=3178C6" alt="Types"></a>
|
|
9
|
+
<a href="https://www.npmjs.com/package/@vidovo/pixel-js"><img src="https://img.shields.io/npm/unpacked-size/@vidovo/pixel-js?color=55635f" alt="Size"></a>
|
|
10
|
+
<a href="https://github.com/vidovo/pixel-js/actions/workflows/ci.yml"><img src="https://github.com/vidovo/pixel-js/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
|
|
11
|
+
<a href="LICENSE"><img src="https://img.shields.io/npm/l/@vidovo/pixel-js?color=55635f" alt="License"></a>
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
Send conversions, refunds and cancellations from your server to the Vidovo pixel. A sale your server sends is confirmed, and it still counts when the buyer's browser blocks the pixel or closes too soon.
|
|
15
|
+
|
|
16
|
+
## Install
|
|
4
17
|
|
|
5
18
|
```bash
|
|
6
19
|
npm install @vidovo/pixel-js
|
|
7
20
|
```
|
|
8
21
|
|
|
9
|
-
Node.js 20.19 or newer, with `import` or `require`. No dependencies.
|
|
22
|
+
Node.js 20.19 or newer, with `import` or `require`. TypeScript types included. No dependencies.
|
|
10
23
|
|
|
11
24
|
## Usage
|
|
12
25
|
|
|
@@ -20,35 +33,50 @@ await pixel.conversion({
|
|
|
20
33
|
order_id: '5001',
|
|
21
34
|
value: 80.0,
|
|
22
35
|
currency: 'USD',
|
|
23
|
-
vidovo: order.vidovo, // the
|
|
36
|
+
vidovo: order.vidovo, // who the buyer was, saved at checkout
|
|
24
37
|
});
|
|
38
|
+
```
|
|
25
39
|
|
|
26
|
-
|
|
40
|
+
### Refunds and cancellations
|
|
41
|
+
|
|
42
|
+
`refunded_total` is everything refunded on the order so far.
|
|
43
|
+
|
|
44
|
+
```js
|
|
27
45
|
await pixel.refund({ order_id: '5001', refunded_total: 30.0, currency: 'USD' });
|
|
28
46
|
|
|
29
|
-
await pixel.cancellation({ order_id: '
|
|
47
|
+
await pixel.cancellation({ order_id: '5002' });
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`send` takes several events and sends them in one request. Vidovo takes all of them or none.
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
await pixel.send([
|
|
54
|
+
{ type: 'conversion', event_name: 'sign_up', event_id: 'signup-7', customer_id: '7', vidovo },
|
|
55
|
+
{ type: 'cancellation', order_id: '5002' },
|
|
56
|
+
]);
|
|
30
57
|
```
|
|
31
58
|
|
|
32
|
-
|
|
59
|
+
### The buyer
|
|
60
|
+
|
|
61
|
+
A sale is credited to the Vidovo link that brought its buyer. The pixel keeps the buyer in cookies on your site, so read them off a request the buyer's browser makes, like your checkout, and save the result with the order:
|
|
33
62
|
|
|
34
63
|
```js
|
|
35
64
|
import { vidovoFromCookies } from '@vidovo/pixel-js';
|
|
36
65
|
|
|
37
|
-
order.vidovo = vidovoFromCookies(req.headers.cookie); // Express
|
|
38
|
-
order.vidovo = vidovoFromCookies(request.headers.get('cookie')); // Next.js, Hono
|
|
66
|
+
order.vidovo = vidovoFromCookies(req.headers.cookie); // Express, Fastify
|
|
67
|
+
order.vidovo = vidovoFromCookies(request.headers.get('cookie')); // Next.js, Hono
|
|
39
68
|
```
|
|
40
69
|
|
|
41
|
-
It is undefined when the
|
|
70
|
+
It is `undefined` when the buyer is not tracked. A payment webhook or a background job has no cookies, so read it while the buyer is there.
|
|
42
71
|
|
|
43
|
-
|
|
72
|
+
If you get the pixel's cookie some other way, pass the `_vdv_visitor` value in `vidovo` as it is, and the SDK turns it into what Vidovo reads:
|
|
44
73
|
|
|
45
74
|
```js
|
|
46
|
-
await pixel.
|
|
47
|
-
{ type: 'conversion', event_name: 'sign_up', event_id: 'signup-7', customer_id: '7', vidovo },
|
|
48
|
-
{ type: 'cancellation', order_id: '5002' },
|
|
49
|
-
]);
|
|
75
|
+
await pixel.conversion({ event_name: 'purchase', order_id: '5001', vidovo: order.vdvVisitorCookie });
|
|
50
76
|
```
|
|
51
77
|
|
|
78
|
+
`vidovo` also takes the text `vidovo('checkout')` gives your page.
|
|
79
|
+
|
|
52
80
|
## Errors
|
|
53
81
|
|
|
54
82
|
An answer other than success throws a `PixelError`, with the message Vidovo sent. A request that gets no answer throws `fetch`'s own error.
|
|
@@ -60,7 +88,7 @@ try {
|
|
|
60
88
|
await pixel.conversion(conversion);
|
|
61
89
|
} catch (error) {
|
|
62
90
|
if (error instanceof PixelError && error.retryAfter) {
|
|
63
|
-
// 429 or 503: send
|
|
91
|
+
// 429 or 503: send the same request again after error.retryAfter seconds
|
|
64
92
|
}
|
|
65
93
|
}
|
|
66
94
|
```
|
|
@@ -68,5 +96,9 @@ try {
|
|
|
68
96
|
## Options
|
|
69
97
|
|
|
70
98
|
```js
|
|
71
|
-
new Pixel(secretKey, { baseUrl: 'https://px.vidovo.com', timeout: 10_000 });
|
|
99
|
+
const pixel = new Pixel(secretKey, { baseUrl: 'https://px.vidovo.com', timeout: 10_000 });
|
|
72
100
|
```
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
[MIT](LICENSE)
|
package/dist/index.d.ts
CHANGED
|
@@ -20,7 +20,10 @@ export interface Conversion {
|
|
|
20
20
|
customer_id?: Maybe<Id>;
|
|
21
21
|
/** The discount codes used, up to 10. */
|
|
22
22
|
discount_codes?: Maybe<string[]>;
|
|
23
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* The buyer: what `vidovoFromCookies` read, the text `vidovo('checkout')` gave
|
|
25
|
+
* your page, or the pixel's `_vdv_visitor` cookie value as it is.
|
|
26
|
+
*/
|
|
24
27
|
vidovo?: Maybe<string>;
|
|
25
28
|
/** The visitor ID from `vidovo('visitor', ...)`, in place of `vidovo`. */
|
|
26
29
|
visitor_id?: Maybe<string>;
|
package/dist/index.js
CHANGED
|
@@ -41,7 +41,7 @@ export class Pixel {
|
|
|
41
41
|
Authorization: `Bearer ${this.#secretKey}`,
|
|
42
42
|
'Content-Type': 'application/json',
|
|
43
43
|
},
|
|
44
|
-
body: JSON.stringify({ events }),
|
|
44
|
+
body: JSON.stringify({ events: events.map(withBuyer) }),
|
|
45
45
|
signal: AbortSignal.timeout(this.#timeout),
|
|
46
46
|
});
|
|
47
47
|
if (response.ok)
|
|
@@ -98,3 +98,14 @@ export function vidovoFromCookies(cookieHeader) {
|
|
|
98
98
|
// visitor's clock. The time they were last seen is that time here.
|
|
99
99
|
return `${newest[2]}.${newest[1]}${link ? `.${link[3]}` : ''}`;
|
|
100
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* A conversion as Vidovo reads it. The pixel's cookie value, passed in `vidovo`
|
|
103
|
+
* as it is, gets the time the buyer was last seen in front, as
|
|
104
|
+
* `vidovoFromCookies` writes it.
|
|
105
|
+
*/
|
|
106
|
+
function withBuyer(event) {
|
|
107
|
+
if (event.type !== 'conversion' || typeof event.vidovo !== 'string')
|
|
108
|
+
return event;
|
|
109
|
+
const kept = VISITOR_COOKIE.exec(event.vidovo);
|
|
110
|
+
return kept ? { ...event, vidovo: `${kept[2]}.${event.vidovo}` } : event;
|
|
111
|
+
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vidovo/pixel-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Send conversions, refunds and cancellations from your server to the Vidovo pixel.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"vidovo",
|
|
8
|
+
"pixel",
|
|
9
|
+
"conversions",
|
|
10
|
+
"server-side tracking",
|
|
11
|
+
"attribution"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/vidovo/pixel-js#readme",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/vidovo/pixel-js.git"
|
|
17
|
+
},
|
|
18
|
+
"bugs": "https://github.com/vidovo/pixel-js/issues",
|
|
6
19
|
"type": "module",
|
|
20
|
+
"main": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
7
22
|
"exports": {
|
|
8
23
|
".": {
|
|
9
24
|
"types": "./dist/index.d.ts",
|