如何禁用 Google 跟踪代码管理器控制台日志记录

How to disable Google Tag Manager console logging

将 Google 标签管理器添加到项目后,我在控制台中看到了很多它的日志条目。有没有办法禁用它?控制台日志充满噪音:

GoogleTagManager info: Processing logged event: _vs with parameters: {
    "_o" = auto;
    "_pc" = UIViewController;
    "_pi" = "-3988739357756819671";
    "_sc" = "Bubbie.MamboBamboViewController";
    "_si" = "-3988739357756819670";
}
2017-07-27 12:01:09.744 BubbieHuff[77205:6894827] GoogleTagManager info: Processing logged event: show_view with parameters: {
    "_sc" = "Bubbie.MamboBamboViewController";
    "_si" = "-3988739357756819670";
    name = Mambo;
}

您没有指定语言。在您的情况下,警告级别似乎足够了。

// Optional: Change the LogLevel to Verbose to enable logging at VERBOSE and higher levels.
[self.tagManager.logger setLogLevel:kTAGLoggerLogLevelVerbose];

可用级别(docs):

  • kTAGLoggerLogLevelVerbose
  • kTAGLoggerLogLevelDebug
  • kTAGLoggerLogLevelInfo
  • kTAGLoggerLogLevelWarning
  • kTAGLoggerLogLevelError
  • kTAGLoggerLogLevelNone

来自官方文档: https://developers.google.com/tag-manager/ios/v3/#logger(已弃用,支持 Firebase Analytics)

我刚刚在结合了 Google 跟踪代码管理器和 Firebase 的项目中遇到了这个问题。由于没有 header 公开有关日志记录的信息,因此我找不到关闭它的方法。

这是我想出的一个猴子补丁,可以让你控制来自 GTM 的信息日志。

+ (void)patchGoogleTagManagerLogging {

    Class class = NSClassFromString(@"TAGLogger");

    SEL originalSelector = NSSelectorFromString(@"info:");
    SEL detourSelector = @selector(detour_info:);

    Method originalMethod = class_getClassMethod(class, originalSelector);
    Method detourMethod = class_getClassMethod([self class], detourSelector);

    class_addMethod(class,
                    detourSelector,
                    method_getImplementation(detourMethod),
                    method_getTypeEncoding(detourMethod));

    method_exchangeImplementations(originalMethod, detourMethod);
}


+ (void)detour_info:(NSString*)message {
    return; // Disable logging
}

Swift Scoud 解决方案的 3 版本:

static func hideGTMLogs() {
    let tagClass: AnyClass? = NSClassFromString("TAGLogger")

    let originalSelector = NSSelectorFromString("info:")
    let detourSelector = #selector(AppDelegate.detour_info(message:))

    guard let originalMethod = class_getClassMethod(tagClass, originalSelector),
        let detourMethod = class_getClassMethod(AppDelegate.self, detourSelector) else { return }

    class_addMethod(tagClass, detourSelector,
                    method_getImplementation(detourMethod), method_getTypeEncoding(detourMethod))
    method_exchangeImplementations(originalMethod, detourMethod)
}

@objc
static func detour_info(message: String) {
    return
}

经过大量挖掘,我设法在 Google 跟踪代码管理器 v7.0.0 中找到了禁用警告和错误日志(不幸的是不是信息日志)的方法。

下面的代码写在Swift5:

static func turnOffGTMLogs() {

        let tagClass: AnyClass? = NSClassFromString("TAGJSExportedInstructions")

        guard
            var properties = class_copyMethodList(tagClass, nil)
            else { return }

        let detourSelector = #selector(FirebaseInitializer.detour_logMessage(with:message:))
        var pointed = properties.pointee
        while(!pointed.isNil()) {
            if method_getName(pointed).coreStoreDumpString.contains("logMessage") {
                guard let detourMethod = class_getClassMethod(FirebaseInitializer.self, detourSelector) else { return }
                let _ = class_replaceMethod(tagClass, method_getName(pointed), method_getImplementation(detourMethod), method_getTypeEncoding(pointed))
                break
            }
            properties = properties.advanced(by: 1)
            pointed = properties.pointee
        }
    }

@objc
static func detour_logMessage(with level: Int, message: String) {
    return
}

不透明指针的扩展:

private extension OpaquePointer {
/*Used to check if value pointed by the opaque pointer is nil (to silence compiler warnings as self == nil would also work)
It works by figuring out whether the pointer is a nil pointer, from it's debug description .*/
func isNil() -> Bool {
    return !self.debugDescription.contains { "123456789abcdef".contains([=11=].lowercased()) }
}

之后你只需要调用 turnOffGTMLogs() 一次(最好是在你初始化 GTM 的同一个地方,通常是在 AppDelegate 中)来关闭日志输出。

在 Swift 5 中,要自动静默来自 GTAG 的信息和警告日志,只需在您的应用委托中插入并调用这两个方法(hideGTMLogsInfo() 和 hideGTMLogsWarning()):

static func hideGTMLogsInfo() {
    let tagClass: AnyClass? = NSClassFromString("TAGLogger")

    let originalSelector = NSSelectorFromString("info:")
    let detourSelector = #selector(AppDelegate.detour_info(message:))

    guard let originalMethod = class_getClassMethod(tagClass, originalSelector),
        let detourMethod = class_getClassMethod(AppDelegate.self, detourSelector) else { return }

    class_addMethod(tagClass, detourSelector,
                    method_getImplementation(detourMethod), method_getTypeEncoding(detourMethod))
    method_exchangeImplementations(originalMethod, detourMethod)
}

static func hideGTMLogsWarning() {
    let tagClass: AnyClass? = NSClassFromString("TAGLogger")

    let originalSelector = NSSelectorFromString("warning:")
    let detourSelector = #selector(AppDelegate.detour_warning(message:))

    guard let originalMethod = class_getClassMethod(tagClass, originalSelector),
        let detourMethod = class_getClassMethod(AppDelegate.self, detourSelector) else { return }

    class_addMethod(tagClass, detourSelector,
                    method_getImplementation(detourMethod), method_getTypeEncoding(detourMethod))
    method_exchangeImplementations(originalMethod, detourMethod)
}

@objc
static func detour_warning(message: String) {
    return
}
@objc
static func detour_info(message: String) {
    return
}

这是 Swift 5 的清理版本,作为 class 您可以像这样在 AppDelegate 中使用:

GoogleTagManagerHelper.hideGTMLogs()

import Foundation

internal final class GoogleTagManagerHelper {

    enum LogLevel: String, CaseIterable {
        case info = "info:"
        case warning = "warning:"
    }

    static func hideGTMLogs() {
        LogLevel.allCases.forEach { hideGTMLogs(for: [=11=]) }
    }

    static func hideGTMLogs(for level: GoogleTagManagerHelper.LogLevel) {
        let tagClass: AnyClass? = NSClassFromString("TAGLogger")
        let originalSelector = NSSelectorFromString(level.rawValue)
        let detourSelector = #selector(detour_log(message:))
        guard
            let originalMethod = class_getClassMethod(tagClass, originalSelector),
              let detourMethod = class_getClassMethod(GoogleTagManagerHelper.self, detourSelector)
        else { return }
        class_addMethod(
            tagClass,
            detourSelector,
            method_getImplementation(detourMethod),
            method_getTypeEncoding(detourMethod)
        )
        method_exchangeImplementations(originalMethod, detourMethod)
    }

    @objc static func detour_log(message: String) { return }
}