Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for Joomla gzip #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
63 changes: 32 additions & 31 deletions src/Client_Instrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,56 @@

defined('_JEXEC') or die;

use Joomla\CMS\Factory;

/**
* Does client-side instrumentation using the Javascript SDK for Application Insights
* @copyright Copyright 2015. All rights re-served.
* @license No information.
*/
class Client_Instrumentation
{
/**
* Add prefix
*
* @param string $_instrumentationkey
*
* @param string $_title
*/
function addPrefix($_instrumentationkey, $_title) {
$rawSnippet = '<script type="text/javascript">
var appInsights=window.appInsights||function(config){
function s(config){t[config]=function(){var i=arguments;t.queue.push(function(){t[config].apply(t,i)})}}var t={config:config},r=document,f=window,e="script",o=r.createElement(e),i,u;for(o.src=config.url||"//az416426.vo.msecnd.net/scripts/a/ai.0.js",r.getElementsByTagName(e)[0].parentNode.appendChild(o),t.cookie=r.cookie,t.queue=[],i=["Event","Exception","Metric","PageView","Trace"];i.length;)s("track"+i.pop());return config.disableExceptionTracking||(i="onerror",s("_"+i),u=f[i],f[i]=function(config,r,f,e,o){var s=u&&u(config,r,f,e,o);return s!==!0&&t["_"+i](config,r,f,e,o),s}),t
}({
instrumentationKey:"INSTRUMENTATION_KEY"
});

window.appInsights=appInsights;
appInsights.trackPageView("PAGE_NAME", PAGE_URL);
</script>';

/**
* Add prefix
*
* @param string $_instrumentationkey
*
* @param string $_title
*/
function addPrefix($_instrumentationkey, $_title)
{
$rawSnippet = <<<JOOMLAAISCRIPT
var sdkInstance="appInsightsSDK";window[sdkInstance]="appInsights";var aiName=window[sdkInstance],aisdk=window[aiName]||function(e){function n(e){t[e]=function(){var n=arguments;t.queue.push(function(){t[e].apply(t,n)})}}var t={config:e};t.initialize=!0;var i=document,a=window;setTimeout(function(){var n=i.createElement("script");n.src=e.url||"https://az416426.vo.msecnd.net/scripts/b/ai.2.min.js",i.getElementsByTagName("script")[0].parentNode.appendChild(n)});try{t.cookie=i.cookie}catch(e){}t.queue=[],t.version=2;for(var r=["Event","PageView","Exception","Trace","DependencyData","Metric","PageViewPerformance"];r.length;)n("track"+r.pop());n("startTrackPage"),n("stopTrackPage");var s="Track"+r[0];if(n("start"+s),n("stop"+s),n("setAuthenticatedUserContext"),n("clearAuthenticatedUserContext"),n("flush"),!(!0===e.disableExceptionTracking||e.extensionConfig&&e.extensionConfig.ApplicationInsightsAnalytics&&!0===e.extensionConfig.ApplicationInsightsAnalytics.disableExceptionTracking)){n("_"+(r="onerror"));var o=a[r];a[r]=function(e,n,i,a,s){var c=o&&o(e,n,i,a,s);return!0!==c&&t["_"+r]({message:e,url:n,lineNumber:i,columnNumber:a,error:s}),c},e.autoExceptionInstrumented=!0}return t}(
{
instrumentationKey:"INSTRUMENTATION_KEY"
}
);window[aiName]=aisdk,aisdk.queue&&0===aisdk.queue.length&&aisdk.trackPageView({});
window.appInsights.trackPageView({name: 'PAGE_NAME', url: 'PAGE_URL'});
JOOMLAAISCRIPT;

$patterns = array();
$replacements = array();

$patterns[0] = '/INSTRUMENTATION_KEY/';
$patterns[1] = '/PAGE_NAME/';
$patterns[2] = '/PAGE_URL/';

// Sets Instrumentation Key
$replacements[0] = $_instrumentationkey;

// Sets the page title
$replacements[1] = $_title;
$replacements[1] = json_encode($_title, JSON_HEX_APOS);

// Validate if displaying home page
if ($_title == 'Home')
{
if ($_title == 'Home') {
$replacements[2] = 'window.location.origin';
}
else
{
$replacements[2] = 'window.location.origin + "/'.rawurlencode($_title).'"';
} else {
$replacements[2] = 'window.location.origin + "/' . rawurlencode($_title) . '"';
}

echo preg_replace($patterns, $replacements, $rawSnippet);
$document = Factory::getDocument();

// Add Javascript
$document->addScriptDeclaration(preg_replace($patterns, $replacements, $rawSnippet));
}
}
8 changes: 7 additions & 1 deletion src/Server_Instrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ function endRequest()
$requestName = $this->getRequestName();
$startTime = $_SERVER["REQUEST_TIME"];
$duration = ($this->getMicrotime() - $this->_startTime) * 1000;
$this->_telemetryClient->trackRequest($requestName, $url, $startTime, $duration);
$responseCode = http_response_code();
if ($responseCode >= 200 && $responseCode < 400 ) {
$succesFull = true;
} else {
$succesFull = false;
}
$this->_telemetryClient->trackRequest($requestName, $url, $startTime, $duration, $responseCode, $succesFull);

// Flush all telemetry items
$this->_telemetryClient->flush();
Expand Down