# vuepress配置google adsense、analytics

(adsbygoogle = window.adsbygoogle || []).push({});

目录

出于获利和统计需要,要给vuepress添加google adsense、analytics代码,vuepress有官方插件可以提供analytics 集成功能:

# google-analytics插件

https://vuepress.vuejs.org/zh/plugin/official/plugin-google-analytics.html

不过按照文档配置后,编译出现以下问题,总提示找不到插件:

warning [vuepress] cannot resolve plugin "@vuepress/google-analytics"

在node_modules目录已经有这个插件,谷歌了一圈没找到结果,放弃用插件集成ID。

# 通过config.js配置head(推荐)

配置config.js,增加head属性,vuepress会把head的自定义标签注入到全部页面,

用法[tagName, { attrName: attrValue }, innerHTML?]

以下配置analytics id为:UA-000000000-1,adsense发布商id为:ca-pub-000000000000

module.exports = {
  head: [
    ['script', { src: "https://www.googletagmanager.com/gtag/js?id=UA-000000000-1",async: true}],
    ['script', {},
                " window.dataLayer = window.dataLayer || [];\
                function gtag(){dataLayer.push(arguments);}\
                gtag('js', new Date());\
                gtag('config', 'UA-000000000-1');"],
    
     ['script', { src: "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" ,async: true}],
     ['script', {},
                '(adsbygoogle = window.adsbygoogle || []).push({\
                google_ad_client: "ca-pub-000000000000",\
                enable_page_level_ads: true});'],
  ]
}

增加一个自定义的 favicon

module.exports = {
  head: [
    ['link', { rel: 'icon', href: '/logo.png' }]
  ]
}
Last Updated: 3/20/2023, 5:01:24 PM