diff --git a/README.md b/README.md index beb84d6..961c761 100755 --- a/README.md +++ b/README.md @@ -28,6 +28,28 @@ Using top level options: id: 'ca-pub-#########' } } + +``` +Using runtime config: + +```js +{ + modules: [ + ['@nuxtjs/google-adsense'] + ], + + 'google-adsense': { + onPageLoad: false, + pageLevelAds: false, + }, + + publicRuntimeConfig: { + 'google-adsense': { + id: process.env.GOOGLE_ADSENSE_ID, + test: process.env.GOOGLE_ADSENSE_TEST_MODE === 'true', + }, + }, +} ``` The asynchronous ad code (`//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js`) is automatically diff --git a/lib/module.js b/lib/module.js index dbf1656..439a858 100644 --- a/lib/module.js +++ b/lib/module.js @@ -20,13 +20,13 @@ const TestID = 'ca-google' // Adsense script URL const AdSenseURL = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js' -module.exports = function nuxtAdSense (moduleOptions = {}) { +module.exports = function nuxtAdSense(moduleOptions = {}) { const options = Object.assign({}, Defaults, this.options['google-adsense'] || moduleOptions) // Normalize options options.test = Boolean(options.test) options.pageLevelAds = Boolean(options.pageLevelAds) - options.includeQuery = String(Boolean(options.includeQuery)) + options.includeQuery = Boolean(options.includeQuery) options.analyticsUacct = options.analyticsUacct || '' options.analyticsDomainName = options.analyticsDomainName || '' options.overlayBottom = Boolean(options.overlayBottom) @@ -109,7 +109,7 @@ module.exports = function nuxtAdSense (moduleOptions = {}) { } } -function ensureScriptExecuteOnce (script) { +function ensureScriptExecuteOnce(script) { return `if (!window.__abg_called){ ${script} window.__abg_called = true;}` } diff --git a/lib/plugin.template.js b/lib/plugin.template.js index 5fb346d..3f4480b 100755 --- a/lib/plugin.template.js +++ b/lib/plugin.template.js @@ -1,140 +1,156 @@ import Vue from 'vue' -// Custom AdSense Ad Component -const adsbygoogle = { - render (h) { - return h( - 'ins', - { - 'class': ['adsbygoogle'], - style: this.adStyle, - attrs: { - 'data-ad-client': this.adClient, - 'data-ad-slot': this.adSlot || null, - 'data-ad-format': this.adFormat, - 'data-ad-region': this.show ? this.adRegion() : null, - 'data-ad-layout': this.adLayout || null, - 'data-ad-layout-key': this.adLayoutKey || null, - 'data-page-url': this.pageUrl ? this.pageUrl : null, - 'data-analytics-uacct': this.analyticsUacct ? this.analyticsUacct : null, - 'data-analytics-domain-name': this.analyticsDomainName ? this.analyticsDomainName : null, - 'data-adtest': <%= options.test ? '\'on\'' : 'null' %>, - 'data-adsbygoogle-status': this.show ? null : '', - 'data-full-width-responsive': this.adFullWidthResponsive || null, - }, - domProps: { - innerHTML: this.show ? '' : ' ' - }, - key: Math.random() - } - ) - }, - props: { - adClient: { - type: String, - default: '<%= options.id %>' - }, - adSlot: { - type: String - }, - adFormat: { - type: String, - default: 'auto' - }, - adLayout: { - type: String - }, - adLayoutKey: { - type: String +export default async (ctx) => { + const runtimeConfig = ctx.$config && ctx.$config['google-adsense'] || {} + const moduleOptions = <%= serialize(options) %> + const options = { ...moduleOptions, ...runtimeConfig } + + if (ctx.isDev && process.env.NODE_ENV !== 'production') { + options.test = true + } + + if (options.test) { + options.id = 'ca-google' + } + + // Custom AdSense Ad Component + const adsbygoogle = { + render (h) { + return h( + 'ins', + { + 'class': ['adsbygoogle'], + style: this.adStyle, + attrs: { + 'data-ad-client': this.adClient, + 'data-ad-slot': this.adSlot || null, + 'data-ad-format': this.adFormat, + 'data-ad-region': this.show ? this.adRegion() : null, + 'data-ad-layout': this.adLayout || null, + 'data-ad-layout-key': this.adLayoutKey || null, + 'data-page-url': this.pageUrl ? this.pageUrl : null, + 'data-analytics-uacct': this.analyticsUacct ? this.analyticsUacct : null, + 'data-analytics-domain-name': this.analyticsDomainName ? this.analyticsDomainName : null, + 'data-adtest': options.test ? 'on' : null, + 'data-adsbygoogle-status': this.show ? null : '', + 'data-full-width-responsive': this.adFullWidthResponsive || null, + }, + domProps: { + innerHTML: this.show ? '' : ' ' + }, + key: Math.random() + } + ) }, - adStyle: { - type: Object, - default () { - return { - display: 'block' + props: { + adClient: { + type: String, + default: options.id + }, + adSlot: { + type: String + }, + adFormat: { + type: String, + default: 'auto' + }, + adLayout: { + type: String + }, + adLayoutKey: { + type: String + }, + adStyle: { + type: Object, + default () { + return { + display: 'block' + } } + }, + adFullWidthResponsive: { + type: Boolean, + default: false + }, + pageUrl: { + type: String + }, + analyticsUacct: { + type: String, + default: options.analyticsUacct + }, + analyticsDomainName: { + type: String, + default: options.analyticsDomainName + }, + includeQuery: { + type: Boolean, + default: options.includeQuery } }, - adFullWidthResponsive: { - type: Boolean, - default: false - }, - pageUrl: { - type: String - }, - analyticsUacct: { - type: String, - default: '<%= options.analyticsUacct %>' - }, - analyticsDomainName: { - type: String, - default: '<%= options.analyticsDomainName %>' - }, - includeQuery: { - type: Boolean, - default: <%= options.includeQuery %> - } - }, - data () { - return { - show: true - } - }, - mounted () { - this.showAd() - }, - watch: { - '$route' (to, from) { - // Update if element is connected to DOM. - // Prevent updating not connected alive componentns. - if (this.$el && !this.$el.isConnected) { - return; + data () { + return { + show: true } - if (to.fullPath === from.fullPath) { - return; - } - const keys = Object.keys - const toQuery = to.query - const fromQuery = from.query - let changed = false - if (to.path !== from.path) { - changed = true - } else if (this.includeQuery) { - // If we include query params, check to see if they are loosely unequal - changed = (keys(toQuery).length !== keys(fromQuery).length) || !keys(toQuery).every(k => toQuery[k] === fromQuery[k]) - } - if (changed) { - // If the route has changed, update the ad - this.updateAd() - } - } - }, - methods: { - adRegion () { - return 'page-' + Math.random() }, - updateAd () { - if (this.isServer) { - return + mounted () { + this.showAd() + }, + watch: { + '$route' (to, from) { + // Update if element is connected to DOM. + // Prevent updating not connected alive componentns. + if (this.$el && !this.$el.isConnected) { + return; + } + if (to.fullPath === from.fullPath) { + return; + } + const keys = Object.keys + const toQuery = to.query + const fromQuery = from.query + let changed = false + if (to.path !== from.path) { + changed = true + } else if (this.includeQuery) { + // If we include query params, check to see if they are loosely unequal + changed = (keys(toQuery).length !== keys(fromQuery).length) || !keys(toQuery).every(k => toQuery[k] === fromQuery[k]) + } + if (changed) { + // If the route has changed, update the ad + this.updateAd() + } } - // Reset the INS element - this.show = false - // Show new ad on nextTick - this.$nextTick(this.showAd) }, - showAd () { - this.show = true - this.$nextTick(() => { - try { - // Once ad container () DOM has (re-)rendered, requesst a new advert - (window.adsbygoogle = window.adsbygoogle || []).push({}) - } catch (error) { - console.error(error) + methods: { + adRegion () { + return 'page-' + Math.random() + }, + updateAd () { + if (this.isServer) { + return } - }) + // Reset the INS element + this.show = false + // Show new ad on nextTick + this.$nextTick(this.showAd) + }, + showAd () { + this.show = true + console.log("updateAd"); + + this.$nextTick(() => { + try { + // Once ad container () DOM has (re-)rendered, requesst a new advert + (window.adsbygoogle = window.adsbygoogle || []).push({}) + } catch (error) { + console.error(error) + } + }) + } } } -} -// Register our ad component under the desired tag name -Vue.component('<%= options.tag %>', adsbygoogle) + // Register our ad component under the desired tag name + Vue.component(options.tag, adsbygoogle) +}