<commit_before>var application = require("application");

var settings = {
    logging: null
};

exports.initalize = function (config) {
    if(config.trackingId){
        var configureError = new interop.Reference();
        GGLContext.sharedInstance().configureWithError(configureError);

        var gai = GAI.sharedInstance();
        gai.trackUncaughtExceptions = true;
        var defaultTracker = gai.trackerWithTrackingId(config.trackingId);

        
        if(config.dispatchInterval){
            gai.dispatchInterval = config.dispatchInterval;
        } 

        if(config.userId){
            var gAIUserId = "&uid"; //kGAIUserId
            GAITracker.prototype.setValue.call(defaultTracker, gAIUserId, config.userId);
        }

        if(config.logging){
            settings.logging = config.logging;
             
             if(config.logging.native){
                var logLevel = 4; //kGAILogLevelVerbose
                gai.logger.logLevel = logLevel;
             }
        }
        
        global.gaTracker = defaultTracker;
        global.gaInstance = gai;
        
        //setup timers
        global.gaTimers = [];
    }else{
        throw "Sup boss, how do you plan on tracking with no trackingId?  Please add it to the config";
    }
}

exports.logView = function(viewname){
    logToConsole("Analytics Event: Log Screen View: " + viewname + " at " + new Date());
    
    var gAIScreenName =  "&cd"; //kGAIScreenName
    var event = GAIDictionaryBuilder.createScreenView().setForKey(viewname, gAIScreenName);
    var builtEvent = event.build();

    if(global.gaTracker)
        GAITracker.prototype.send.call(global.gaTracker, builtEvent);
    else
        logToConsole("Unable to locate tracker to log view");
}

exports.logEvent = function(data){
    logToConsole("Analytics Event:" + JSON.stringify(data) + " at " + new Date());
    var event = GAIDictionaryBuilder.createEventWithCategoryActionLabelValue(
      data.category,
      data.action,
      data.label,
      data.value  
    );
    var builtEvent = event.build();
    
    if(global.gaTracker)
        GAITracker.prototype.send.call(global.gaTracker, builtEvent);
    else
        logToConsole("Unable to locate tracker to log event");
}

exports.logException = function (data) {
    var description = "";
    var fatal = "";
    
    if( typeof data === 'object') {
        description = data.description;
        fatal = (data.fatal) ? data.fatal : false;
    }
    else {
        //not object
        description = data;
        fatal = false;
    }
    
    logToConsole("Analytics Logging Exception: " + description);

    var event = GAIDictionaryBuilder.createExceptionWithDescriptionWithFatal(description, fatal);
    var builtEvent = event.build();
    
    GAITracker.prototype.send.call(global.gaTracker, builtEvent);
}



exports.dispatch = function (args){
    logToConsole("Analytics Flushing dispatch event queue");
    GAI.sharedInstance().dispatch();
};


exports.getTracker = function () {
    return global.gaTracker;
}



//## TIMING FUNCTIONS ##
//Raw Timer
exports.logTimingEvent = function(data){
    logTiming(data);
}

//Start
exports.startTimer = function (timerName, data) {
    global.gaTimers.push({ 
        name: timerName,
        value: new Date(),
        data: data
    })
}

//End
exports.stopTimer = function (timerName) {
    var endTime = new Date();
    var foundTimer = false;

    //Find the timer
    for(var i = 0; i < global.gaTimers.length; i++){
        var timer = global.gaTimers[i];
        if(timer.name == timerName){
            foundTimer = true;

            //set timer
            timer.data.value = endTime.getTime() - timer.value.getTime();

            //Process event
            logTiming(timer.data);
            global.gaTimers.splice(i,1);
            break;
        }
    }   
    
    if(!foundTimer){
        logToConsole("Unable to find timer start event named " + timerName);
    }
}

function logTiming(data){
    var event = GAIDictionaryBuilder.createTimingWithCategoryIntervalNameLabel(
      data.category,
      data.value,
      data.name,
      data.label
    );

    var builtEvent = event.build();

    if(global.gaTracker){
        GAITracker.prototype.send.call(global.gaTracker, builtEvent);
        logToConsole("Analytics Timing Event:" + JSON.stringify(data) + " at " + new Date());
    }
    else
        logToConsole("Unable to locate tracker to log event");
}

function logToConsole(message){
    if(settings.logging){
        if(settings.logging.console){
            console.log(message);
        }
    }
}<commit_msg>added ios demographics tracking support<commit_after>var application = require("application");

var settings = {
    logging: null
};

exports.initalize = function (config) {
    if(config.trackingId){
        var configureError = new interop.Reference();
        GGLContext.sharedInstance().configureWithError(configureError);

        var gai = GAI.sharedInstance();
        gai.trackUncaughtExceptions = true;
        var defaultTracker = gai.trackerWithTrackingId(config.trackingId);

        if (config.hasOwnProperty('enableDemographics')) {
            if (config.enableDemographics) {
                defaultTracker.allowIDFACollection = true;
            }
        }        

        if(config.dispatchInterval){
            gai.dispatchInterval = config.dispatchInterval;
        } 

        if(config.userId){
            var gAIUserId = "&uid"; //kGAIUserId
            GAITracker.prototype.setValue.call(defaultTracker, gAIUserId, config.userId);
        }

        if(config.logging){
            settings.logging = config.logging;
             
             if(config.logging.native){
                var logLevel = 4; //kGAILogLevelVerbose
                gai.logger.logLevel = logLevel;
             }
        }
        
        global.gaTracker = defaultTracker;
        global.gaInstance = gai;
        
        //setup timers
        global.gaTimers = [];
    }else{
        throw "Sup boss, how do you plan on tracking with no trackingId?  Please add it to the config";
    }
}

exports.logView = function(viewname){
    logToConsole("Analytics Event: Log Screen View: " + viewname + " at " + new Date());
    
    var gAIScreenName =  "&cd"; //kGAIScreenName
    var event = GAIDictionaryBuilder.createScreenView().setForKey(viewname, gAIScreenName);
    var builtEvent = event.build();

    if(global.gaTracker)
        GAITracker.prototype.send.call(global.gaTracker, builtEvent);
    else
        logToConsole("Unable to locate tracker to log view");
}

exports.logEvent = function(data){
    logToConsole("Analytics Event:" + JSON.stringify(data) + " at " + new Date());
    var event = GAIDictionaryBuilder.createEventWithCategoryActionLabelValue(
      data.category,
      data.action,
      data.label,
      data.value  
    );
    var builtEvent = event.build();
    
    if(global.gaTracker)
        GAITracker.prototype.send.call(global.gaTracker, builtEvent);
    else
        logToConsole("Unable to locate tracker to log event");
}

exports.logException = function (data) {
    var description = "";
    var fatal = "";
    
    if( typeof data === 'object') {
        description = data.description;
        fatal = (data.fatal) ? data.fatal : false;
    }
    else {
        //not object
        description = data;
        fatal = false;
    }
    
    logToConsole("Analytics Logging Exception: " + description);

    var event = GAIDictionaryBuilder.createExceptionWithDescriptionWithFatal(description, fatal);
    var builtEvent = event.build();
    
    GAITracker.prototype.send.call(global.gaTracker, builtEvent);
}



exports.dispatch = function (args){
    logToConsole("Analytics Flushing dispatch event queue");
    GAI.sharedInstance().dispatch();
};


exports.getTracker = function () {
    return global.gaTracker;
}



//## TIMING FUNCTIONS ##
//Raw Timer
exports.logTimingEvent = function(data){
    logTiming(data);
}

//Start
exports.startTimer = function (timerName, data) {
    global.gaTimers.push({ 
        name: timerName,
        value: new Date(),
        data: data
    })
}

//End
exports.stopTimer = function (timerName) {
    var endTime = new Date();
    var foundTimer = false;

    //Find the timer
    for(var i = 0; i < global.gaTimers.length; i++){
        var timer = global.gaTimers[i];
        if(timer.name == timerName){
            foundTimer = true;

            //set timer
            timer.data.value = endTime.getTime() - timer.value.getTime();

            //Process event
            logTiming(timer.data);
            global.gaTimers.splice(i,1);
            break;
        }
    }   
    
    if(!foundTimer){
        logToConsole("Unable to find timer start event named " + timerName);
    }
}

function logTiming(data){
    var event = GAIDictionaryBuilder.createTimingWithCategoryIntervalNameLabel(
      data.category,
      data.value,
      data.name,
      data.label
    );

    var builtEvent = event.build();

    if(global.gaTracker){
        GAITracker.prototype.send.call(global.gaTracker, builtEvent);
        logToConsole("Analytics Timing Event:" + JSON.stringify(data) + " at " + new Date());
    }
    else
        logToConsole("Unable to locate tracker to log event");
}

function logToConsole(message){
    if(settings.logging){
        if(settings.logging.console){
            console.log(message);
        }
    }
}

{'fraction_non_alphanumeric': 0.11277001270648031, 'fraction_numerical': 0.0006353240152477764, 'mean_word_length': 2.3804581245526126, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7107418', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2690, 'n_words': 578}