diff --git a/JavaScript/JavaScriptSDK/AppInsights.ts b/JavaScript/JavaScriptSDK/AppInsights.ts index addaf490..430b043b 100644 --- a/JavaScript/JavaScriptSDK/AppInsights.ts +++ b/JavaScript/JavaScriptSDK/AppInsights.ts @@ -8,6 +8,7 @@ /// /// /// +/// module Microsoft.ApplicationInsights { @@ -54,7 +55,7 @@ module Microsoft.ApplicationInsights { * The main API that sends telemetry to Application Insights. * Learn more: http://go.microsoft.com/fwlink/?LinkID=401493 */ - export class AppInsights implements IAppInsightsInternal { + export class AppInsights implements IAppInsightsInternal, IAppInsights { // Counts number of trackAjax invokations. // By default we only monitor X ajax call per view to avoid too much load. diff --git a/JavaScript/JavaScriptSDK/AppInsightsModule.ts b/JavaScript/JavaScriptSDK/AppInsightsModule.ts new file mode 100644 index 00000000..7d7b60e4 --- /dev/null +++ b/JavaScript/JavaScriptSDK/AppInsightsModule.ts @@ -0,0 +1,69 @@ +/// +/// + +class AppInsightsModule { + private static appInsightsName = "appInsights"; + public static appInsightsInstance: Microsoft.ApplicationInsights.IAppInsights; + + private static _createLazyMethod(name) { + var aiObject = window[AppInsightsModule.appInsightsName]; + + // Define a temporary method that queues-up a the real method call + aiObject[name] = function () { + // Capture the original arguments passed to the method + var originalArguments = arguments; + // If the queue is available, it means that the function wasn't yet replaced with actual function value + if (aiObject.queue) { + aiObject.queue.push(() => aiObject[name].apply(aiObject, originalArguments)); + } + else { + // otheriwse execute the function + aiObject[name].apply(aiObject, originalArguments); + } + } + + AppInsightsModule.appInsightsInstance[name] = aiObject[name]; + }; + + public static initialize(aiConfig: Microsoft.ApplicationInsights.IConfig) { + + + if (!window[AppInsightsModule.appInsightsName]) { + window[AppInsightsModule.appInsightsName] = { + config: aiConfig + }; + + var scriptElement = document.createElement("script"); + scriptElement.src = "http://az416426.vo.msecnd.net/scripts/a/ai.0.js"; + document.head.appendChild(scriptElement); + + var aiObject = window[AppInsightsModule.appInsightsName]; + + // capture initial cookie + aiObject.cookie = document.cookie; + aiObject.queue = []; + + var method = ["trackEvent", "trackException", "trackMetric", "trackPageView", "trackTrace", "trackAjax", "setAuthenticatedUserContext", "clearAuthenticatedUserContext"]; + while (method.length) { + AppInsightsModule._createLazyMethod(method.pop()); + } + + // collect global errors + if (!aiConfig.disableExceptionTracking) { + AppInsightsModule._createLazyMethod("_onerror"); + var originalOnError = window["_onerror"]; + window["_onerror"] = function (message, url, lineNumber, columnNumber, error) { + var handled = originalOnError && originalOnError(message, url, lineNumber, columnNumber, error); + if (handled !== true) { + aiObject["_onerror"](message, url, lineNumber, columnNumber, error); + } + + return handled; + }; + } + } + } +} + +export var initialize: (config: Microsoft.ApplicationInsights.IConfig) => void = AppInsightsModule.initialize; +export var AppInsights: Microsoft.ApplicationInsights.IAppInsights = AppInsightsModule.appInsightsInstance; \ No newline at end of file diff --git a/JavaScript/JavaScriptSDK/IAppInsights.ts b/JavaScript/JavaScriptSDK/IAppInsights.ts new file mode 100644 index 00000000..68ddeec0 --- /dev/null +++ b/JavaScript/JavaScriptSDK/IAppInsights.ts @@ -0,0 +1,59 @@ +module Microsoft.ApplicationInsights { + export interface IAppInsights { + + /** + * Starts timing how long the user views a page or other item. Call this when the page opens. + * This method doesn't send any telemetry. Call {@link stopTrackTelemetry} to log the page when it closes. + * @param name A string that idenfities this item, unique within this HTML document. Defaults to the document title. + */ + startTrackPage(name?: string); + + /** + * Logs how long a page or other item was visible, after {@link startTrackPage}. Call this when the page closes. + * @param name The string you used as the name in startTrackPage. Defaults to the document title. + * @param url String - a relative or absolute URL that identifies the page or other item. Defaults to the window location. + * @param properties map[string, string] - additional data used to filter pages and metrics in the portal. Defaults to empty. + * @param measurements map[string, number] - metrics associated with this page, displayed in Metrics Explorer on the portal. Defaults to empty. + */ + stopTrackPage(name?: string, url?: string, properties?: Object, measurements?: Object); + + /** + * Logs that a page or other item was viewed. + * @param name The string you used as the name in startTrackPage. Defaults to the document title. + * @param url String - a relative or absolute URL that identifies the page or other item. Defaults to the window location. + * @param properties map[string, string] - additional data used to filter pages and metrics in the portal. Defaults to empty. + * @param measurements map[string, number] - metrics associated with this page, displayed in Metrics Explorer on the portal. Defaults to empty. + * @param duration number - the number of milliseconds it took to load the page. Defaults to undefined. If set to default value, page load time is calculated internally. + */ + trackPageView(name?: string, url?: string, properties?: Object, measurements?: Object, duration?: number); + + /** + * Start timing an extended event. Call {@link stopTrackEvent} to log the event when it ends. + * @param name A string that identifies this event uniquely within the document. + */ + startTrackEvent(name: string); + + + /** + * Log an extended event that you started timing with {@link startTrackEvent}. + * @param name The string you used to identify this event in startTrackEvent. + * @param properties map[string, string] - additional data used to filter events and metrics in the portal. Defaults to empty. + * @param measurements map[string, number] - metrics associated with this event, displayed in Metrics Explorer on the portal. Defaults to empty. + */ + stopTrackEvent(name: string, properties?: Object, measurements?: Object); + + /** + * Log a user action or other occurrence. + * @param name A string to identify this event in the portal. + * @param properties map[string, string] - additional data used to filter events and metrics in the portal. Defaults to empty. + * @param measurements map[string, number] - metrics associated with this event, displayed in Metrics Explorer on the portal. Defaults to empty. + */ + trackEvent(name: string, properties?: Object, measurements?: Object); + + /** + * Log an AJAX request + * @param + */ + trackAjax(id: string, absoluteUrl: string, pathName: string, totalTime: number, success: boolean, resultCode: number, method?: string); + } +} \ No newline at end of file diff --git a/JavaScript/JavaScriptSDK/JavaScriptSDK.csproj b/JavaScript/JavaScriptSDK/JavaScriptSDK.csproj index 6fd8dd8f..18dd8ae9 100644 --- a/JavaScript/JavaScriptSDK/JavaScriptSDK.csproj +++ b/JavaScript/JavaScriptSDK/JavaScriptSDK.csproj @@ -16,7 +16,6 @@ ..\ true - 12.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) ApplicationInsights.Javascript @@ -25,8 +24,10 @@ min\$(OutFileBaseName).$(AIVersionMajor) min\$(OutFileBaseName).js True + AMD 2008 + @@ -123,6 +124,8 @@ + + diff --git a/package.json b/package.json index cbc80f53..978289ab 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,15 @@ { "name": "applicationinsights-js", "version": "0.22.14", - "description": "[Application Insights](https://azure.microsoft.com/services/application-insights/) tells you about your app\u0027s performance and usage. By adding a few lines of code to your web pages, you get data about how many users you have, which pages are most popular, how fast pages load, whether they throw exceptions, and more. And you can add code to track more detailed user activity.", + "description": "Microsoft Application Insights JavaScript SDK", "main": "dist/ai.0.js", + "keywords": [ + "browser performance monitoring", + "script errors", + "application insights", + "microsoft", + "azure" + ], "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, @@ -10,7 +17,7 @@ "type": "git", "url": "git+https://github.com/Microsoft/ApplicationInsights-JS.git" }, - "author": "", + "author": "Microsoft Application Insights Team", "license": "MIT", "bugs": { "url": "https://github.com/Microsoft/ApplicationInsights-JS/issues"