Expand my Community achievements bar.

Announcement: Calling all learners and mentors! Applications are now open for the Adobe Analytics 2024 Mentorship Program! Come learn from the best to prepare for an official certification in Adobe Analytics.

Issues with Adobe Analytics integration on iOS using WKWebView

Avatar

Level 1

Hello,

I am currently working on integrating Adobe Analytics into an iOS application using WKWebView. The integration works perfectly on Android, confirming that the credentials (s_account and trackingServer) are correct.

Here's a snippet of the Swift code I'm using:

 

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

    var webView: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Initialize WKWebView and load URL
        let config = WKWebViewConfiguration()
        let userContentController = WKUserContentController()

        if let appMeasurementScript = readAppMeasurementJS() {
            let userScript = WKUserScript(source: appMeasurementScript, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
            userContentController.addUserScript(userScript)
        }

        config.userContentController = userContentController
        webView = WKWebView(frame: self.view.bounds, configuration: config)
        webView.navigationDelegate = self
        webView.uiDelegate = self
        self.view.addSubview(webView)

        if let url = URL(string: "http://example.com") {
            let request = URLRequest(url: url)
            webView.load(request)
        }
    }

    // Function to read AppMeasurement.js from bundle
    private func readAppMeasurementJS() -> String? {
        if let filepath = Bundle.main.path(forResource: "AppMeasurement", ofType: "js") {
            do {
                let contents = try String(contentsOfFile: filepath)
                return contents
            } catch {
                print("Failed to read AppMeasurement.js")
                return nil
            }
        }
        return nil
    }

    // Inject additional script after page load
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        let appMeasurementInitialization = """
            var s_account='accountID';
            var s=s_gi(s_account);
            s.trackingServer='Servername';
            s.pageName=window.location.pathname;
            s.t();
            alert('AppMeasurement script executed');
        """

        webView.evaluateJavaScript(appMeasurementInitialization) { (result, error) in
            if let error = error {
                print("Failed to inject initialization script: \(error.localizedDescription)")
            } else {
                print("Successfully injected initialization script")
            }
        }
    }

    // Handle JavaScript alerts
    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
        print("JavaScript alert: \(message)")
        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in completionHandler() }))
        self.present(alert, animated: true, completion: nil)
    }

    // Allow JavaScript content
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, preferences: WKWebpagePreferences, decisionHandler: @escaping (WKNavigationActionPolicy, WKWebpagePreferences) -> Void) {
        preferences.allowsContentJavaScript = true
        decisionHandler(.allow, preferences)
    }
}

 

Problem: While this code successfully injects and executes the AppMeasurement script on Android devices, it does not seem to log any data in Adobe Analytics when tested on iOS devices. The appMeasurementInitialization script is executed without errors, as evidenced by the alerts and console logs, but no data appears in Adobe Analytics.

Question: Could you please provide guidance on what might be causing this issue specifically on iOS devices? Are there any additional configurations or considerations required for Adobe Analytics integration with WKWebView on iOS?

Thank you in advance for your assistance.

0 Replies