Tıklama davranışını optimize etme

Platform seçin: Android iOS Android (Eski)

Uygulamanız web içeriği göstermek için WKWebView kullanıyorsa aşağıdaki nedenlerden dolayı tıklama davranışını optimize etmeyi düşünebilirsiniz:

  • WKWebView, sekmeli taramayı desteklemez. Yeni sekme açmaya çalışan reklam tıklamaları varsayılan olarak hiçbir işlem yapmaz.

  • Aynı sekmede açılan reklam tıklamaları sayfayı yeniden yükler. Örneğin, H5 oyunları barındırıyorsanız ve her oyunun durumunu korumak istiyorsanız reklam tıklamalarını WKWebView dışında açılmaya zorlayabilirsiniz.

  • Otomatik doldurma özelliği, WKWebView dilinde kredi kartı bilgilerini desteklemiyor. Bu durum, reklamverenler için daha az e-ticaret dönüşümüne yol açarak web içeriğinin para kazanma özelliğini olumsuz etkileyebilir.

Bu kılavuzda, web görünümü içeriğini korurken mobil web görünümlerindeki tıklama davranışını optimize etmek için önerilen adımlar sağlanmaktadır.

Ön koşullar

Uygulama

Reklam bağlantılarının href hedef özelliği _blank, _top, _self veya _parent olarak ayarlanabilir. Reklam bağlantıları, window.open(url, "_blank") gibi JavaScript işlevleri de içerebilir.

Aşağıdaki tabloda, bu bağlantıların her birinin web görünümünde nasıl davrandığı açıklanmaktadır.

href hedef özelliği Varsayılan WKWebView tıklama davranışı
target="_blank" Bağlantı, web görünümü tarafından işlenmez.
target="_top" Mevcut web görünümünde bağlantıyı yeniden yükleyin.
target="_self" Mevcut web görünümünde bağlantıyı yeniden yükleyin.
target="_parent" Mevcut web görünümünde bağlantıyı yeniden yükleyin.
JavaScript işlevi Varsayılan WKWebView tıklama davranışı
window.open(url, "_blank") Bağlantı, web görünümü tarafından işlenmez.

WKWebView örneğinizde tıklama davranışını optimize etmek için aşağıdaki adımları uygulayın:

  1. WKWebView örneğinizde WKUIDelegate değerini ayarlayın.

  2. WKWebView örneğinizde WKNavigationDelegate değerini ayarlayın.

  3. Tıklama URL'sinin davranışını optimize edip etmeyeceğinizi belirleyin.

    • WKNavigationAction nesnesindeki navigationType özelliğinin, optimize etmek istediğiniz bir tıklama türü olup olmadığını kontrol edin. Kod örneği, yalnızca href özelliğine sahip bir bağlantının tıklanması için geçerli olan .linkActivated değerini kontrol eder.

    • WKNavigationAction nesnesindeki targetFrame özelliğini kontrol edin. nil döndürürse gezinme hedefi yeni bir penceredir. WKWebView bu tıklamayı işleyemediğinden, bu tıklamaların manuel olarak işlenmesi gerekir.

  4. URL'yi harici bir tarayıcıda (SFSafariViewController) mı yoksa mevcut web görünümünde mi açacağınıza karar verin. Kod snippet'i, SFSafariViewController göstererek siteden ayrılan URL'lerin nasıl açılacağını gösterir.

Kod örneği

Aşağıdaki kod snippet'i, web görünümü tıklama davranışının nasıl optimize edileceğini gösterir. Örneğin, mevcut alanın hedef alandan farklı olup olmadığını kontrol eder. Kullandığınız ölçütler değişebileceğinden bu yalnızca bir yaklaşımdır.

Swift

import GoogleMobileAds
import SafariServices
import WebKit

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

  override func viewDidLoad() {
    super.viewDidLoad()

    // ... Register the WKWebView.

    // 1. Set the WKUIDelegate on your WKWebView instance.
    webView.uiDelegate = self;
    // 2. Set the WKNavigationDelegate on your WKWebView instance.
    webView.navigationDelegate = self
  }

  // Implement the WKUIDelegate method.
  func webView(
      _ webView: WKWebView,
      createWebViewWith configuration: WKWebViewConfiguration,
      for navigationAction: WKNavigationAction,
      windowFeatures: WKWindowFeatures) -> WKWebView? {
    // 3. Determine whether to optimize the behavior of the click URL.
    if didHandleClickBehavior(
        currentURL: webView.url,
        navigationAction: navigationAction) {
      print("URL opened in SFSafariViewController.")
    }

    return nil
  }

  // Implement the WKNavigationDelegate method.
  func webView(
      _ webView: WKWebView,
      decidePolicyFor navigationAction: WKNavigationAction,
      decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
  {
    // 3. Determine whether to optimize the behavior of the click URL.
    if didHandleClickBehavior(
        currentURL: webView.url,
        navigationAction: navigationAction) {
      return decisionHandler(.cancel)
    }

    decisionHandler(.allow)
  }

  // Implement a helper method to handle click behavior.
  func didHandleClickBehavior(
      currentURL: URL,
      navigationAction: WKNavigationAction) -> Bool {
    guard let targetURL = navigationAction.request.url else {
      return false
    }

    // Handle custom URL schemes such as itms-apps:// by attempting to
    // launch the corresponding application.
    if navigationAction.navigationType == .linkActivated {
      if let scheme = targetURL.scheme, !["http", "https"].contains(scheme) {
        UIApplication.shared.open(targetURL, options: [:], completionHandler: nil)
        return true
      }
    }

    guard let currentDomain = currentURL.host,
      let targetDomain = targetURL.host else {
      return false
    }

    // Check if the navigationType is a link with an href attribute or
    // if the target of the navigation is a new window.
    if (navigationAction.navigationType == .linkActivated ||
      navigationAction.targetFrame == nil) &&
      // If the current domain does not equal the target domain,
      // the assumption is the user is navigating away from the site.
      currentDomain != targetDomain {
      // 4. Open the URL in a SFSafariViewController.
      let safariViewController = SFSafariViewController(url: targetURL)
      present(safariViewController, animated: true)
      return true
    }

    return false
  }
}

Objective-C

@import GoogleMobileAds;
@import SafariServices;
@import WebKit;

@interface ViewController () <WKNavigationDelegate, WKUIDelegate>

@property(nonatomic, strong) WKWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  // ... Register the WKWebView.

  // 1. Set the WKUIDelegate on your WKWebView instance.
  self.webView.uiDelegate = self;
  // 2. Set the WKNavigationDelegate on your WKWebView instance.
  self.webView.navigationDelegate = self;
}

// Implement the WKUIDelegate method.
- (WKWebView *)webView:(WKWebView *)webView
  createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration
             forNavigationAction:(WKNavigationAction *)navigationAction
                  windowFeatures:(WKWindowFeatures *)windowFeatures {
  // 3. Determine whether to optimize the behavior of the click URL.
  if ([self didHandleClickBehaviorForCurrentURL: webView.URL
      navigationAction: navigationAction]) {
    NSLog(@"URL opened in SFSafariViewController.");
  }

  return nil;
}

// Implement the WKNavigationDelegate method.
- (void)webView:(WKWebView *)webView
    decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
                    decisionHandler:
                        (void (^)(WKNavigationActionPolicy))decisionHandler {
  // 3. Determine whether to optimize the behavior of the click URL.
  if ([self didHandleClickBehaviorForCurrentURL: webView.URL
      navigationAction: navigationAction]) {
    decisionHandler(WKNavigationActionPolicyCancel);
    return;
  }

  decisionHandler(WKNavigationActionPolicyAllow);
}

// Implement a helper method to handle click behavior.
- (BOOL)didHandleClickBehaviorForCurrentURL:(NSURL *)currentURL
                    navigationAction:(WKNavigationAction *)navigationAction {
  NSURL *targetURL = navigationAction.request.URL;

  // Handle custom URL schemes such as itms-apps:// by attempting to
  // launch the corresponding application.
  if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
    NSString *scheme = targetURL.scheme;
    if (![scheme isEqualToString:@"http"] && ![scheme isEqualToString:@"https"]) {
      [UIApplication.sharedApplication openURL:targetURL options:@{} completionHandler:nil];
      return YES;
    }
  }

  NSString *currentDomain = currentURL.host;
  NSString *targetDomain = targetURL.host;

  if (!currentDomain || !targetDomain) {
    return NO;
  }

  // Check if the navigationType is a link with an href attribute or
  // if the target of the navigation is a new window.
  if ((navigationAction.navigationType == WKNavigationTypeLinkActivated
      || !navigationAction.targetFrame)
      // If the current domain does not equal the target domain,
      // the assumption is the user is navigating away from the site.
      && ![currentDomain isEqualToString: targetDomain]) {
     // 4. Open the URL in a SFSafariViewController.
    SFSafariViewController *safariViewController =
        [[SFSafariViewController alloc] initWithURL:targetURL];
    [self presentViewController:safariViewController animated:YES
        completion:nil];
    return YES;
  }

  return NO;
}

Sayfada gezinme deneyiminizi test etme

Sayfa gezinme değişikliklerinizi test etmek için

https://google.github.io/webview-ads/test/#click-behavior-tests

ekleyebilirsiniz. Farklı bağlantı türlerini tıklayarak uygulamanızda nasıl davrandıklarını görün.

Kontrol etmeniz gereken şeylerden bazıları aşağıda belirtilmiştir:

  • Her bağlantı, amaçlanan URL'yi açar.
  • Uygulamaya geri dönüldüğünde, sayfa durumunun korunduğunu doğrulamak için test sayfasının sayacı sıfırlanmaz.