@inspira-npm/events-react 0.1.0 → 0.3.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/dist/index.d.ts +12 -2
- package/dist/index.js +30 -8
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import { AdClickProps, AdClickProps as AdClickProps$1, BrowserEventName, CLICK_ID_PARAM, CONSENT_CHANGE_EVENT, ConsentState, DEFAULT_CONSENT_COOKIE, EventName, EventProps, EventProps as EventProps$1, ImpressionEventName, ImpressionEventName as ImpressionEventName$1, ImpressionOptions, ImpressionOptions as ImpressionOptions$1, Tracker, Tracker as Tracker$1, TrackerConfig, cookieConsent, createNoopTracker, createTracker, isClickId, parseConsentValue, readClickIdFromUrl, withClickId } from "@inspira-npm/events";
|
|
2
|
+
import { AdClickProps, AdClickProps as AdClickProps$1, BrowserEventName, CLICK_ID_PARAM, CONSENT_CHANGE_EVENT, ClickRecordOptions, ConsentState, DEFAULT_CONSENT_COOKIE, EventName, EventProps, EventProps as EventProps$1, ImpressionEventName, ImpressionEventName as ImpressionEventName$1, ImpressionOptions, ImpressionOptions as ImpressionOptions$1, OutboundClickProps, OutboundClickProps as OutboundClickProps$1, Tracker, Tracker as Tracker$1, TrackerConfig, cookieConsent, createNoopTracker, createTracker, isClickId, parseConsentValue, readClickIdFromUrl, withClickId, withTrackingParams } from "@inspira-npm/events";
|
|
3
3
|
import { ReactElement, ReactNode } from "react";
|
|
4
4
|
interface EventsProviderProps {
|
|
5
5
|
/** Tracker creado con `createTracker` (a nivel de módulo, para que no cambie entre renders). */
|
|
@@ -41,4 +41,14 @@ export declare function useClickIdCapture(): void;
|
|
|
41
41
|
* `props` y `options` pueden ser literales nuevos en cada render: se comparan por valor.
|
|
42
42
|
*/
|
|
43
43
|
export declare function useImpression<N extends ImpressionEventName$1>(name: N, props: EventProps$1<N>, options?: ImpressionOptions$1): (el: Element | null) => void;
|
|
44
|
-
|
|
44
|
+
/** Mismo contrato que en `useAdClick`. */
|
|
45
|
+
type OutboundClickEvent = AdClickEvent;
|
|
46
|
+
type OutboundClickHandlers = AdClickHandlers;
|
|
47
|
+
/**
|
|
48
|
+
* Props para el `<a>` de un link o CTA medido hacia otro sitio o página (p. ej. el CTA de una
|
|
49
|
+
* landing que lleva al sitio principal): `<a {...useOutboundClick(props, url)}>`. Igual que
|
|
50
|
+
* `useAdClick`: el `href` del render es la URL tal cual y al hacer click registra
|
|
51
|
+
* `outbound_click` y reescribe el `href` con `?ipc=` antes de que el navegador navegue.
|
|
52
|
+
*/
|
|
53
|
+
export declare function useOutboundClick(props: OutboundClickProps$1, url: string): OutboundClickHandlers;
|
|
54
|
+
export { type AdClickEvent, type AdClickHandlers, type AdClickProps, type BrowserEventName, CLICK_ID_PARAM, CONSENT_CHANGE_EVENT, type ClickRecordOptions, type ConsentState, DEFAULT_CONSENT_COOKIE, type EventName, type EventProps, type EventsProviderProps, type ImpressionEventName, type ImpressionOptions, type OutboundClickEvent, type OutboundClickHandlers, type OutboundClickProps, type Tracker, type TrackerConfig, cookieConsent, createNoopTracker, createTracker, isClickId, parseConsentValue, readClickIdFromUrl, withClickId, withTrackingParams };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { CLICK_ID_PARAM, CONSENT_CHANGE_EVENT, DEFAULT_CONSENT_COOKIE, cookieConsent, createNoopTracker, createNoopTracker as createNoopTracker$1, createTracker, isClickId, parseConsentValue, readClickIdFromUrl, withClickId } from "@inspira-npm/events";
|
|
2
|
+
import { CLICK_ID_PARAM, CONSENT_CHANGE_EVENT, DEFAULT_CONSENT_COOKIE, cookieConsent, createNoopTracker, createNoopTracker as createNoopTracker$1, createTracker, isClickId, parseConsentValue, readClickIdFromUrl, withClickId, withTrackingParams } from "@inspira-npm/events";
|
|
3
3
|
import { createContext, createElement, useCallback, useContext, useEffect, useMemo, useRef } from "react";
|
|
4
4
|
// src/provider.ts
|
|
5
5
|
const TrackerContext = createContext(null);
|
|
@@ -33,22 +33,24 @@ function useTrack() {
|
|
|
33
33
|
const tracker = useTracker();
|
|
34
34
|
return useCallback((name, props) => tracker.track(name, props), [tracker]);
|
|
35
35
|
}
|
|
36
|
-
// src/
|
|
36
|
+
// src/tracked-link.ts
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
38
|
+
* Base de `useAdClick` y `useOutboundClick`: en cada click llama a `trackClick` (que registra el
|
|
39
|
+
* evento y devuelve la URL con `?ipc=`) y reescribe el `href` del `<a>` antes de que el navegador
|
|
40
|
+
* navegue. `trackClick` debe ser estable (una función de módulo).
|
|
40
41
|
*/
|
|
41
|
-
function
|
|
42
|
+
function useTrackedLink(props, url, trackClick) {
|
|
42
43
|
const tracker = useTracker();
|
|
43
44
|
const propsKey = JSON.stringify(props);
|
|
44
45
|
const onClick = useCallback((event) => {
|
|
45
|
-
const href = tracker
|
|
46
|
+
const href = trackClick(tracker, JSON.parse(propsKey), url);
|
|
46
47
|
const target = event.currentTarget;
|
|
47
48
|
if (typeof HTMLAnchorElement !== "undefined" && target instanceof HTMLAnchorElement) target.href = href;
|
|
48
49
|
}, [
|
|
49
50
|
tracker,
|
|
50
51
|
propsKey,
|
|
51
|
-
url
|
|
52
|
+
url,
|
|
53
|
+
trackClick
|
|
52
54
|
]);
|
|
53
55
|
const onAuxClick = useCallback((event) => {
|
|
54
56
|
if (event.button !== void 0 && event.button !== 1) return;
|
|
@@ -64,6 +66,15 @@ function useAdClick(props, url) {
|
|
|
64
66
|
onAuxClick
|
|
65
67
|
]);
|
|
66
68
|
}
|
|
69
|
+
// src/use-ad-click.ts
|
|
70
|
+
const trackAdClick = (tracker, props, url) => tracker.trackAdClick(props, url);
|
|
71
|
+
/**
|
|
72
|
+
* Props para el `<a>` de un ad: `<a {...useAdClick(props, url)}>`. Al hacer click registra
|
|
73
|
+
* `ad_click` y cambia el `href` del ancla a la URL con `?ipc=` antes de que el navegador navegue.
|
|
74
|
+
*/
|
|
75
|
+
function useAdClick(props, url) {
|
|
76
|
+
return useTrackedLink(props, url, trackAdClick);
|
|
77
|
+
}
|
|
67
78
|
// src/use-click-id-capture.ts
|
|
68
79
|
/**
|
|
69
80
|
* En el sitio destino de un ad: guarda el `?ipc=` de la URL para atribuir la conversión.
|
|
@@ -99,4 +110,15 @@ function useImpression(name, props, options) {
|
|
|
99
110
|
optionsKey
|
|
100
111
|
]);
|
|
101
112
|
}
|
|
102
|
-
|
|
113
|
+
// src/use-outbound-click.ts
|
|
114
|
+
const trackOutboundClick = (tracker, props, url) => tracker.trackOutboundClick(props, url);
|
|
115
|
+
/**
|
|
116
|
+
* Props para el `<a>` de un link o CTA medido hacia otro sitio o página (p. ej. el CTA de una
|
|
117
|
+
* landing que lleva al sitio principal): `<a {...useOutboundClick(props, url)}>`. Igual que
|
|
118
|
+
* `useAdClick`: el `href` del render es la URL tal cual y al hacer click registra
|
|
119
|
+
* `outbound_click` y reescribe el `href` con `?ipc=` antes de que el navegador navegue.
|
|
120
|
+
*/
|
|
121
|
+
function useOutboundClick(props, url) {
|
|
122
|
+
return useTrackedLink(props, url, trackOutboundClick);
|
|
123
|
+
}
|
|
124
|
+
export { CLICK_ID_PARAM, CONSENT_CHANGE_EVENT, DEFAULT_CONSENT_COOKIE, EventsProvider, cookieConsent, createNoopTracker, createTracker, isClickId, parseConsentValue, readClickIdFromUrl, useAdClick, useClickIdCapture, useImpression, useOutboundClick, useTrack, useTracker, withClickId, withTrackingParams };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inspira-npm/events-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Provider y hooks de React para @inspira-npm/events.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"prepublishOnly": "npm run typecheck && npm run test && npm run build"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@inspira-npm/events": "^0.
|
|
27
|
+
"@inspira-npm/events": "^0.3.0"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"react": ">=18"
|