Google AnalyticsをあなたのVueに統合する方法.JSページ


もともと公開webdeasy.de !
Google Analyticsは、ウェブサイト訪問を分析する良いツールです.Vue以来.JSページは、ほとんど一つのページ・アプリケーションとして実現されます、Google Analyticsの統合は「普通の」ウェブサイトと少し異なります.ここでは、まだどのように動作を見つけることができます.

目次

  • 1. Install Vue Analytics
  • 2. Set up Vue Analytics
  • 2.1 Tracking page views via router
  • 2.2 Tracking page views manually
  • 3. Opt Out
  • 4. Event Tracking
  • Conclusion
  • 私は、それをあなたのVueと仮定します.JSのアプリケーションは既に実行されており、あなたのサイトと一緒にライブに行くことです.さもなければ、このポストには興味がないでしょう.
    簡単に処理するためにvue-analytics . これは私たちに多くの利点を提供します、なぜならば、我々は手動ですべてのページ呼び出しを追跡する必要がないので、単にVueルータを通過することができます.詳細は次の通りです.

    VUE解析をインストールする

    After installing (and programming your Vue.js app) you can install the vue-analytics module. You can use the latest version (my version v5.17.2, September 2019), I couldn't find any bugs with my application so far. To do this, use the following command:

    npm install vue-analytics
    

    VUE解析の設定

    Afterwards we have to integrate the module vue-analytics in our main.js and transfer our Google Analytics ID. The marked lines have to be added.

    // src/main.js
    
    import Vue from 'vue';
    import App from './App.vue';
    import router from './router';
    import store from './store';
    import VueAnalytics from 'vue-analytics';
    
    Vue.config.productionTip = false;
    
    // Configuration VueAnalytics
    Vue.use(VueAnalytics, {
        id: 'UA-xxxxxxxxx-x'
    });
    
    new Vue({
        router,
        store,
        render: h => h(App)
    }).$mount('#app');
    
    You have to replace the parameter id in line 13 with your own Google Analytics Tracking ID. You can find it under Administration > Property > Property Settings > Tracking ID.


    ルータ経由で2.1のトラッキングページビュー

    It is possible to pass our router object to the VueAnalytics object. This will manually send all page views to Google Analytics in the background and save us a lot of work. The marked line must be inserted.

    // src/main.js
    
    import Vue from 'vue';
    import App from './App.vue';
    import router from './router';
    import store from './store';
    import VueAnalytics from 'vue-analytics';
    
    Vue.config.productionTip = false;
    
    // Configuration VueAnalytics
    Vue.use(VueAnalytics, {
        id: 'UA-xxxxxxxxx-x',
        router
    });
    
    new Vue({
        router,
        store,
        render: h => h(App)
    }).$mount('#app');
    

    I have this running live on a page myself and can confirm that the code works like this and the data is sent to Google Analytics accordingly.

    手動でページをトラッキング

    Alternatively, we can track the page views manually. To do this, we must include the highlighted line in our component or view as follows.

    // src/components/HelloWorld.vue
    
    export default {
      name: 'HelloWorld',
      props: {
        msg: String
      },
      mounted() {
        this.$ga.page('/pagename');
      }
    };
    

    Pop-ups can be a useful application for manual tracking. There are cases where you want the open/display to be considered as a page call. This is feasible.

    3 . opout

    Opt-out is the name given to the deactivation of an option by the user. In this case the user should be able to decide manually that his data may not be tracked by Google Analytics.

    According to the DSGVO (Datenschutz-Grundverordnung), this function must be offered on every EU page. I also recommend this option on other sites, because there are many people who do not agree with it.

    The opt-out may be included in the privacy statement as follows:

    <p>
      Click <a href="#" @click.prevent="disableTracking">here</a>,
      to disable the tracking through Google Analytics.
    </p>
    

    When clicking, we execute the disableTracking function and issue a corresponding message.

    export default {
      methods: {
        disableTracking: function() {
          this.$ga.disable();
          alert('Tracking disabled');
        }
      }
    };
    

    Conversely, we can also activate tracking again:

    this.$ga.enable();
    

    イベント追跡

    Events can be used to better analyze the behavior of your visitors so that you can make any changes to the site in terms of usability.

    An application example is the tracking of language switching, which means you can determine how often a user is on the move in which language. With the help of this guide あなたのVueのテキストを作ることができます.JSアプリ多言語.イベントの追跡は、アプリケーションエリア(リンククリック、ライトボックスを開く任意の数)に拡張することができます.
    この処理は、操作領域に応じて実行される.だからリンククリックまたは同様.
    // src/components/HelloWorld.vue
    
    export default {
      name: 'HelloWorld',
      props: {
        msg: String
      },
      methods: {
        click: function() {
          this.$ga.event('category', 'action', 'label', 123)
        }
      },
      mounted() {
        this.$ga.page('/pagename');
      }
    };
    
    パラメータの名前を付ける最良の方法はGoogle Analytics documentation .
    Lightboxを開く例は、このように見えるかもしれません(この場合、42は閉じられたLightboxのIDです).
    this.$ga.event('Lightbox', 'click', 'Closed Lightbox', 42)
    

    結論

    With the help of vue-analytics we were able to easily integrate Google Analytics into our SPA Vue.js application. Also the opt-out and event tracking could be realized with a few lines of code.

    Happy tracking!

    Thanks for reading! If you liked this article, please let me know and share it! If you want to you can check out my blog そして、私に続いてください!😊