'method_name',
self::$position => ['function_to_add' => 'render_analytics', 'priority' => 999999],
];
}
/**
* Plugin options settings.
*/
private ?array $options = null;
private static $position = '';
/**
* Init the constructor.
*
* @param array $argument Plugin options settings.
*/
function __construct(array $options = [])
{
$this->options = $options;
self::$position = $this->options['google_analytics_position'];
// add_filter( 'body_class', array( $this, 'render_tag_manager' ), 10000, 2 );
}
// add_filter( 'body_class', 'render_tag_manager', 10000 );
/**
* Google tag manager
* @link http://www.tagmanageritalia.it/come-installare-google-tag-manager-tramite-wordpress/
* Filters the list of CSS body classes for the current post or page.
*
* @since 2.2.2
*
* @param array $classes An array of body classes.
* @param array $class An array of additional classes added to the body.
*/
public function render_tag_manager($classes, $class)
{
if (is_preview() && is_admin()) {
return;
}
if (empty($this->options['google_tag_manager_id'])) {
return;
}
$snippet = sprintf(
'',
esc_js($this->options['google_tag_manager_id'])
);
$classes[] = sprintf(
'">%s
$parameter) {
if (empty($parameter)) {
continue;
}
if (is_array($parameter)) {
$output .= json_encode($parameter, JSON_THROW_ON_ERROR);
} else {
$output .= sprintf(
'"%s"%s',
esc_js($parameter),
$count_params - 1 === $i ? '' : ','
);
}
$i++;
}
$output .= ');';
/**
* This filters the output of the html attributes.
*
* @var string
*/
// $output = apply_filters( "italystrap_attr_{$context}_output", $output, $parameters, $context, $args );
return $output;
}
/**
* Maybe anonimize IP
*
* @return string [description]
*/
public function maybe_anonimize_ip()
{
if (empty($this->options['google_analytics_anonymizeIp'])) {
return '';
}
$parameters = ['command' => 'set', 'fields' => 'anonymizeIp', 'fields_object' => 'true'];
return $this->get_ga($parameters);
}
/**
* Init ga
*
* @return string [description]
*/
public function ga_commands_queue()
{
$parameters = [['command' => 'create', 'fields' => esc_js($this->options['google_analytics_id']), 'fields_object' => 'auto'], ['command' => 'send', 'fields' => 'pageview']];
$parameters = apply_filters('italystrap_ga_commands_queue', $parameters, $this->options);
$output = '';
foreach ($parameters as $array) {
$output .= $this->get_ga($array);
}
$output .= $this->maybe_anonimize_ip();
return apply_filters('italystrap_ga_commands_queue_output', $output, $this);
}
/**
* Standard analytics script
*
* @link https://developers.google.com/analytics/devguides/collection/analyticsjs/
*
* @param string $value [description]
* @return string [description]
*/
public function standard_script($get_ga)
{
return sprintf(
'',
$get_ga
);
}
/**
* The alternative async tracking snippet below adds support for preloading,
* which will provide a small performance boost on modern browsers,
* but can degrade to synchronous loading and execution on IE 9 and
* older mobile browsers that do not recognize the async script attribute.
* Only use this tracking snippet if your visitors primarily use
* modern browsers to access your site.
*
* @link https://developers.google.com/analytics/devguides/collection/analyticsjs/
*
* @param string $value [description]
* @return string [description]
*/
public function alternative_script($get_ga)
{
return sprintf(
'',
$get_ga
);
}
/**
* Insert your ID in Option Theme admin panel
* Print code only if value exist
*
* @todo google event tracking time
* @link https://programma-affiliazione.amazon.it/gp/associates/promo/a20140921?rw_useCurrentProtocol=1
*
* @link https://developers.google.com/analytics/devguides/collection/analyticsjs/
*
* @return string Return google analytics code
*/
public function render_analytics()
{
if (is_customize_preview() || is_preview() || is_admin()) {
return;
}
if (empty($this->options['google_analytics_id'])) {
return;
}
// $output = $this->alternative_script( $this->ga_commands_queue() );
$output = $this->standard_script($this->ga_commands_queue());
echo $output; // XSS ok.
}
}