Skip to content
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

Merged
merged 23 commits into from
Jan 7, 2022
Merged

Allow opting into analytics #101

merged 23 commits into from
Jan 7, 2022

Conversation

kienstra
Copy link
Contributor

@kienstra kienstra commented Dec 9, 2021

Changes

  • Copies BMO's analytics work from Genesis Blocks into GCB
  • Port the Settings page PHP from Pro to free
  • Not in scope: using analytics, like sending an event

Fixes GF-3267

Testing instructions

  1. composer i && npm i && npm run dev
  2. /wp-admin > Custom Blocks > Settings
  3. Check 'Enable analytics':
    Screen Shot 2021-12-16 at 10 26 01 AM
  4. Click 'Save Changes'
  5. Create a new post
  6. In the DevTools, enter window.GcbAnalytics
  7. Expected: It's defined:
    Screen Shot 2021-12-16 at 10 26 50 AM

@kienstra kienstra changed the title Allow opting in to analytics Allow opting into analytics Dec 9, 2021
if ( Settings::ANALYTICS_OPTED_IN_VALUE === get_option( Settings::ANALYTICS_OPTION_NAME ) ) {
wp_enqueue_script(
self::ANALYTICS_SCRIPT_SLUG,
'https://www.googletagmanager.com/gtag/js?id=UA-17364082-14',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The UA id should probably be unique to GCB

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be able to get that from the GA dashboard

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK nice, could you Slack me a link to that?

@@ -1 +1 @@
lts/*
14
Copy link
Contributor Author

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's 16. It seems that Gutenberg still uses 14

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.

@@ -0,0 +1 @@
export { default as GAClient } from './GAClient';
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

@kienstra kienstra marked this pull request as ready for review December 16, 2021 16:20
};

// @ts-ignore
this.config = window.genesisAnalyticsConfig || {};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this value need to be window.gcbAnalyticsConfig ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uf, you're right.

Copy link
Contributor Author

@kienstra kienstra Dec 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b45e3a3

registerBlocks( genesisCustomBlocks, gcbBlocks, Edit );

domReady( () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

domReady is interesting. Where you coming across any issues that needed this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, I didn't notice that it was needed.

Moving it out of domReady() now…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved out in 87d1f45


domReady( () => {
// @ts-ignore
window.dataLayer = window.dataLayer || [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was you logic for moving window.dataLayer our of GAClient ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a functional reason, I just like class files to have only the class, and no logic other than that.

Maybe from too much time in PHP 🤣

But if you'd prefer, I could move it back.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I'm going to move it back.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it back in 9d8bd80

*
* @return {() => void} A debounced function.
*/
const debounce = ( func, wait ) => {

Choose a reason for hiding this comment

The 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 default.

export const debounce = () => { ... }

That will prevent the need for export default debounce at the bottom, and it can be included with

include { debounce }  from './debounce';

Copy link
Contributor Author

@kienstra kienstra Dec 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this plugin's setup is a bit outdated, with the index.js file having all of the exports.

Ideally, I'd modernize the whole js/src directory.

But it might be strange to not have this debounce in index.js, while all of the other helper functions are in it.

@@ -158,6 +166,22 @@ public function editor_assets() {
]
);

if ( Settings::ANALYTICS_OPTED_IN_VALUE === get_option( Settings::ANALYTICS_OPTION_NAME ) ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to factor in any historical opt-in events like we just merged into GB?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, good question.

That might be something for product, but I think for this scope, this might be enough.

There is no genesisAnalyticsConfig
in this plugin.
There is no genesisAnalyticsConfig
in this plugin.
It's an essential part of the class,
so it's strange to not have it in that file.
* @param {boolean | number | string} enable The value to be set.
*/
enableAnalytics( enable ) {
enable = !! +enable;

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

@kienstra kienstra Dec 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I stole that from BMO.

I didn't know about + either.

It's first casting enable to a Number with +enable. For example, converting '0' to 0.

Then, it casts it to a Boolean with !!.

I think this prevents an enable value of '0' from being true.

Like:

if ( '0' ) {
    console.log( 'This is true' );
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course! I just found out about +

@johnstonphilip
Copy link

Looks all good here!
Screen Shot 2021-12-20 at 1 12 55 PM
Screen Shot 2021-12-20 at 1 13 02 PM
Screen Shot 2021-12-20 at 1 14 19 PM

@johnstonphilip johnstonphilip self-requested a review December 20, 2021 18:16
@kienstra
Copy link
Contributor Author

Hi @johnstonphilip,
Thanks a lot for reviewing and testing this!

Copy link

@BMO-tech BMO-tech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@kienstra
Copy link
Contributor Author

@BMO-tech, thanks so much for your testing and review.

@kienstra kienstra merged commit eb77207 into develop Jan 7, 2022
@kienstra kienstra deleted the add/analytics-opt-in branch January 7, 2022 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants