<commit_before>import $ from 'jquery';
import AppConfig from "appConfig";
import getBrowserWindow from "./getBrowserWindow";
import * as _ from 'lodash';
import {log} from "./consoleLog";


export function initializeTracking(){

    if (!_.isEmpty(AppConfig.serverConfig.google_analytics_profile_id)) {
        embedGoogleAnalytics(AppConfig.serverConfig.google_analytics_profile_id!);
    }

}

export function embedGoogleAnalytics(ga_code:string){

    $(document).ready(function() {
        $('<script async src="https://www.google-analytics.com/analytics.js"></script>').appendTo("body");
        $('<script async src="https://cdnjs.cloudflare.com/ajax/libs/autotrack/2.4.1/autotrack.js"></script>').appendTo("body");
        getBrowserWindow().ga=getBrowserWindow().ga||function(){(ga.q=ga.q||[]).push(arguments)};
        const ga:UniversalAnalytics.ga = getBrowserWindow().ga;
        ga.l=+new Date;
        ga('create', ga_code, 'auto');
        ga('require', 'urlChangeTracker');
        ga('require', 'cleanUrlTracker', {
            stripQuery:true,
            trailingSlash:'remove'
        });
        ga('send', 'pageview');
    });
}

export function sendSentryMessage(msg:string) {
    log("sentry message", msg);
    if ((window as any).Sentry) {
        (window as any).Sentry.captureException(new Error(msg));
    }
}

export function getGAInstance(): UniversalAnalytics.ga {

    const ga:UniversalAnalytics.ga = getBrowserWindow().ga;

    return ga || function(){};

}

let queryCount = 0;

enum GACustomFieldsEnum {
  QueryCount = "metric1",
  OQL = "dimension1",
  StudyCount = "metric2",
  Genes = "dimension2",
  VirtualStudy = "dimension3"
};


export function trackQuery(cancerStudyIds:string[], oql:string, geneSymbols:string[], isVirtualStudy:boolean){
    getGAInstance()('send', 'event', 'resultsView', 'query', cancerStudyIds.join(",")+",", {
        [GACustomFieldsEnum.QueryCount]:queryCount++,
        [GACustomFieldsEnum.OQL]:oql,
        [GACustomFieldsEnum.StudyCount]:cancerStudyIds.length,
        [GACustomFieldsEnum.Genes]:geneSymbols.join(",")+",",
        [GACustomFieldsEnum.VirtualStudy]: isVirtualStudy.toString()
    });
}<commit_msg>Tracking: new event to track count of query submissions in one load of application<commit_after>import $ from 'jquery';
import AppConfig from "appConfig";
import getBrowserWindow from "./getBrowserWindow";
import * as _ from 'lodash';
import {log} from "./consoleLog";


export function initializeTracking(){

    if (!_.isEmpty(AppConfig.serverConfig.google_analytics_profile_id)) {
        embedGoogleAnalytics(AppConfig.serverConfig.google_analytics_profile_id!);
    }

}

export function embedGoogleAnalytics(ga_code:string){

    $(document).ready(function() {
        $('<script async src="https://www.google-analytics.com/analytics.js"></script>').appendTo("body");
        $('<script async src="https://cdnjs.cloudflare.com/ajax/libs/autotrack/2.4.1/autotrack.js"></script>').appendTo("body");
        getBrowserWindow().ga=getBrowserWindow().ga||function(){(ga.q=ga.q||[]).push(arguments)};
        const ga:UniversalAnalytics.ga = getBrowserWindow().ga;
        ga.l=+new Date;
        ga('create', ga_code, 'auto');
        ga('require', 'urlChangeTracker');
        ga('require', 'cleanUrlTracker', {
            stripQuery:true,
            trailingSlash:'remove'
        });
        ga('send', 'pageview');
    });
}

export function sendSentryMessage(msg:string) {
    log("sentry message", msg);
    if ((window as any).Sentry) {
        (window as any).Sentry.captureException(new Error(msg));
    }
}

export function getGAInstance(): UniversalAnalytics.ga {

    const ga:UniversalAnalytics.ga = getBrowserWindow().ga;

    return ga || function(){};

}

let queryCount = 0;

enum GACustomFieldsEnum {
  QueryCount = "metric1",
  OQL = "dimension1",
  StudyCount = "metric2",
  Genes = "dimension2",
  VirtualStudy = "dimension3"
};


export function trackQuery(cancerStudyIds:string[], oql:string, geneSymbols:string[], isVirtualStudy:boolean){

    const qCount = queryCount++;

    getGAInstance()('send','event','resultsView','queryCount',qCount);

    getGAInstance()('send', 'event', 'resultsView', 'query', cancerStudyIds.join(",")+",", {
        [GACustomFieldsEnum.QueryCount]:qCount,
        [GACustomFieldsEnum.OQL]:oql,
        [GACustomFieldsEnum.StudyCount]:cancerStudyIds.length,
        [GACustomFieldsEnum.Genes]:geneSymbols.join(",")+",",
        [GACustomFieldsEnum.VirtualStudy]: isVirtualStudy.toString()
    });
}
{'fraction_non_alphanumeric': 0.16079295154185022, 'fraction_numerical': 0.003964757709251102, 'mean_word_length': 4.183789954337899, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '6170905', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1365, 'n_words': 233}