Skip to content

Commit

Permalink
Feature/control display (#45)
Browse files Browse the repository at this point in the history
1. フックの順番がへんだったので変更
2. 関数を切り出し
  • Loading branch information
fumikito authored Jun 21, 2023
2 parents 9284007 + d6fcc55 commit 99a0f20
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
21 changes: 20 additions & 1 deletion ga-communicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}

// Initialize plugin.
add_action( 'plugin_loaded', function() {
add_action( 'plugins_loaded', function() {
// Add i18n
load_plugin_textdomain( 'ga-communicator', false, basename( __DIR__ ) . '/languages' );
// Load composer.
Expand Down Expand Up @@ -107,3 +107,22 @@ function ga_communicator_popular_posts( $query = [], $conditions = [] ) {
function ga_communicator_realtime_report( $request = [] ) {
return \Kunoichi\GaCommunicator::get_instance()->ga4_realtime_report( $request );
}

/**
* Render body open tag forcibly.
*
* @return void
*/
function ga_communicator_force_body_open() {
\Kunoichi\GaCommunicator\Utility\ScriptRenderer::get_instance()->body_open();
}

/**
* Render script tag forcibly.
*
* @param string $type Type of tag. Default is gtag. "universal" and "manual" is also available.
* @return void
*/
function ga_communicator_render( $type = 'gtag' ) {
echo \Kunoichi\GaCommunicator\Utility\ScriptRenderer::get_instance()->get_tag( $type );
}
26 changes: 18 additions & 8 deletions src/Kunoichi/GaCommunicator/Utility/ScriptRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ protected function init() {
}

/**
* Render analytics scripts.
* Get tag to render.
*
* @param string $type Tag type. gtag, universal, manual, or ""(empty).
* @return string
*/
public function render() {
$type = $this->setting->get_option( 'tag' );
if ( ! $type ) {
// No output.
return;
}
public function get_tag( $type = 'gtag' ) {
$additional = $this->setting->get_option( 'extra' );
$id = $this->setting->get_option( 'property' );
$ga4_id = $this->setting->get_option( 'ga4-tracking-id' );
Expand All @@ -50,7 +48,19 @@ public function render() {
}
$tag = $this->setting->placeholder->tag( $type, $id, $additional );
$replaced = $this->setting->placeholder->replace( $tag );
echo $replaced;
return apply_filters( 'ga_communicator_tag', $replaced, $type, $id );
}

/**
* Render analytics scripts.
*/
public function render() {
$type = $this->setting->get_option( 'tag' );
if ( ! $type ) {
// No output.
return;
}
echo $this->get_tag( $type );
}

/**
Expand Down

0 comments on commit 99a0f20

Please sign in to comment.