Google Analytics(分析)无法正确跟踪来自facebook / instagram和其他平台的流量来源/媒介。有大量的流量作为源/介质不存在
let onesignalInitSettings = [kOSSettingsKeyAutoPrompt:false]
OneSignal.initWithLaunchOptions(launchOptions, appId: AppLibraryKeys.OneSignalAppId, handleNotificationReceived: { [weak self](notification) in
if let payload = notification?.payload {
self?.handleOneSignalPayload(payload)
}
}, handleNotificationAction: { [weak self](result) in
if let payload = result?.notification.payload {
self?.handleOneSignalPayload(payload)
}
}, settings: onesignalInitSettings)
OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;
OneSignal.promptForPushNotifications(userResponse: { accepted in
print("User accepted notifications: \(accepted)")
})
OneSignal.add(self)
fileprivate func handleOneSignalPayload(_ payload:OSNotificationPayload){
guard payload.additionalData != nil else {
return
}
if let utmUrl = URL(string: (payload.additionalData["utm_link"] as? String) ?? "") {
APPDELEGATE.window?.rootViewController?.logCrashlyticsWith(
key: CrashlyticLogsEvents.SourceLoggedFromNotifications,
value: utmUrl.absoluteString)
logSources(url: utmUrl)
}
} 私有函数logSources(url:URL){
let urlString = url.absoluteString
guard let tracker = GAI.sharedInstance().defaultTracker else { return }
// setCampaignParametersFromUrl: parses Google Analytics campaign ("UTM")
// parameters from a string url into a Map that can be set on a Tracker.
let hitParams = GAIDictionaryBuilder()
// Set campaign data on the map, not the tracker directly because it only
// needs to be sent once.
// [hitParams setCampaignParametersFromUrl:urlString]; hitParams.setCampaignParametersFromUrl(urlString)
// Campaign source is the only required campaign field. If previous call
// did not set a campaign source, use the hostname as a referrer instead.
let hitParamsDict = hitParams.build()
tracker.set(kGAIScreenName, value: "AppDelegate")
let builder = GAIDictionaryBuilder.createScreenView().setAll(hitParamsDict as! [AnyHashable : Any])
tracker.send(builder?.build() as! [AnyHashable : Any])
}