Swift中的自定义尺寸

时间:2017-01-03 17:52:15

标签: swift google-analytics google-analytics-sdk

我有一个与Google Analytics集成的Objective-C应用。现在,我正在尝试集成一个用Swift编写的应用程序。

有我的Objective-C代码:

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())

    }

我正在快速尝试

Sub Main()
    For Each arg As String In My.Application.CommandLineArgs
        Select Case Trim(LCase(arg))
            Case "/docx-pdf"
                Dim oWord As Word.Application
                Dim oDoc As Word.Document
                Dim path As String
                Console.WriteLine("Podaj scieżkę folderu:")
                path = Console.ReadLine()
                Dim afiles As String() = Directory.GetFiles(path + "\", "*.docx")
                Dim bfiles As String() = Directory.GetFiles(path + "\", "~$*")
                Dim cfiles = afiles.Except(bfiles)
                For Each file As String In cfiles
                    oWord = CreateObject("word.application")
                    oWord.Visible = False
                    oDoc = oWord.Documents.Open(file, ReadOnly:=True)
                    oDoc.SaveAs(FileName:=file.Replace(".docx", ".pdf"), FileFormat:=Word.WdSaveFormat.wdFormatPDF)
                    oWord.Quit()
                Next
        End Select
    Next
End Sub

但我得到GAIFields没有成员customDimension。那么,Swift中的代码应该怎么样?

2 个答案:

答案 0 :(得分:4)

它对我有用:

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

答案 1 :(得分:0)

我正在跟踪具有自定义尺寸的自定义异常,希望这对某些人有帮助


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

快乐跟踪..:)