2017-07-12 21 views

回答

0

的文檔不幸缺乏應用實例集成任何東西。希望有足夠的時間,人們會提出足夠的要求,以便文檔有明確的指導。在此同時,我認爲這將有助於你:

(注:此使用Angulartics映射事件段或其他分析工具你可以繞過Angulartics部分,但爲什麼推倒重來?)

  1. 您需要做的第一件事是在您的應用中包含細分庫。他們在您的源概覽屏幕(https://segment.com/yoda/sources/ {您的源名稱/概述)中提供了該片段。這看起來像<script> !function() ... </script>。複製並粘貼到您的應用的index.html文件中,靠近其部分的頂部。
  2. 將Angulartics添加到您的項目中:

    a。在項目中安裝Angulartics:npm install angulartics2 --save

    (事後,我建議你從你的package.json文件中刪除上angulartics的^,以便確切版本安裝在未來)

    灣包括Angulartics在App模塊:

代碼(SRC /應用/ app.module.ts):

import { 
    Angulartics2Module, 
    Angulartics2Segment, 
} from 'angulartics2' 

... 

@NgModule({ 
    imports: [ 
    Angulartics2Module.forRoot([ Angulartics2Segment ]) 
    ] 
}) 
export class AppModule {} 
使用Angulartics包
  • 開始在代碼:
  • import { Angulartics2Segment } from 'angulartics2' 
     
        ... 
     
        export class SomeComponent { 
     
         constructor(private analytics: Angulartics2Segment) { } 
     
         public submitButtonClicked(){ 
     
         let properties = { 
     
          foo: 'bar', 
     
          baz: 42, 
     
          etc: { some: 'thing' } 
     
         } 
     
         this.analytics.eventTrack('Event Action', properties) 
     
         } 
     
        }

    現在,只要組件觸發submitButtonClicked()方法,該事件就會被跟蹤。您可以爲事件添加任何級別的屬性,但請確保您的分析可視化工具可以理解架構。例如,如果您想在Google Analytics中跟蹤事件,則需要將屬性限制爲2,並將其命名爲「類別」和「標籤」。

    1

    我最終做的是將分段提供的analytics.js腳本放到index.html中。然後在您的模板中,在您的導入和@Component外面,通過在'declare var analytics'中寫入來暴露Segment腳本中可用的分析變量。

    的index.html

    <head> 
        <script src="cordova.js"></script> 
    
        <!-- Drop in script here --> 
        <script type="text/javascript"> 
        !function(){var analytics=window.analytics=window.analytics||[]; if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="4.0.0"; 
        analytics.load("YOUR_WRITE_KEY"); 
        }}(); 
        </script> 
    
    </head> 
    

    home.ts

    import {Component} from '@angular/core'; 
    etc.... 
    
    //expose analytics variable 
    declare var analytics; 
    
    @Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html' 
    }) 
    
    export class HomePage { 
    constructor() { 
    
    analytics.page("Home"); 
    } 
    
    myButton(){ 
        analytics.track("Button Click"); 
    
    
        console.log("My Button was clicked"); 
    } 
    
    } 
    

    您可以在任何TS申報文件的分析,你需要使用區段。

    相關問題