简体   繁体   中英

How to disable Google Tag Manager console logging

After I added Google Tag Manager to the project I see lots its log entries in the console. Is there a way to disable it? Console log is full of noise:

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;
}

I just had this problem in a project that combines Google Tag Manager and Firebase. Since no header about logging is exposed I couldn't find a way to turn it off.

This is a monkey patch I came up with that lets you control the info logs from 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 3 version of Scoud's solution:

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
}

You didn't specify the language. Warning level would seem enough in your case.

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

Available levels ( docs ):

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

From the official docs: https://developers.google.com/tag-manager/ios/v3/#logger (deprecated in favor of Firebase Analytics)

After a lot of digging, I managed to find a way to disable warning and error logs (not info logs unfortunately) in Google Tag Manager v7.0.0.

Code below is written in Swift 5:

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
}

Extension for opaque pointer:

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($0.lowercased()) }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM