Indirect Widget Embedment

I am not sure that this is a problem of Matomo or my own ignorance about the use of PHP. I am posting it in order to clarify the matter.

BACKGROUND: I have no trouble communicating with the Matomo reporting API to extract desired bits of information about my visitors that I then share with my visitors on a page that I call the Looking Glass. This is achieved with an AJAX call using a JSON type response variable and a PHP cURL request file. The beauty of this mechanism is that I do not have to expose my authorization token to the general public, but can still display Matomo report contents without having to make all information potentially available to Matomo users via the autonomous user setting in the Matomo administrative panel.

This method of reporting runs into trouble, however, when I try to take advantage of Matomo’s well-constructed widgets. Please find below the strategy that fails and comment when and where possible.

FAILED STRATEGY:

Create an iframe in an HTML document whose src attribute points to a PHP file that makes a call to the Matomo reporting API and requests a widget.

The CODE:

<!DOCTYPE html>
<html lang="en">
<meta name="keywords" content="HTML, CSS, XML, XHTML, JavaScript">
<head>
    <meta charset="utf-8" />
    <title>Live Visitor Widget</title>
    <meta name="generator" content="BBEdit 10.5" />
</head>
<body>
<?php
    ini_set('log_errors', 1);
    ini_set('error_log', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'error.log');
    ini_set('html_errors', 0);
    ini_set('display_errors', 0);
    error_reporting(E_ALL);
    $url = 'https:// ... /index.php?module=Widgetize&action=iframe&widget=1&moduleToWidgetize=Live&actionToWidgetize=widget&idSite=1&period=day&date=yesterday&disableLink=1&token_auth=...';
    $curl_request = curl_init();
    curl_setopt($curl_request, CURLOPT_URL, $url);
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 0);
    if(curl_exec($curl_request) === false) {
        echo 'Curl error: ' . curl_error($curl_request);
    } else {
        echo 'Operation completed without any errors';
    }
    curl_close($curl_request);
?>
</body>
</html>

RESULT SET (See Images):

unformatted_widget_1Unformatted_widget_2

DESIRED RESULT (A Working Widget With Full Functionality)

What follows is a direct embed of the same URL via the src attribute of a custom iframe. This widget is totally functionable.

working_widget

Roddy

Hi,

Keep in mind that your script doesn’t forward the CSS/JS/Images.

Hi, Lukas!

Although it appears that the CSS, JS, and image links are missing, the entire complement is there including the HTML header tags. One has only to check the source code of the including iframe.

The browser, however, is doing work that the cURL is not. Either this, or the cURL is stripping away things that are needed, but no longer present. I do not know which, and I have little idea about how to overcome the problem.

Roddy