2017-01-03 100 views
1

我有一個與Google Analytics集成的Objective-C應用程序。現在,我正在嘗試整合一個用Swift編寫的應用程序。Swift中的自定義尺寸

有我的Objective-C代碼:

- (void) signInGoogleAnalytics { 
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker]; 

// You only need to set User ID on a tracker once. By setting it on the tracker, the ID will be 
// sent with all subsequent hits. 
[tracker set:kGAIUserId 
     value:self.txtStoreCode.text]; 


NSString *dimensionUsuarioLogado = [NSString stringWithFormat:@"%@", _txtEmployee.text]; 
NSString *dimensionLoja = [NSString stringWithFormat:@"%@", _txtStoreCode.text]; 

[tracker send:[[[GAIDictionaryBuilder createScreenView] set:dimensionUsuarioLogado 
                forKey:[GAIFields customDimensionForIndex:1]] build]]; 

[tracker send:[[[GAIDictionaryBuilder createScreenView] set:dimensionLoja 
                forKey:[GAIFields customDimensionForIndex:2]] build]]; 
} 

我試圖在迅速

func signInGoogleAnalytics() { 
     let tracker = GAI.sharedInstance().defaultTracker 
     tracker.set(kGAIUserId, value: txtStore.text) 

     var dimensionUsuarioLogado = "\(txtUser.text)" 
     var dimensionLoja = "\(txtStore.text)" 

     tracker.send(GAIDictionaryBuilder.createScreenView().set(dimensionUsuarioLogado, forKey: GAIFields.customDimension(forIndex: 1)).build()) 

     tracker.send(GAIDictionaryBuilder.createScreenView().set(dimensionLoja, forKey: GAIFields.customDimension(forIndex: 1)).build()) 

    } 

,但我發現GAIFields沒有成員customDimension。那麼,Swift中的代碼應該如何?

回答

1

它的工作對我來說:

func signInGoogleAnalytics() { 
    let tracker = GAI.sharedInstance().defaultTracker 
    tracker.set(kGAIUserId, value: txtStore.text) 

    let dimensionUsuarioLogado = "\(txtUser.text)" 
    let dimensionLoja = "\(txtStore.text)" 

    tracker.send(GAIDictionaryBuilder.createScreenView().set(dimensionUsuarioLogado, forKey: GAIFields.customDimensionForIndex(1)).build() as NSDictionary as [NSObject : AnyObject]) 

    tracker.send(GAIDictionaryBuilder.createScreenView().set(dimensionLoja, forKey: GAIFields.customDimensionForIndex(2)).build() as NSDictionary as [NSObject : AnyObject]) 
}