-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from studiopress/add/analytics-opt-in
Allow opting into analytics
- Loading branch information
Showing
15 changed files
with
433 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
lts/* | ||
14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { debounce } from '../helpers'; | ||
|
||
// @ts-ignore | ||
window.dataLayer = window.dataLayer || []; | ||
|
||
/** | ||
* Genesis Analytics Client | ||
* | ||
* Forked from BMO's work in Genesis Blocks. | ||
* | ||
* Follows the singleton pattern to prevent multiple instances of the GA Client from being used. | ||
* https://developers.google.com/analytics/devguides/collection/gtagjs | ||
*/ | ||
export default class GAClient { | ||
/** | ||
* Is Google Analytics enabled. | ||
* | ||
* @type {boolean} | ||
*/ | ||
enabled = false; | ||
|
||
/** | ||
* Google Analytics Client | ||
* | ||
* @type {Object} | ||
*/ | ||
client; | ||
|
||
/** | ||
* Google Analytics Measurment ID. | ||
* | ||
* Todo: update this for GCB. | ||
* | ||
* @type {string} | ||
*/ | ||
GA_ID = 'UA-12345'; | ||
|
||
/** | ||
* Class constructor. | ||
*/ | ||
constructor() { | ||
this.client = function() { | ||
// @ts-ignore | ||
window.dataLayer.push( arguments ); | ||
}; | ||
|
||
// @ts-ignore | ||
this.config = window.gcbAnalyticsConfig || {}; | ||
if ( this.config.ga_opt_in ) { | ||
this.enableAnalytics( this.config.ga_opt_in ); | ||
this.initClient(); | ||
} | ||
} | ||
|
||
/** | ||
* Enables Google Analytics. | ||
* Setting this value allows the GA Client to respect any opt out configuration. | ||
* | ||
* https://developers.google.com/analytics/devguides/collection/gtagjs/user-opt-out | ||
* | ||
* @param {boolean | number | string} enable The value to be set. | ||
*/ | ||
enableAnalytics( enable ) { | ||
enable = !! +enable; | ||
|
||
if ( enable ) { | ||
// Remove ga-disable-GA_MEASUREMENT_ID property to enable GA. | ||
delete window[ `ga-disable-${ this.GA_ID }` ]; | ||
} else { | ||
// Set ga-disable-GA_MEASUREMENT_ID property to disable GA. | ||
window[ `ga-disable-${ this.GA_ID }` ] = '1'; | ||
} | ||
this.enabled = enable; | ||
} | ||
|
||
/** | ||
* Sets up the initial values of the Google Analytics client. | ||
*/ | ||
initClient() { | ||
this.client( 'js', new Date() ); | ||
this.client( 'config', this.GA_ID, { send_page_view: false } ); | ||
} | ||
|
||
/** | ||
* Sends an event to Google Analytics. | ||
* | ||
* @param {string} action | ||
* @param {{event_category: string; event_label?: string;}} params | ||
*/ | ||
send( action, params ) { | ||
if ( this.enabled ) { | ||
this.client( 'event', action, params ); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a debounced copy of send method. | ||
*/ | ||
sendDebounce = debounce( this.send.bind( this ), 500 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as GAClient } from './GAClient'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Ensures that the provided function isn't called multiple times in succession. | ||
* | ||
* Forked from BMO's work in Genesis Blocks. | ||
* | ||
* @param {() => any} func | ||
* @param {number} wait | ||
* | ||
* @return {() => void} A debounced function. | ||
*/ | ||
const debounce = ( func, wait ) => { | ||
let timeout; | ||
return function executedFunction( ...args ) { | ||
const later = () => { | ||
clearTimeout( timeout ); | ||
func( ...args ); | ||
}; | ||
|
||
clearTimeout( timeout ); | ||
timeout = setTimeout( later, wait ); | ||
}; | ||
}; | ||
|
||
export default debounce; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
/** | ||
* Genesis Custom Blocks Settings. | ||
* | ||
* @package Genesis\CustomBlocks | ||
* @copyright Copyright(c) 2021, Genesis Custom Blocks | ||
* @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0) | ||
*/ | ||
|
||
namespace Genesis\CustomBlocks\Admin; | ||
|
||
use Genesis\CustomBlocks\ComponentAbstract; | ||
|
||
/** | ||
* Class Settings | ||
*/ | ||
class Settings extends ComponentAbstract { | ||
|
||
/** | ||
* Option name for the notices. | ||
* | ||
* @var string | ||
*/ | ||
const NOTICES_OPTION_NAME = 'genesis_custom_blocks_notices'; | ||
|
||
/** | ||
* Settings group to opt into analytics. | ||
* | ||
* @var string | ||
*/ | ||
const SETTINGS_GROUP = 'genesis-custom-blocks-settings-page'; | ||
|
||
/** | ||
* Option name to opt into analytics. | ||
* | ||
* @var string | ||
*/ | ||
const ANALYTICS_OPTION_NAME = 'genesis_custom_blocks_analytics_opt_in'; | ||
|
||
/** | ||
* The value when a user has opted into analytics. | ||
* | ||
* @var string | ||
*/ | ||
const ANALYTICS_OPTED_IN_VALUE = 'genesis_custom_blocks_analytics_opt_in'; | ||
|
||
/** | ||
* Page slug. | ||
* | ||
* @var string | ||
*/ | ||
const PAGE_SLUG = 'genesis-custom-blocks-settings'; | ||
|
||
/** | ||
* Register any hooks that this component needs. | ||
*/ | ||
public function register_hooks() { | ||
add_action( 'admin_menu', [ $this, 'add_submenu_pages' ] ); | ||
add_action( 'admin_init', [ $this, 'register_settings' ] ); | ||
} | ||
|
||
/** | ||
* Add submenu pages to the Genesis Custom Blocks menu. | ||
*/ | ||
public function add_submenu_pages() { | ||
add_submenu_page( | ||
'edit.php?post_type=' . genesis_custom_blocks()->get_post_type_slug(), | ||
__( 'Genesis Custom Blocks Settings', 'genesis-custom-blocks' ), | ||
__( 'Settings', 'genesis-custom-blocks' ), | ||
'manage_options', | ||
self::PAGE_SLUG, | ||
[ $this, 'render_page' ] | ||
); | ||
} | ||
|
||
/** | ||
* Renders the Settings page. | ||
*/ | ||
public function render_page() { | ||
include genesis_custom_blocks()->get_path() . 'php/Views/Settings.php'; | ||
} | ||
|
||
/** | ||
* Register Genesis Custom Blocks settings. | ||
*/ | ||
public function register_settings() { | ||
register_setting( self::SETTINGS_GROUP, self::ANALYTICS_OPTION_NAME ); | ||
} | ||
} |
Oops, something went wrong.