Skip to content

Commit

Permalink
chore: bumps topsort sdk to 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
barbmarcio committed Sep 5, 2024
1 parent 3ca89f6 commit ae39c11
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@
"vitest": "^2.0.5"
},
"dependencies": {
"@topsort/sdk": "^0.2.0"
"@topsort/sdk": "^0.3.1"
}
}
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 36 additions & 11 deletions src/detector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { type Config, Entity, TopsortEvent, reportEvent } from "@topsort/sdk";
import {
TopsortClient,
type Config,
Entity,
Event as TopsortEvent,
} from "@topsort/sdk";
import { version } from "../package.json";
import { ProcessorResult, Queue } from "./queue";
import { truncateSet } from "./set";
Expand All @@ -17,7 +22,10 @@ const bidStore = new BidStore("ts-b");
* just be a random number;
*/
function generateId(): string {
return window.URL.createObjectURL?.(new Blob()).split("/").pop() || Math.random() + "";
return (
window.URL.createObjectURL?.(new Blob()).split("/").pop() ||
Math.random() + ""
);
}

let globalUserId: string | undefined;
Expand Down Expand Up @@ -138,16 +146,18 @@ async function processor(data: ProductEvent[]): Promise<ProcessorResult> {
host: window.TS.url,
userAgent: `ts.js/${version}`,
};
const topsortClient = new TopsortClient(config);
for (const entry of data) {
promises.push(
reportEvent(config, getApiPayload(entry))
topsortClient
.reportEvent(getApiPayload(entry))
.then((result) => {
const q = result.retry ? r.retry : r.done;
q.add(entry.id);
})
.catch(() => {
r.done.add(entry.id);
}),
})
);
}
await Promise.all(promises);
Expand Down Expand Up @@ -193,9 +203,13 @@ function logEvent(info: ProductEvent, node: Node) {

function getId(event: ProductEvent): string {
const items = JSON.stringify(event.items || []);
return [event.page, event.type, event.product ?? event.additionalProduct, event.bid, items].join(
"-",
);
return [
event.page,
event.type,
event.product ?? event.additionalProduct,
event.bid,
items,
].join("-");
}

function getPage(): string {
Expand All @@ -210,7 +224,11 @@ function getEvent(type: EventType, node: HTMLElement): ProductEvent {
let product = node.dataset.tsProduct;
let bid = node.dataset.tsResolvedBid;
let additionalProduct: string | undefined = undefined;
if (bid == "inherit" && product && (type == "Click" || type == "Impression")) {
if (
bid == "inherit" &&
product &&
(type == "Click" || type == "Impression")
) {
bid = bidStore.get();
additionalProduct = product;
product = undefined;
Expand Down Expand Up @@ -262,7 +280,7 @@ const intersectionObserver = !!window.IntersectionObserver
},
{
threshold: INTERSECTION_THRESHOLD,
},
}
)
: undefined;

Expand Down Expand Up @@ -341,13 +359,20 @@ function start() {
}
checkChildren(document);
const MutationObserverImpl =
window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
window.MutationObserver ||
window.WebKitMutationObserver ||
window.MozMutationObserver;
const mutationObserver = new MutationObserverImpl(mutationCallback);
mutationObserver.observe(document, {
attributes: true,
childList: true,
subtree: true,
attributeFilter: ["data-ts-product", "data-ts-action", "data-ts-items", "data-ts-resolved-bid"],
attributeFilter: [
"data-ts-product",
"data-ts-action",
"data-ts-items",
"data-ts-resolved-bid",
],
});
}

Expand Down

0 comments on commit ae39c11

Please sign in to comment.