Skip to content

Commit

Permalink
Confirmed Matomo 4 compliance.
Browse files Browse the repository at this point in the history
  • Loading branch information
KZeni committed Nov 25, 2020
1 parent 786bb89 commit 1b7fbac
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 146 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.0.4

- Confirmed Matomo 4 compliance.

## 1.0.3

- Better scoped the widget styles.
Expand All @@ -15,4 +19,4 @@

## 1.0

- Initial Release.
- Initial Release.
220 changes: 112 additions & 108 deletions Widgets/GetCrazyEggInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,54 @@
* To configure a widget simply call the corresponding methods as described in the API-Reference:
* http://developer.piwik.org/api-reference/Piwik/Plugin\Widget
*/
class GetCrazyEggInfo extends Widget{
public static function configure(WidgetConfig $config){
/**
* Set the category the widget belongs to. You can reuse any existing widget category or define
* your own category.
*/
$config->setCategoryId('CrazyEggWidgetByAmperage_CrazyEgg');

/**
* Set the subcategory the widget belongs to. If a subcategory is set, the widget will be shown in the UI.
*/
// $config->setSubcategoryId('General_Overview');

/**
* Set the name of the widget belongs to.
*/
$config->setName('CrazyEggWidgetByAmperage_CrazyEggSnapshots');

/**
* Set the order of the widget. The lower the number, the earlier the widget will be listed within a category.
*/
$config->setOrder(50);

/**
* Optionally set URL parameters that will be used when this widget is requested.
* $config->setParameters(array('myparam' => 'myvalue'));
*/

/**
* Define whether a widget is enabled or not. For instance some widgets might not be available to every user or
* might depend on a setting (such as Ecommerce) of a site. In such a case you can perform any checks and then
* set `true` or `false`. If your widget is only available to users having super user access you can do the
* following:
*
* $config->setIsEnabled(\Piwik\Piwik::hasUserSuperUserAccess());
* or
* if (!\Piwik\Piwik::hasUserSuperUserAccess())
* $config->disable();
*/
}

public function amp_get_contents($url){
if(ini_get('allow_url_fopen')){
class GetCrazyEggInfo extends Widget
{
public static function configure(WidgetConfig $config)
{
/**
* Set the category the widget belongs to. You can reuse any existing widget category or define
* your own category.
*/
$config->setCategoryId('CrazyEggWidgetByAmperage_CrazyEgg');

/**
* Set the subcategory the widget belongs to. If a subcategory is set, the widget will be shown in the UI.
*/
// $config->setSubcategoryId('General_Overview');

/**
* Set the name of the widget belongs to.
*/
$config->setName('CrazyEggWidgetByAmperage_CrazyEggSnapshots');

/**
* Set the order of the widget. The lower the number, the earlier the widget will be listed within a category.
*/
$config->setOrder(50);

/**
* Optionally set URL parameters that will be used when this widget is requested.
* $config->setParameters(array('myparam' => 'myvalue'));
*/

/**
* Define whether a widget is enabled or not. For instance some widgets might not be available to every user or
* might depend on a setting (such as Ecommerce) of a site. In such a case you can perform any checks and then
* set `true` or `false`. If your widget is only available to users having super user access you can do the
* following:
*
* $config->setIsEnabled(\Piwik\Piwik::hasUserSuperUserAccess());
* or
* if (!\Piwik\Piwik::hasUserSuperUserAccess())
* $config->disable();
*/
}

public function amp_get_contents($url)
{
if (ini_get('allow_url_fopen')) {
return file_get_contents($url);
}else{
} else {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
Expand All @@ -75,24 +78,25 @@ public function amp_get_contents($url){
curl_close($curl);
return $output;
}
}

/**
* This method renders the widget. It's on you how to generate the content of the widget.
* As long as you return a string everything is fine. You can use for instance a "Piwik\View" to render a
* twig template. In such a case don't forget to create a twig template (eg. myViewTemplate.twig) in the
* "templates" directory of your plugin.
*
* @return string
*/
public function render(){
try {

}

/**
* This method renders the widget. It's on you how to generate the content of the widget.
* As long as you return a string everything is fine. You can use for instance a "Piwik\View" to render a
* twig template. In such a case don't forget to create a twig template (eg. myViewTemplate.twig) in the
* "templates" directory of your plugin.
*
* @return string
*/
public function render()
{
try {
$debug_help = false; // Toggle whether the Crazy Egg API helper text should be shown
$debug_siteurl = false; // Toggle the site URL being checked for at Crazy Egg
$debug_api = false; // Toggle whether the basic Crazy Egg API Status output should be shown
$debug_authentication = false; // Toggle whether the Crazy Egg API authentication testing output should be shown
$debug_snapshots = false; // Toggle whether the Crazy Egg API snapshots results should be shown
$debug_authentication = false; // Toggle whether the Crazy Egg API authentication testing output should be shown
$debug_snapshots = false; // Toggle whether the Crazy Egg API snapshots results should be shown
$debug_matching_snapshots = false; // Toggle whether the Crazy Egg API snapshot results for the matching site should be shown

$output = '<div class="widget-body">';

Expand All @@ -104,9 +108,10 @@ public function render(){
$api_key = $settings->crazyEggAPIKey->getValue();
$secret = $settings->crazyEggSecretKey->getValue();

if($api_key == '' || $secret == ''){
$output.= '<p>You first need to configure the CrazyEgg.com API Keys in your <a href="index.php?module=UsersManager&action=userSettings#CrazyEggWidgetByAmperage">user settings</a>.</p>';
}else{ // API Keys have been provided
if ($api_key == '' || $secret == '') {
$output .= '<p>You first need to configure the CrazyEgg.com API Keys in your <a href="index.php?module=UsersManager&action=userSettings#CrazyEggWidgetByAmperage">user settings</a>.</p>';
} else {
// API Keys have been provided

// Get the site's URL from Piwik
$idSite = Common::getRequestVar('idSite');
Expand All @@ -118,76 +123,75 @@ public function render(){
if (empty($siteurlurl) || !UrlHelper::isLookLikeUrl($siteurl)) {
$siteurl = $site->getMainUrl();
}
$siteurl = str_replace('https://','',str_replace('http://','',$siteurl)); // Strip both http:// and https:// from the URL being checked
$siteurl = str_replace('https://', '', str_replace('http://', '', $siteurl)); // Strip both http:// and https:// from the URL being checked

if($debug_help){
$output.= '<p>This is where the Crazy Egg API data should be shown (snapshot list with details, etc. as documented at <a href="https://app.crazyegg.com/v2/options/api" target="_blank">https://app.crazyegg.com/v2/options/api</a>)</p>';
if ($debug_help) {
$output .= '<p>This is where the Crazy Egg API data should be shown (snapshot list with details, etc. as documented at <a href="https://app.crazyegg.com/v2/options/api" target="_blank">https://app.crazyegg.com/v2/options/api</a>)</p>';
}

if($debug_siteurl){
$output.= '<p><strong>Site URL:</strong> <code>'.$siteurl.'</code></p>';
if ($debug_siteurl) {
$output .= '<p><strong>Site URL:</strong> <code>' . $siteurl . '</code></p>';
}

if($debug_api){
if ($debug_api) {
$api_status = $this->amp_get_contents('https://app.crazyegg.com/api/v2/status.json'); // Get the status of the Crazy Egg API (no authentication/signing needed)
$output.= '<p><strong>Crazy Egg API Status:</strong> <code>'.$api_status.'</code></p>';
$output .= '<p><strong>Crazy Egg API Status:</strong> <code>' . $api_status . '</code></p>';
}

if($debug_authentication){
$authentication_request = "test=value&api_key=".$api_key;
$content = str_replace('=','',$authentication_request); // Concatenate the name and value into a string (required by Crazy Egg API v2)
$content = explode('&',$content); // Make it so each request parameter is an item in the array so we can sort them alphabetically
if ($debug_authentication) {
$authentication_request = "test=value&api_key=" . $api_key;
$content = str_replace('=', '', $authentication_request); // Concatenate the name and value into a string (required by Crazy Egg API v2)
$content = explode('&', $content); // Make it so each request parameter is an item in the array so we can sort them alphabetically
sort($content); // Sort the array alphabetically (required by Crazy Eg API v2)
$content = implode('',$content); // Turn array into string
$content = implode('', $content); // Turn array into string
$authentication_request_signed = hash_hmac("sha256", $content, $secret); // The result for the required Request Signing
$authentication = $this->amp_get_contents('https://app.crazyegg.com/api/v2/authenticate.json?'.$authentication_request.'&signed='.$authentication_request_signed); // Test authentication with keys & singing
$output.= '<p><strong>Crazy Egg API Authentication:</strong> <code>'.$authentication.'</code></p>';
$authentication = $this->amp_get_contents('https://app.crazyegg.com/api/v2/authenticate.json?' . $authentication_request . '&signed=' . $authentication_request_signed); // Test authentication with keys & singing
$output .= '<p><strong>Crazy Egg API Authentication:</strong> <code>' . $authentication . '</code></p>';
}

$snapshots_request = "api_key=".$api_key;
$content = str_replace('=','',$snapshots_request); // Concatenate the name and value into a string (required by Crazy Egg API v2)
$content = explode('&',$content); // Make it so each request parameter is an item in the array so we can sort them alphabetically
$snapshots_request = "api_key=" . $api_key;
$content = str_replace('=', '', $snapshots_request); // Concatenate the name and value into a string (required by Crazy Egg API v2)
$content = explode('&', $content); // Make it so each request parameter is an item in the array so we can sort them alphabetically
sort($content); // Sort the array alphabetically (required by Crazy Eg API v2)
$content = implode('',$content); // Turn array into string
$content = implode('', $content); // Turn array into string
$snapshots_request_signed = hash_hmac("sha256", $content, $secret); // The result for the required Request Signing
$snapshots = $this->amp_get_contents('https://app.crazyegg.com/api/v2/snapshots.json?'.$snapshots_request.'&signed='.$snapshots_request_signed); // Get the list of snapshots in this account
$snapshots = $this->amp_get_contents('https://app.crazyegg.com/api/v2/snapshots.json?' . $snapshots_request . '&signed=' . $snapshots_request_signed); // Get the list of snapshots in this account

if($debug_snapshots){
$output.= '<p><strong>Crazy Egg Snapshots:</strong> <code>'.$snapshots.'</code></p>';
if ($debug_snapshots) {
$output .= '<p><strong>Crazy Egg Snapshots:</strong> <code>' . $snapshots . '</code></p>';
}

$snapshots = json_decode($snapshots);
$snapshotCount = 0;
foreach($snapshots as $snapshot){
if (strpos($snapshot->source_url, $siteurl) !== false) { // Only snapshot info if it's for the current site's URL
$output.= '<a href="https://app.crazyegg.com/v2/snapshots/'.$snapshot->id.'" target="_blank" class="crazyegg-snapshot alert alert-info"><h4 class="name">'.$snapshot->name.'</h4><img src="'.$snapshot->thumbnail_url.'" class="thumbnail" alt="Thumbnail" width="154" height="102" /><span class="total_visits">'.number_format($snapshot->total_visits).'</span><span class="total_clicks">'.number_format($snapshot->total_clicks).'</span><span class="status">'.ucfirst($snapshot->status).'</span><span class="heatmap-preview"><img src="'.$snapshot->heatmap_url.'" class="heatmap" alt="Heatmap" width="100%" /><img src="'.$snapshot->screenshot_url.'" class="screenshot" alt="Screenshot" width="100%" /></span></a><!-- .crazyegg-snapshot -->';
foreach ($snapshots as $snapshot) {
if (strpos($snapshot->source_url, $siteurl) !== false) {
// Only snapshot info if it's for the current site's URL
if ($debug_matching_snapshots) {
$output .= '<p><strong>Crazy Egg Snapshot:</strong> <code>' . print_r($snapshot, true) . '</code></p>';
}
$output .= '<a href="https://app.crazyegg.com/v2/snapshots/' . $snapshot->id . '" target="_blank" class="crazyegg-snapshot alert alert-info"><h4 class="name">' . $snapshot->name . '</h4><img src="' . $snapshot->thumbnail_url . '" class="thumbnail" alt="Thumbnail" width="154" height="102" /><span class="total_visits">' . number_format($snapshot->total_visits) . '</span><span class="total_clicks">' . number_format($snapshot->total_clicks) . '</span><span class="status">' . ucfirst($snapshot->status) . '</span><span class="heatmap-preview"><img src="' . $snapshot->heatmap_url . '" class="heatmap" alt="Heatmap" width="100%" /><img src="' . $snapshot->screenshot_url . '" class="screenshot" alt="Screenshot" width="100%" /></span></a><!-- .crazyegg-snapshot -->';
$snapshotCount++;
}
}

if($snapshotCount<1){
$output.= '<p>No <a href="https://www.crazyegg.com" target="_blank">Crazy Egg</a> snapshots have been added for this website\'s URL ('.$siteurl.') under the account the Crazy Egg API keys are for yet.</p>';
if ($snapshotCount < 1) {
$output .= '<p>No <a href="https://www.crazyegg.com" target="_blank">Crazy Egg</a> snapshots have been added for this website\'s URL (' . $siteurl . ') under the account the Crazy Egg API keys are for yet.</p>';
}

}

$output.= '</div>';
$output .= '</div>';
return $output;

} catch (\Exception $e) {
return $this->error($e);
}
}

/**
* @param \Exception $e
* @return string
*/
private function error($e)
{
return '<div class="pk-emptyDataTable">'
. Piwik::translate('General_ErrorRequest', array('', ''))
. ' - ' . $e->getMessage() . '</div>';
}

}
} catch (\Exception $e) {
return $this->error($e);
}
}

/**
* @param \Exception $e
* @return string
*/
private function error($e)
{
return '<div class="pk-emptyDataTable">' . Piwik::translate('General_ErrorRequest', ['', '']) . ' - ' . $e->getMessage() . '</div>';
}
}
66 changes: 29 additions & 37 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
{
"name": "CrazyEggWidgetByAmperage",
"version": "1.0.3",
"description": "Show Crazy Egg snapshots for the current site as a dashboard widget.",
"theme": false,
"keywords": [
"amperage",
"crazy egg",
"crazyegg",
"widget",
"heatmap",
"heat map",
"dashboard"
],
"license": "GPL v3+",
"homepage": "https://www.amperagemarketing.com",
"require": {
"piwik": ">=3.2.1-stable,<4.0.0-b1"
},
"support": {
"email": "[email protected]",
"issues": "https://github.com/AMPERAGE-Marketing/Matomo-Crazy-Egg-Widget/issues",
"wiki": "https://github.com/AMPERAGE-Marketing/Matomo-Crazy-Egg-Widget/wiki",
"source": "https://github.com/AMPERAGE-Marketing/Matomo-Crazy-Egg-Widget"
},
"authors": [
{
"name": "Amperage Marketing & Fundraising",
"email": "[email protected]",
"homepage": "http://www.amperagemarketing.com"
},
{
"name": "Kurt Zenisek",
"email": "[email protected]",
"homepage": "http://kzeni.com"
}
]
}
"name": "CrazyEggWidgetByAmperage",
"version": "1.0.4",
"description": "Show Crazy Egg snapshots for the current site as a dashboard widget.",
"theme": false,
"keywords": ["amperage", "crazy egg", "crazyegg", "widget", "heatmap", "heat map", "dashboard"],
"license": "GPL v3+",
"homepage": "https://www.amperagemarketing.com",
"require": {
"piwik": ">=3.2.1-stable,<5.0.0-b1"
},
"support": {
"email": "[email protected]",
"issues": "https://github.com/AMPERAGE-Marketing/Matomo-Crazy-Egg-Widget/issues",
"wiki": "https://github.com/AMPERAGE-Marketing/Matomo-Crazy-Egg-Widget/wiki",
"source": "https://github.com/AMPERAGE-Marketing/Matomo-Crazy-Egg-Widget"
},
"authors": [
{
"name": "Amperage Marketing & Fundraising",
"email": "[email protected]",
"homepage": "http://www.amperagemarketing.com"
},
{
"name": "Kurt Zenisek",
"email": "[email protected]",
"homepage": "http://kzeni.com"
}
]
}

0 comments on commit 1b7fbac

Please sign in to comment.