@raona/ga
Version:
Raona utilities to work with Google Analytics
157 lines (150 loc) • 5.55 kB
TypeScript
export declare type IntranetEvent = '';
export declare const GA_Info: {
UserInfo: string;
PageInfo: string;
PageView: string;
};
export declare type GA_Action = 'Liked' | 'Unliked' | 'Favorite' | 'Unfavorite' | 'Card Click' | 'Añadir Quick Link' | 'Quitar Quick Link' | 'Results' | 'No results';
export declare type GA_Category = 'Interacción' | 'Search';
/** User data needed to report to Google Analytics */
export interface GA_User {
/** User language */
language: string;
/** User office or country */
office: string;
/** User department */
department: string;
/** User jobtitle */
jobtitle: string;
}
export interface GA_View {
viewOrigin: string;
originUrl: string;
sectionName: string;
}
/** Structure for the needed info when a concrete element is treated */
export interface ItemInfo {
/**Dimension 6*/
contentType: string;
/**Dimension 7*/
category: string;
/**Metric 1*/
elementId: number;
/**Dimension 9*/
destinationUrl?: string;
/**Dimension 11 */
keywords?: string[];
/**Event label for Interaction */
title?: string;
}
/** Current structure, the ones with * are optional:
DIMENSIONS:
* Dimension1 === User Language
* Dimension2 === User Country
* Dimension3 === User Department
* Dimension4 === User JobTitle
* Dimension5 === View origin (If the visit comes from the Intranet or external source, add a query string in your code for this)
* *Dimension6 === Content type of the element that generates de trace
* *Dimension7 === Element Category (Category of the element that generates the trace)
* Dimension8 === Origin Url (Url where the trace is launched)
* *Dimension9 === Destination Url (in case it's a redirect)
* Dimension10 === Webpart (will be called Section) name that launches the trace
* Dimension11 === User info, this is an agregation of dimension1,d2,d3,d4. Why? PowerBI has a limitation of 8 variables being used at the same time. Agregated so the report can show more info
METRICS:
* *Metric1 === ID of the element that interacts.
EVENTS CATEGORIES: GA_Category
EVENTS ACTIONS: GA_Action
*/
export declare class GoogleAnalyticsService {
private static readonly GLOBAL_KEY;
private gaTrackingID;
private previousPage;
private startTime;
private DEBUG;
private user;
private view;
private itemInfo;
/**
* Initializes Google Analytics
* @param gaTrackingID Requiered track ID. Should be in "UA-XXXXXXXXX-X" format
* @throws Exception if the track ID doesn't follow the required format
*/
constructor(gaTrackingID: string, debugMode?: boolean);
/** ---------- INITIALIZE ---------- */
/**
* Function that inserts the GA script onto the page
*/
insertGAScript(): void;
/**
* Test if the ga function exists on window, if not, it reinserts the script onto it
*/
testGA(): void;
/** ----------- SETTERS ---------- */
/**
* Sets/Updates the user information saved onto the service
* @param user The user information
*/
setUser(user: GA_User): void;
setView(view: GA_View): void;
setItemInfo(itemInfo: ItemInfo): void;
/**
* Generates the object for the custom dimensions and metrics
*/
getDMObject(): any;
/**
* Sends a page view of the current location.
*/
currentLocationView(): void;
/**
* Sends a page view of the current location. Specifically sends: location.pathname
* @param url The url being viewed
*/
pageView(url: string): void;
/**
* Sends a page view of the current location. Specifically sends: location.pathname
*/
intranetView(): void;
/**
* Sets the start time to take note when the page loads
*/
startTiming(): void;
/**
* Sends timing for the current page visit
*/
exitPage(): void;
/**
* Sends a custom event to GA
* @param category Category of the event, includes the action in itself
* @param action Action done that triggered the event
* @param label Any description you want to give the event
*/
event(category: string, action: string, label: string): void;
/**
* Sends an intranet event
* @param category A category for the event, limited options to events that can happen on the intranet
* @param action An action for the event, limited options to events that can happen on the intranet
* @requires itemInfo to be set beforehand
*/
intranetEvent(category: GA_Category, action: GA_Action): void;
/**
* Sends a trace when is invoked about if a search value ends having results or not
* @param hasResults If the search done has generated results
* @param section Where the search was done (subsite or webpart)
* @param searchValue The value searched
*/
searchEvent(hasResults: boolean, searchValue: string): void;
/** --------- UTILS ---------- */
/**
* console.warn IF it's debugging
* @param warning Warning string message
*/
private warn;
/**
* console.error IF it's debugging
* @param error Error message
*/
private error;
/** --------- DEBUG & DISCOVERY PURPOSES ---------- */
private pageViewDebug;
private customEventDebug;
}