iT邦幫忙

3

Nuxt.js - GTM & GA & FB Pixel 安裝

Ares 2019-06-13 21:08:514735 瀏覽

行銷常常會需要追蹤網頁的流量,這時候就會用到這些工具了,這次就是教學一下怎麼埋入程式碼
目前都有現成套件可使用,用法也非常簡單~~

FB Pixel

  1. 安裝套件 nuxt-facebook-pixel-module
$ npm install --save nuxt-facebook-pixel-module
  1. 進行基礎設定,細項設定可參考官方
// nuxt.config.js

module.exports = {
  modules: [
    'nuxt-facebook-pixel-module',
  ],
  facebook: {
    track: 'PageView',
    // 替換自己 FB Pixel 的 ID
    pixelId: 'FACEBOOK_PIXEL_ID',
    disabled: false
  },
}
  1. 確認是否安裝成功
    安裝插件 Facebook Pixel Helper,並於網站點選插件,如成功安裝就會看到以下畫面
    FB Pixel成功安裝畫面

GA

官方有教學可參考

  1. 於 plugins 資料夾內新增檔案,並放入程式碼,記得替換自己的 ID
// plugins/ga.js

if (process.client && process.env.NODE_ENV === 'production') {

  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  
  // 替換自己 GA 的 ID
  ga('create', 'UA-XXXXXXXX-X', 'auto')
}

export default ({ app: { router }, store }) => {
  router.afterEach((to, from) => {
    ga('set', 'page', to.fullPath)
    ga('send', 'pageview')
  })
}
  1. plugins 加入設定檔
// nuxt.config.js

module.exports = {
  plugins: [
    { src: '~plugins/ga.js', ssr: false }
  ]
}
  1. 確認是否安裝成功
    安裝插件 Tag Assistant,並於網站點選插件,會看到以下畫面
    Tag Assistant安裝後畫面
    綠色表示正常,藍色、黃色、紅色表示有問題

GTM

  1. 安裝套件 Google Tag Manager
$ npm install --save @nuxtjs/google-tag-manager
  1. 進行基礎設定,細項設定可參考官方
// nuxt.config.js

module.exports = {
  modules: [
    // 替換自己 GTM 的 ID
    ['@nuxtjs/google-tag-manager', { id: 'GTM-XXXXXXX' }]
  ]
}
  1. 確認是否安裝成功
    同上使用 Tag Assistant

以上就是在 Nuxt 中如何安裝與驗證這些工具的方法~~


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言