@skhema/embed 0.1.1 → 0.1.3

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.cjs CHANGED
@@ -265,10 +265,10 @@ class AnalyticsBatcher {
265
265
  }
266
266
  async sendComponentEmbeds(embeds) {
267
267
  if (embeds.length === 0) return;
268
- try {
269
- const payload = {
270
- action: "component_embed",
271
- events: embeds.map((embed) => ({
268
+ for (const embed of embeds) {
269
+ try {
270
+ const payload = {
271
+ action: "component_embed",
272
272
  contributor_id: embed.contributorId,
273
273
  component_type: embed.componentType,
274
274
  component_hash: embed.componentHash,
@@ -281,11 +281,11 @@ class AnalyticsBatcher {
281
281
  page_url: embed.pageUrl,
282
282
  page_title: embed.pageTitle || "",
283
283
  user_agent: embed.userAgent || ""
284
- }))
285
- };
286
- await sendWithRetry(ANALYTICS_ENDPOINT, payload, "json");
287
- } catch (error) {
288
- console.debug("Component embed tracking failed:", error);
284
+ };
285
+ await sendWithRetry(ANALYTICS_ENDPOINT, payload, "json");
286
+ } catch (error) {
287
+ console.debug("Component embed tracking failed:", error);
288
+ }
289
289
  }
290
290
  }
291
291
  async sendComponentClicks(clicks) {
@@ -312,11 +312,90 @@ class AnalyticsBatcher {
312
312
  }
313
313
  }
314
314
  }
315
- // Ensure batch is sent when page unloads
315
+ // Ensure batch is sent when page unloads.
316
+ // Uses sendBeacon for reliability — async fetch won't complete during unload.
316
317
  flush() {
317
- const hasItems = this.batch.embeds.length > 0 || this.batch.clicks.length > 0 || this.batch.componentEmbeds.length > 0 || this.batch.componentClicks.length > 0;
318
- if (hasItems) {
319
- this.sendBatch();
318
+ if (this.batchTimeout) {
319
+ clearTimeout(this.batchTimeout);
320
+ this.batchTimeout = null;
321
+ }
322
+ const currentBatch = { ...this.batch };
323
+ this.batch = {
324
+ embeds: [],
325
+ clicks: [],
326
+ componentEmbeds: [],
327
+ componentClicks: []
328
+ };
329
+ if (currentBatch.embeds.length > 0) {
330
+ const payload = {
331
+ action: "embed",
332
+ events: currentBatch.embeds.map((embed) => ({
333
+ contributor_id: embed.contributorId,
334
+ element_type: embed.elementType,
335
+ content_hash: embed.contentHash,
336
+ content: embed.content,
337
+ page_url: embed.pageUrl,
338
+ page_title: embed.pageTitle || "",
339
+ user_agent: embed.userAgent || ""
340
+ }))
341
+ };
342
+ this.sendViaBeacon(payload);
343
+ }
344
+ for (const click of currentBatch.clicks) {
345
+ this.sendViaBeacon({
346
+ action: "click",
347
+ contributor_id: click.contributor_id,
348
+ content_hash: click.content_hash,
349
+ source_url: click.source_url,
350
+ element_type: click.element_type
351
+ });
352
+ }
353
+ for (const embed of currentBatch.componentEmbeds) {
354
+ this.sendViaBeacon({
355
+ action: "component_embed",
356
+ contributor_id: embed.contributorId,
357
+ component_type: embed.componentType,
358
+ component_hash: embed.componentHash,
359
+ title: embed.title,
360
+ elements: embed.elements.map((el) => ({
361
+ element_type: el.elementType,
362
+ content: el.content,
363
+ content_hash: el.contentHash
364
+ })),
365
+ page_url: embed.pageUrl,
366
+ page_title: embed.pageTitle || "",
367
+ user_agent: embed.userAgent || ""
368
+ });
369
+ }
370
+ for (const click of currentBatch.componentClicks) {
371
+ this.sendViaBeacon({
372
+ action: "component_click",
373
+ contributor_id: click.contributor_id,
374
+ component_type: click.component_type,
375
+ component_hash: click.component_hash,
376
+ title: click.title,
377
+ source_url: click.source_url
378
+ });
379
+ }
380
+ }
381
+ sendViaBeacon(payload) {
382
+ try {
383
+ if (typeof navigator !== "undefined" && navigator.sendBeacon) {
384
+ navigator.sendBeacon(
385
+ ANALYTICS_ENDPOINT,
386
+ new Blob([JSON.stringify(payload)], { type: "application/json" })
387
+ );
388
+ } else {
389
+ fetch(ANALYTICS_ENDPOINT, {
390
+ method: "POST",
391
+ headers: { "Content-Type": "application/json" },
392
+ body: JSON.stringify(payload),
393
+ credentials: "omit",
394
+ keepalive: true
395
+ }).catch(() => {
396
+ });
397
+ }
398
+ } catch {
320
399
  }
321
400
  }
322
401
  }
@@ -952,6 +1031,7 @@ class SkhemaComponent extends HTMLElement {
952
1031
  return [
953
1032
  "component-type",
954
1033
  "contributor-id",
1034
+ "contributor-name",
955
1035
  "title",
956
1036
  "theme",
957
1037
  "track-analytics",
@@ -966,7 +1046,13 @@ class SkhemaComponent extends HTMLElement {
966
1046
  this.addEventListener("skhema:element-ready", () => {
967
1047
  this.scheduleRender();
968
1048
  });
969
- this.scheduleRender();
1049
+ if (typeof customElements.whenDefined === "function") {
1050
+ customElements.whenDefined("skhema-element").then(() => {
1051
+ this.scheduleRender();
1052
+ });
1053
+ } else {
1054
+ this.scheduleRender();
1055
+ }
970
1056
  this.setupThemeListeners();
971
1057
  } catch (error) {
972
1058
  this.renderError("Failed to initialize component", error);
@@ -1085,7 +1171,8 @@ class SkhemaComponent extends HTMLElement {
1085
1171
  const componentLabel = getComponentTypeLabel(component_type);
1086
1172
  const acronym = getComponentTypeAcronym(component_type);
1087
1173
  const colors = COMPONENT_COLORS[component_type];
1088
- const displayName = this.formatContributorName(contributor_id);
1174
+ const contributorName = this.getAttribute("contributor-name");
1175
+ const displayName = contributorName || this.formatContributorName(contributor_id);
1089
1176
  const themeAttribute = this.getAttribute("theme") || "auto";
1090
1177
  const actualTheme = this.getActualTheme(themeAttribute);
1091
1178
  const redirectUrl = generateComponentRedirectUrl(
@@ -1643,6 +1730,7 @@ class SkhemaElement extends HTMLElement {
1643
1730
  return [
1644
1731
  "element-type",
1645
1732
  "contributor-id",
1733
+ "contributor-name",
1646
1734
  "content",
1647
1735
  "source-url",
1648
1736
  "theme",
@@ -1763,7 +1851,8 @@ class SkhemaElement extends HTMLElement {
1763
1851
  );
1764
1852
  const themeAttribute = this.getAttribute("theme") || "auto";
1765
1853
  const actualTheme = this.getActualTheme(themeAttribute);
1766
- const displayName = this.formatContributorName(contributor_id);
1854
+ const contributorName = this.getAttribute("contributor-name");
1855
+ const displayName = contributorName || this.formatContributorName(contributor_id);
1767
1856
  const componentType = resolveComponentType(element_type);
1768
1857
  const acronym = getComponentTypeAcronym(componentType);
1769
1858
  const colors = COMPONENT_COLORS[componentType];