UNPKG

3.34 kBJavaScriptView Raw
1const { resolve } = require('path')
2
3// Default for adslots (defaults to test mode)
4const Defaults = {
5 tag: 'adsbygoogle',
6 id: null,
7 pageLevelAds: false,
8 includeQuery: false,
9 analyticsUacct: '',
10 analyticsDomainName: '',
11 overlayBottom: false,
12 test: false,
13 onPageLoad: false
14}
15
16// Default client ID for testing
17const TestID = 'ca-google'
18
19// Adsense script URL
20const AdSenseURL = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'
21
22module.exports = function nuxtAdSense (moduleOptions = {}) {
23 const options = Object.assign({}, Defaults, this.options['google-adsense'] || moduleOptions)
24
25 // Normalize options
26 options.test = Boolean(options.test)
27 options.pageLevelAds = Boolean(options.pageLevelAds)
28 options.includeQuery = String(Boolean(options.includeQuery))
29 options.analyticsUacct = options.analyticsUacct || ''
30 options.analyticsDomainName = options.analyticsDomainName || ''
31 options.overlayBottom = Boolean(options.overlayBottom)
32 options.onPageLoad = Boolean(options.onPageLoad)
33
34 if (this.options.dev && process.env.NODE_ENV !== 'production') {
35 // If in DEV mode, place ads in 'test' state automatically
36 // https://www.thedev.blog/3087/test-adsense-ads-safely-without-violating-adsense-tos/
37 options.test = true
38 }
39
40 if (options.test) {
41 // If in test mode, we ue the Test Client ID
42 options.id = TestID
43 }
44
45 if (!options.id || typeof options.id !== 'string') {
46 // Invalid adsense client ID, so don't include
47 // console.warn('Invalid adsense client ID specified')
48 return
49 }
50
51 // Set the desired component tag name
52 options.tag = options.tag || Defaults.tag
53
54 // Register our plugin and pass config options
55 this.addPlugin({
56 src: resolve(__dirname, './plugin.template.js'),
57 fileName: 'adsbygoogle.js',
58 options: options
59 })
60
61 // Add the async Google AdSense script to head
62 this.options.head.script.push({
63 async: true,
64 src: AdSenseURL
65 })
66
67 // Unfortunately these lines are needed to prevent vue-meta from esacping quotes in the init script
68 this.options.head.__dangerouslyDisableSanitizersByTagID = this.options.head.__dangerouslyDisableSanitizersByTagID || {}
69 this.options.head.__dangerouslyDisableSanitizersByTagID.adsbygoogle = ['innerHTML']
70
71 const adsenseScript = `{
72 google_ad_client: "${options.id}",
73 enable_page_level_ads: ${options.pageLevelAds ? 'true' : 'false'},
74 overlays: {bottom: ${options.overlayBottom}}
75 }`
76 // Initialize Adsense with ad client id
77 if (!options.onPageLoad) {
78 this.options.head.script.push({
79 hid: 'adsbygoogle',
80 innerHTML: `
81 (window.adsbygoogle = window.adsbygoogle || []).push(${adsenseScript});`
82 })
83 } else {
84 this.options.head.script.push({
85 hid: 'adsbygoogle',
86 innerHTML: `
87 (window.adsbygoogle = window.adsbygoogle || []).onload = function () {
88 [].forEach.call(document.getElementsByClassName('adsbygoogle'), function () {
89 adsbygoogle.push(${adsenseScript});
90 })
91 }`
92 })
93 }
94
95 // If in DEV mode, add robots meta first to comply with Adsense policies
96 // To prevent MediaPartenrs from scraping the site
97 if (options.test) {
98 this.options.head.meta.unshift({
99 name: 'robots',
100 content: 'noindex,noarchive,nofollow'
101 })
102 }
103}
104
105module.exports.meta = require('./../package.json')