Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
feat: support new adsense code (#165)
Browse files Browse the repository at this point in the history
* feat: support new adsense code

* docs: small quote

* feat: add typing for param

Co-authored-by: Irfan Maulana <maulana.irfan@tokopedia.com>
  • Loading branch information
mazipan and irfan-maulana-tkp authored Jul 28, 2021
1 parent fee66e6 commit d558b86
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ import Ads from 'vue-google-adsense'

Vue.use(require('vue-script2'))

Vue.use(Ads.AutoAdsense, { adClient: 'YOUR_GOOGLE_AD_CLIENT' })
Vue.use(Ads.AutoAdsense, { adClient: 'YOUR_GOOGLE_AD_CLIENT', isNewAdsCode: true })
```

## :gift: Available Props
Expand All @@ -151,6 +151,7 @@ Vue.use(Ads.AutoAdsense, { adClient: 'YOUR_GOOGLE_AD_CLIENT' })
| data-ad-format | String | `auto` for Adsense Ads<br> `fluid` for InFeed and InArticle Ads | Attribute `data-ad-format` from adsense <br> Possible values are `auto`, `horizontal`, `vertical`, `rectangle` or `fluid` |
| data-full-width-responsive | String (`yes` or `no`) | `no` | Attribute `data-full-width-responsive` from adsense |
| is-non-personalized-ads | String (`yes` or `no`) | `no` | Props for request `non-personalized` ads, [read more](https://support.google.com/adsense/answer/9042142?hl=en&ref_topic=7670012) |
| `is-new-ads-code` | String (`yes` or `no`) | `no` | Use new Adsense code (per 19 july 2021), [read more](https://support.google.com/adsense/answer/10627874) |

## :metal: Who is using this library

Expand Down
3 changes: 2 additions & 1 deletion packages/demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ import Ads from 'vue-google-adsense'

Vue.use(require('vue-script2'))

Vue.use(Ads.AutoAdsense, { adClient: 'YOUR_GOOGLE_AD_CLIENT' })
Vue.use(Ads.AutoAdsense, { adClient: 'YOUR_GOOGLE_AD_CLIENT', isNewAdsCode: true })
```

## :gift: Available Props
Expand All @@ -151,6 +151,7 @@ Vue.use(Ads.AutoAdsense, { adClient: 'YOUR_GOOGLE_AD_CLIENT' })
| data-ad-format | String | `auto` for Adsense Ads<br> `fluid` for InFeed and InArticle Ads | Attribute `data-ad-format` from adsense <br> Possible values are `auto`, `horizontal`, `vertical`, `rectangle` or `fluid` |
| data-full-width-responsive | String (`yes` or `no`) | `no` | Attribute `data-full-width-responsive` from adsense |
| is-non-personalized-ads | String (`yes` or `no`) | `no` | Props for request `non-personalized` ads, [read more](https://support.google.com/adsense/answer/9042142?hl=en&ref_topic=7670012) |
| `is-new-ads-code` | String (`yes` or `no`) | `no` | Use new Adsense code (per 19 july 2021), [read more](https://support.google.com/adsense/answer/10627874) |

## :metal: Who is using this library

Expand Down
3 changes: 2 additions & 1 deletion packages/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ import Ads from 'vue-google-adsense'

Vue.use(require('vue-script2'))

Vue.use(Ads.AutoAdsense, { adClient: 'YOUR_GOOGLE_AD_CLIENT' })
Vue.use(Ads.AutoAdsense, { adClient: 'YOUR_GOOGLE_AD_CLIENT', isNewAdsCode: true })
```

## :gift: Available Props
Expand All @@ -151,6 +151,7 @@ Vue.use(Ads.AutoAdsense, { adClient: 'YOUR_GOOGLE_AD_CLIENT' })
| data-ad-format | String | `auto` for Adsense Ads<br> `fluid` for InFeed and InArticle Ads | Attribute `data-ad-format` from adsense <br> Possible values are `auto`, `horizontal`, `vertical`, `rectangle` or `fluid` |
| data-full-width-responsive | String (`yes` or `no`) | `no` | Attribute `data-full-width-responsive` from adsense |
| is-non-personalized-ads | String (`yes` or `no`) | `no` | Props for request `non-personalized` ads, [read more](https://support.google.com/adsense/answer/9042142?hl=en&ref_topic=7670012) |
| `is-new-ads-code` | String (`yes` or `no`) | `no` | Use new Adsense code (per 19 july 2021), [read more](https://support.google.com/adsense/answer/10627874) |

## :metal: Who is using this library

Expand Down
9 changes: 8 additions & 1 deletion packages/lib/src/ads/AdsenseComponent.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<template>
<div :class="rootClass">
<script2 type="text/javascript" async="true" :src="ADS_SCRIPT" />

<template v-if="isNewAdsCode === 'yes'">
<script2 type="text/javascript" async="true" :src="`${ADS_SCRIPT}?client=${dataAdClient}`" crossorigin="anonymous" />
</template>
<template v-if="isNewAdsCode === 'no'">
<script2 type="text/javascript" async="true" :src="ADS_SCRIPT" />
</template>

<ins
:class="insClass"
class="adsbygoogle"
Expand Down
15 changes: 13 additions & 2 deletions packages/lib/src/ads/AutoAdsense.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import constant from '../utils/constant'

interface AutoAdsenseParam {
adClient: string;
isNewAdsCode: boolean;
}

const AutoAdsense = {
install: (Vue, { adClient = '' }) => {
install: (Vue, { adClient = '', isNewAdsCode = false }: AutoAdsenseParam) => {
if (!adClient) {
return Error('AutoAdsense require "adClient" parameter')
}
const head = document.head;
const s = document.createElement('script')
s.type = 'text/javascript';
s.src = constant.ADS_SCRIPT;
s.async = true;
if (isNewAdsCode) {
s.crossOrigin = 'anonymous'
s.src = `${constant.ADS_SCRIPT}?client=${adClient}`;
} else {
s.src = constant.ADS_SCRIPT;
}
s.onload = () => {
// @ts-ignore
(adsbygoogle = window.adsbygoogle || []).push({
Expand Down
9 changes: 8 additions & 1 deletion packages/lib/src/ads/InArticleAdsenseComponent.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<template>
<div :class="rootClass">
<script2 type="text/javascript" async="true" :src="ADS_SCRIPT" />

<template v-if="isNewAdsCode === 'yes'">
<script2 type="text/javascript" async="true" :src="`${ADS_SCRIPT}?client=${dataAdClient}`" crossorigin="anonymous" />
</template>
<template v-if="isNewAdsCode === 'no'">
<script2 type="text/javascript" async="true" :src="ADS_SCRIPT" />
</template>

<ins
:class="insClass"
class="adsbygoogle"
Expand Down
9 changes: 8 additions & 1 deletion packages/lib/src/ads/InFeedAdsenseComponent.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<template>
<div :class="rootClass">
<script2 type="text/javascript" async="true" :src="ADS_SCRIPT" />

<template v-if="isNewAdsCode === 'yes'">
<script2 type="text/javascript" async="true" :src="`${ADS_SCRIPT}?client=${dataAdClient}`" crossorigin="anonymous" />
</template>
<template v-if="isNewAdsCode === 'no'">
<script2 type="text/javascript" async="true" :src="ADS_SCRIPT" />
</template>

<ins
:class="insClass"
class="adsbygoogle"
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/utils/constant.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default {
ADS_SCRIPT: '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'
}
}
6 changes: 6 additions & 0 deletions packages/lib/src/utils/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@ export default {
type: String,
default: ''
},
// Read: https://support.google.com/adsense/answer/10627874
// Issue: https://github.com/mazipan/vue-google-adsense/issues/164
isNewAdsCode: {
type: String,
default: 'no'
},
}

0 comments on commit d558b86

Please sign in to comment.