Swift 中的自定义维度

Custom Dimensions in Swift

我有一个与 Google Analytics 集成的 Objective-C 应用程序。现在,我正在尝试集成一个用 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]];
}

我正在尝试 swift

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中的代码应该怎么写呢?

它对我有用:

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])
}

我正在使用自定义维度跟踪自定义异常:


    //MARK:- CUSTOM EXCEPTION TRACKING

    func doTrackCustomExceptionWithGA(message:String, customDimensionValue:String, isFatal:Bool = false) {

        guard let tracker = GAI.sharedInstance()?.defaultTracker else { return }

        guard let exceptionBuilder = GAIDictionaryBuilder.createException(withDescription: message, withFatal: NSNumber(value: isFatal)) else { return }
        if !customDimensionValue.isEmpty {
            exceptionBuilder.set(customDimensionValue, forKey: GAIFields.customDimension(for: 15))
        }

        guard let build = exceptionBuilder.build() as? [AnyHashable : Any] else { return }
        tracker.send(build)

        // ADDING DUMMY EVENT TO TRACK PREVIOUS EVENT QUICKLY, AS GA EVENTS ARE TRACKED ON NEXT EVENT CALLS 
        // BELOW CODE IS OPTIONAL
        let event = GAIDictionaryBuilder.createScreenView()
        tracker.send(event?.build() as! [NSObject: Any])
    }