-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow opting into analytics #101
Changes from all commits
6b8e8b9
659f77f
0b75e3d
96f2872
6c9b39c
80db35c
ae67220
9a204a5
16e8841
2fd92f4
ce0791a
7a6a566
1848697
8e4d34f
459f339
c5eb1d3
4c6b41b
0b2f869
7c00b23
40cd3de
b45e3a3
87d1f45
9d8bd80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
lts/* | ||
14 | ||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I'm familiar with this syntax. What is happening on this line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I stole that from BMO. I didn't know about It's first casting Then, it casts it to a I think this prevents an Like: if ( '0' ) {
console.log( 'This is true' );
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course! I just found out about |
||
|
||
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 ); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as GAClient } from './GAClient'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is silly for one file, but it follows the pattern of exporting files in all of the other directories |
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 ) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't follow the same standard as the rest of this plugin, but it is possible to name the export instead of using
That will prevent the need for
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this plugin's setup is a bit outdated, with the Ideally, I'd modernize the whole But it might be strange to not have this |
||
let timeout; | ||
return function executedFunction( ...args ) { | ||
const later = () => { | ||
clearTimeout( timeout ); | ||
func( ...args ); | ||
}; | ||
|
||
clearTimeout( timeout ); | ||
timeout = setTimeout( later, wait ); | ||
}; | ||
}; | ||
|
||
export default debounce; |
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 ); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Node LTS used to be
14
, but now it's16
. It seems that Gutenberg still uses14
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interesting! I haven't heard of this
lts
before.