All files / core/appinsights/utils appinsightlibs.js

75% Statements 60/80
60% Branches 15/25
68.97% Functions 20/29
75% Lines 60/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 2091x 1x 1x         2x 2x 2x 2x 2x   2x               2x   2x 2x   2x                                                                                                       2x 2x 1x   1x 1x 1x 1x 1x   1x 1x                           1x 1x 132x           132x     1x                   6x 6x 1x   5x 5x 5x 5x 5x   4x 4x                     2x 2x 2x 28x       28x     2x     2x             2x 2x 1x   1x 1x 1x 1x 1x   1x                     1x 1x 22x           22x     1x                   1x
const { google } = require('googleapis');
const config = require('config');
const logger = require('../../../utils/logger');
 
class AppInsightLibs {
 
    getAllRealtimeUsers() {
        return new Promise((resolve, reject) => {
            const scopes = 'https://www.googleapis.com/auth/analytics.readonly';
            const jwt = new google.auth.JWT(config.get('analytics.ganalytics_service_email'), null, config.get('analytics.ganalytics_private_key'), scopes);
            const view_id = config.get('analytics.view_id');
            return jwt.authorize()
                .then((response) => {
                    return google.analytics('v3').data.realtime.get({
                        'auth': jwt,
                        'ids': 'ga:' + view_id,
                        'metrics': 'rt:activeUsers',
                        'dimensions': 'rt:eventCategory',
                    });
                })
                .then((result) => {
                    var analytics = [];
 
                    Eif (result.data.rows)
                        analytics = result.data.rows;
 
                    resolve(analytics);
                })
                .catch((error) => {
                    reject(error);
                });
        });
    }
 
    getAllUser(startDate, endDate) {
        return new Promise((resolve, reject) => {
            if (!startDate || !endDate) {
                reject(new Error("Invalid Inputs"));
            } else {
                const scopes = 'https://www.googleapis.com/auth/analytics.readonly';
                const jwt = new google.auth.JWT(config.get('analytics.ganalytics_service_email'), null, config.get('analytics.ganalytics_private_key'), scopes);
                const view_id = config.get('analytics.view_id');
                var analytics = [];
                return jwt.authorize()
                    .then((response) => {
                        logger.info(response);
                        return google.analytics('v3').data.ga.get({
                            'auth': jwt,
                            'ids': 'ga:' + view_id,
                            'start-date': startDate,
                            'end-date': endDate,
                            'metrics': 'ga:totalEvents',
                            'dimensions': 'ga:eventCategory'
                        });
                    })
                    .then((result) => {
 
                        if (result.data.rows) {
                            result.data.rows.map(datas => {
                                var info = {
                                    user: datas[0],
                                    count: datas[1],
                                };
                                analytics.push(info);
                            });
                        }
                        var totalActivities = analytics.length;
                        //var totalActivities = result.data.totalsForAllResults["ga:totalEvents"];
                        resolve({ result: { totalUsersFound: totalActivities, analytics: analytics } });
                    })
                    .catch((error) => {
                        reject(error);
                    });
            }
        });
    }
 
    getUsersActivities(emailId, pageId) {
        return new Promise((resolve, reject) => {
            if (!emailId || !pageId) {
                reject(new Error("Invalid Inputs"));
            } else {
                const scopes = 'https://www.googleapis.com/auth/analytics.readonly';
                const jwt = new google.auth.JWT(config.get('analytics.ganalytics_service_email'), null, config.get('analytics.ganalytics_private_key'), scopes);
                const view_id = config.get('analytics.view_id');
                var analytics = [];
                return jwt.authorize()
                    .then((response) => {
                        logger.info(response);
                        return google.analytics('v3').data.ga.get({
                            'auth': jwt,
                            'ids': 'ga:' + view_id,
                            'start-date': '30daysAgo',
                            'end-date': 'today',
                            'metrics': 'ga:eventValue',
                            'dimensions': 'ga:eventCategory,ga:eventAction,ga:eventLabel,ga:dateHourMinute',
                            'filters': `ga:eventCategory==${emailId}`,
                            'sort': '-ga:dateHourMinute',
                            'max-results': 1000,
                            'start-index': pageId
                        });
                    })
                    .then((result) => {
                        Eif (result.data.rows) {
                            result.data.rows.map(datas => {
                                var info = {
                                    user: datas[0],
                                    action: datas[1],
                                    activity: datas[2],
                                    datetime: datas[3]
                                };
                                analytics.push(info);
                            });
                        }
                        resolve(analytics);
                    })
                    .catch((error) => {
                        reject(error);
                    });
            }
        });
    }
 
    getUserActionCount(userEmail, startDate, endDate) {
        return new Promise((resolve, reject) => {
            if (!userEmail || !startDate || !endDate) {
                reject(new Error("Invalid Inputs"));
            } else {
                const scopes = 'https://www.googleapis.com/auth/analytics.readonly';
                const jwt = new google.auth.JWT(config.get('analytics.ganalytics_service_email'), null, config.get('analytics.ganalytics_private_key'), scopes);
                const view_id = config.get('analytics.view_id');
                var analytics = [];
                return jwt.authorize()
                    .then((response) => {
                        logger.info(response);
                        return google.analytics('v3').data.ga.get({
                            'auth': jwt,
                            'ids': 'ga:' + view_id,
                            'start-date': startDate,
                            'end-date': endDate,
                            'metrics': 'ga:totalEvents',
                            'dimensions': 'ga:eventAction',
                            'filters': `ga:eventCategory==${userEmail}`
                        });
                    })
                    .then((result) => {
                        var totalActivities = result.data.totalsForAllResults["ga:totalEvents"];
                        Eif (result.data.rows) {
                            result.data.rows.map(datas => {
                                var info = {
                                    action: datas[0],
                                    count: datas[1],
                                };
                                analytics.push(info);
                            });
                        }
                        resolve({ result: { totalActivities: totalActivities, analytics: analytics } });
                    })
                    .catch((error) => {
                        reject(error);
                    });
            }
        });
    }
 
    getRealtimeUsersActivities(email) {
        return new Promise((resolve, reject) => {
            if (!email) {
                reject(new Error("Invalid Inputs"));
            } else {
                const scopes = 'https://www.googleapis.com/auth/analytics.readonly';
                const jwt = new google.auth.JWT(config.get('analytics.ganalytics_service_email'), null, config.get('analytics.ganalytics_private_key'), scopes);
                const view_id = config.get('analytics.view_id');
                var analytics = [];
                return jwt.authorize()
                    .then((response) => {
                        return google.analytics('v3').data.realtime.get({
                            'auth': jwt,
                            'ids': 'ga:' + view_id,
                            'max-results': 1000,
                            'metrics': 'rt:totalEvents',
                            'dimensions': 'rt:eventCategory,rt:eventAction,rt:eventLabel,rt:minutesAgo',
                            'filters': `rt:eventCategory==${email}`,
                            'sort': 'rt:minutesAgo'
                        });
                    })
                    .then((result) => {
                        Eif (result.data.rows) {
                            result.data.rows.map(datas => {
                                var info = {
                                    user: datas[0],
                                    action: datas[1],
                                    activity: datas[2],
                                    minutesAgo: datas[3]
                                };
                                analytics.push(info);
                            });
                        }
                        resolve(analytics);
                    })
                    .catch((error) => {
                        reject(error);
                    });
            }
        });
    }
}
 
module.exports = AppInsightLibs;