Custom Dimensions in Swift

1.4k views Asked by At

I have an Objective-C app that integrates with Google Analytics. Now, I'm trying to integrate an app written in Swift.

There's my Objective-C Code:

- (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]];
}

and I'm trying in 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())

    }

but I'm getting GAIFields has no member customDimension. Then, how should be the code in Swift?

2

There are 2 answers

0
AskerMobile On BEST ANSWER

It worked for me:

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])
}
0
Dhaval H. Nena On

I was tracking custom exception with custom dimension:


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