Skip to content

Commit ba124ad

Browse files
authored
fix(adsense): support in-feed layout keys (#874)
1 parent 96f358b commit ba124ad

4 files changed

Lines changed: 72 additions & 5 deletions

File tree

‎docs/content/scripts/google-adsense.md‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,13 @@ An `ads.txt` file does not replace AdSense's site review. After publishing it, c
145145
|`data-ad-client`| Your **Google AdSense publisher ID** (`ca-pub-XXXXXXXXXX`). |
146146
|`data-ad-slot`| Your **Ad Slot ID** (available in AdSense dashboard). |
147147
|`data-ad-format`| Ad format type (`auto`, `rectangle`, `horizontal`, `vertical`, `fluid`, `autorelaxed`). |
148-
|`data-ad-layout`| Layout (`in-article`, `in-feed`, `fixed`). |
148+
|`data-ad-layout`| In-article layout (`in-article`). |
149+
|`data-ad-layout-key`| Layout key generated by AdSense for an in-feed unit. |
149150
|`data-full-width-responsive`|**Set to `true`** to make the ad responsive. |
150151

151152
#### Example using `data-ad-layout`
152153

153-
Set `data-ad-layout` for layouts such as `in-article`:
154+
Set `data-ad-layout` for an in-article unit:
154155

155156
```vue
156157
<template>
@@ -163,6 +164,21 @@ Set `data-ad-layout` for layouts such as `in-article`:
163164
</template>
164165
```
165166

167+
#### Example using `data-ad-layout-key`
168+
169+
Copy the layout key from the in-feed unit generated by AdSense:
170+
171+
```vue
172+
<template>
173+
<ScriptGoogleAdsense
174+
data-ad-client="ca-pub-<your-id>"
175+
data-ad-slot="1234567890"
176+
data-ad-format="fluid"
177+
data-ad-layout-key="-6t+ed+2i-1n-4w"
178+
/>
179+
</template>
180+
```
181+
166182
## Handling ad blockers
167183

168184
Use the `error` slot for visitors whose ad blocker stops the script:

‎packages/script/src/registry-types.json‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@
306306
{
307307
"name": "ScriptGoogleAdsenseProps",
308308
"kind": "interface",
309-
"code": "interface ScriptGoogleAdsenseProps {\n dataAdClient: string\n dataAdSlot: string\n dataAdFormat?: 'auto' | 'rectangle' | 'vertical' | 'horizontal' | 'fluid' | 'autorelaxed'\n dataAdLayout?: 'in-article' | 'in-feed' | 'fixed'\n dataFullWidthResponsive?: boolean\n /**\n * Defines the trigger event to load the script.\n */\n trigger?: ElementScriptTrigger\n}"
309+
"code": "interface ScriptGoogleAdsenseProps {\n dataAdClient: string\n dataAdSlot: string\n dataAdFormat?: 'auto' | 'rectangle' | 'vertical' | 'horizontal' | 'fluid' | 'autorelaxed'\n dataAdLayout?: 'in-article'\n dataAdLayoutKey?: string\n dataFullWidthResponsive?: boolean\n /**\n * Defines the trigger event to load the script.\n */\n trigger?: ElementScriptTrigger\n}"
310310
},
311311
{
312312
"name": "ScriptGoogleAdsenseEvents",
@@ -4545,7 +4545,12 @@
45454545
},
45464546
{
45474547
"name": "dataAdLayout",
4548-
"type": "'in-article' | 'in-feed' | 'fixed'",
4548+
"type": "'in-article'",
4549+
"required": false
4550+
},
4551+
{
4552+
"name": "dataAdLayoutKey",
4553+
"type": "string",
45494554
"required": false
45504555
},
45514556
{

‎packages/script/src/runtime/components/ScriptGoogleAdsense.vue‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const props = withDefaults(defineProps<{
1010
dataAdClient:string
1111
dataAdSlot:string
1212
dataAdFormat?:'auto'|'rectangle'|'vertical'|'horizontal'|'fluid'|'autorelaxed'
13-
dataAdLayout?:'in-article'|'in-feed'|'fixed'
13+
dataAdLayout?:'in-article'
14+
dataAdLayoutKey?:string
1415
dataFullWidthResponsive?:boolean
1516
/**
1617
* Defines the trigger event to load the script.
@@ -89,6 +90,7 @@ const rootAttrs = computed(() => {
8990
:data-ad-slot="dataAdSlot"
9091
:data-ad-format="dataAdFormat"
9192
:data-ad-layout="dataAdLayout"
93+
:data-ad-layout-key="dataAdLayoutKey"
9294
:data-full-width-responsive="dataFullWidthResponsive"
9395
v-bind="rootAttrs"
9496
/>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import{mountSuspended}from'@nuxt/test-utils/runtime'
2+
import{beforeEach,describe,expect,it,vi}from'vitest'
3+
importScriptGoogleAdsensefrom'../../packages/script/src/runtime/components/ScriptGoogleAdsense.vue'
4+
5+
constmocks=vi.hoisted(()=>({
6+
status: {__v_isRef: true,value: 'loaded'},
7+
useScriptGoogleAdsense: vi.fn(),
8+
useScriptTriggerElement: vi.fn(()=>()=>{}),
9+
}))
10+
11+
vi.mock('../../packages/script/src/runtime/composables/useScriptTriggerElement',()=>({
12+
useScriptTriggerElement: mocks.useScriptTriggerElement,
13+
}))
14+
15+
vi.mock('../../packages/script/src/runtime/registry/google-adsense',()=>({
16+
useScriptGoogleAdsense: mocks.useScriptGoogleAdsense,
17+
}))
18+
19+
describe('google adsense component',()=>{
20+
beforeEach(()=>{
21+
vi.clearAllMocks()
22+
mocks.useScriptGoogleAdsense.mockReturnValue({status: mocks.status})
23+
window.adsbygoogle=[]
24+
})
25+
26+
it('renders the in-feed layout key on the ad unit',async()=>{
27+
constwrapper=awaitmountSuspended(ScriptGoogleAdsense,{
28+
props: {
29+
dataAdClient: 'ca-pub-123',
30+
dataAdSlot: '456',
31+
dataAdFormat: 'fluid',
32+
dataAdLayoutKey: '-6t+ed+2i-1n-4w',
33+
},
34+
})
35+
36+
expect({
37+
adUnit: wrapper.get('ins').attributes('data-ad-layout-key'),
38+
wrapper: wrapper.attributes('data-ad-layout-key'),
39+
}).toEqual({
40+
adUnit: '-6t+ed+2i-1n-4w',
41+
wrapper: undefined,
42+
})
43+
})
44+
})

0 commit comments

Comments
 (0)