@vdaluz/astro-affiliate 0.5.1 → 0.7.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 +24 -1
- package/package.json +1 -1
- package/src/components/AffiliateLink.astro +10 -2
- package/src/lib/remark.ts +13 -0
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ Alternatively, a pinned https tarball from a tag works too, with no registry inv
|
|
|
15
15
|
```jsonc
|
|
16
16
|
// package.json
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@vdaluz/astro-affiliate": "https://github.com/vdaluz/astro-affiliate/archive/refs/tags/v0.
|
|
18
|
+
"@vdaluz/astro-affiliate": "https://github.com/vdaluz/astro-affiliate/archive/refs/tags/v0.7.0.tar.gz"
|
|
19
19
|
}
|
|
20
20
|
```
|
|
21
21
|
|
|
@@ -159,6 +159,20 @@ fails the build. **Compliance by construction:** every program actually used by
|
|
|
159
159
|
links in a post must be declared in that post's `affiliates:` frontmatter array, or the build
|
|
160
160
|
fails with a clear error - there's no way to ship an affiliate link without its disclosure.
|
|
161
161
|
|
|
162
|
+
The plugin also writes the post's own catalog keys, in document order with duplicates removed,
|
|
163
|
+
to `affiliateKeys` in the page's frontmatter - useful for a consumer that wants to know "which
|
|
164
|
+
catalog items did this post actually link to" without re-parsing markdown (e.g. to seed a
|
|
165
|
+
related-products widget from a post's own links before falling back to other sources). It's
|
|
166
|
+
`undefined`, not an empty array, on a post with no affiliate links - read it as
|
|
167
|
+
`affiliateKeys ?? []`. Access it via Astro's `remarkPluginFrontmatter`:
|
|
168
|
+
|
|
169
|
+
```astro
|
|
170
|
+
---
|
|
171
|
+
const { remarkPluginFrontmatter } = await render(entry);
|
|
172
|
+
const usedKeys = remarkPluginFrontmatter.affiliateKeys ?? [];
|
|
173
|
+
---
|
|
174
|
+
```
|
|
175
|
+
|
|
162
176
|
## `.astro` pages (`<AffiliateLink>`)
|
|
163
177
|
|
|
164
178
|
For gear pages or other non-markdown content:
|
|
@@ -176,6 +190,15 @@ import { affiliate } from '../config/affiliate';
|
|
|
176
190
|
|
|
177
191
|
Renders `target="_blank" rel="noopener noreferrer sponsored"` by default. Pass `class` to style it, or `channel` to target a per-channel tag/link (see [Per-channel tags](#per-channel-tags-reposts-syndication)) - falls back to the program's default when omitted or unconfigured for that channel.
|
|
178
192
|
|
|
193
|
+
### Click tracking
|
|
194
|
+
|
|
195
|
+
`<AffiliateLink>` renders `data-affiliate-key`, `data-affiliate-channel` (omitted when no `channel`
|
|
196
|
+
prop is passed), and `data-affiliate-program` on the anchor. This package emits no click events
|
|
197
|
+
itself - it's a data-attribute contract a consumer's own analytics wiring can read. See
|
|
198
|
+
[`@vdaluz/astro-opt-in-analytics`'s README](https://github.com/vdaluz/astro-opt-in-analytics#affiliate-click-tracking)
|
|
199
|
+
for `bindAffiliateClickTracking()`, which reads these attributes and reports an `affiliate-click`
|
|
200
|
+
event once analytics consent is granted.
|
|
201
|
+
|
|
179
202
|
## Disclosure (`<AffiliateDisclosure>`)
|
|
180
203
|
|
|
181
204
|
Render at the **top** of the post body, above any affiliate links (FTC: disclosure before links,
|
package/package.json
CHANGED
|
@@ -12,9 +12,17 @@ interface Props {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
const { config, affiliateKey, channel, class: className } = Astro.props;
|
|
15
|
-
const { url } = resolveAffiliate(config, affiliateKey, channel);
|
|
15
|
+
const { url, program } = resolveAffiliate(config, affiliateKey, channel);
|
|
16
16
|
---
|
|
17
17
|
|
|
18
|
-
<a
|
|
18
|
+
<a
|
|
19
|
+
href={url}
|
|
20
|
+
target="_blank"
|
|
21
|
+
rel="noopener noreferrer sponsored"
|
|
22
|
+
class={className}
|
|
23
|
+
data-affiliate-key={affiliateKey}
|
|
24
|
+
data-affiliate-channel={channel}
|
|
25
|
+
data-affiliate-program={program}
|
|
26
|
+
>
|
|
19
27
|
<slot />
|
|
20
28
|
</a>
|
package/src/lib/remark.ts
CHANGED
|
@@ -15,6 +15,7 @@ interface VFileWithAstroFrontmatter {
|
|
|
15
15
|
astro?: {
|
|
16
16
|
frontmatter?: {
|
|
17
17
|
affiliates?: string[];
|
|
18
|
+
affiliateKeys?: string[];
|
|
18
19
|
};
|
|
19
20
|
};
|
|
20
21
|
};
|
|
@@ -36,6 +37,11 @@ function collectAffiliateLinkNodes(node: MdastNode, out: MdastNode[]) {
|
|
|
36
37
|
* `affiliates:` frontmatter (so `<AffiliateDisclosure>` knows to render it).
|
|
37
38
|
* An unknown key, or a used program missing from frontmatter, fails the build.
|
|
38
39
|
*
|
|
40
|
+
* Also writes the post's own affiliate catalog keys, in document order with
|
|
41
|
+
* duplicates removed, to `affiliateKeys` in the page's frontmatter (via
|
|
42
|
+
* `remarkPluginFrontmatter`) - undefined, not an empty array, on a post with
|
|
43
|
+
* no affiliate links, so a consumer should read it as `affiliateKeys ?? []`.
|
|
44
|
+
*
|
|
39
45
|
* import { remarkAffiliate } from '@vdaluz/astro-affiliate/remark';
|
|
40
46
|
*
|
|
41
47
|
* export default defineConfig({
|
|
@@ -50,12 +56,14 @@ export function remarkAffiliate(config: AffiliateConfig) {
|
|
|
50
56
|
|
|
51
57
|
const declaredAffiliates = file.data?.astro?.frontmatter?.affiliates ?? [];
|
|
52
58
|
const usedPrograms = new Set<string>();
|
|
59
|
+
const usedKeys: string[] = [];
|
|
53
60
|
|
|
54
61
|
for (const node of linkNodes) {
|
|
55
62
|
const key = node.url!.slice(AFFILIATE_PREFIX.length);
|
|
56
63
|
const { url, program } = resolveAffiliate(config, key);
|
|
57
64
|
node.url = url;
|
|
58
65
|
usedPrograms.add(program);
|
|
66
|
+
if (!usedKeys.includes(key)) usedKeys.push(key);
|
|
59
67
|
}
|
|
60
68
|
|
|
61
69
|
for (const program of usedPrograms) {
|
|
@@ -65,5 +73,10 @@ export function remarkAffiliate(config: AffiliateConfig) {
|
|
|
65
73
|
);
|
|
66
74
|
}
|
|
67
75
|
}
|
|
76
|
+
|
|
77
|
+
file.data ??= {};
|
|
78
|
+
file.data.astro ??= {};
|
|
79
|
+
file.data.astro.frontmatter ??= {};
|
|
80
|
+
file.data.astro.frontmatter.affiliateKeys = usedKeys;
|
|
68
81
|
};
|
|
69
82
|
}
|