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

feat(analytics): automatically link GA4 with Site Kit #1698

Merged
merged 4 commits into from
Jun 10, 2022
Merged

Conversation

adekbadek
Copy link
Member

All Submissions:

Changes proposed in this Pull Request:

Adds automatic setup of GA4 property with Site Kit, if the property exists in GA. This is a PR to alpha, so it's included with the next release.

Initially submitted as #1681.

How to test the changes in this Pull Request:

Test on a site with Site Kit configured and Universal GA connected, but GA4 not connected.

  1. Confirm that the page is not sending hits to the GA4 property on master*
  2. Visit Site Kit's Analytics settings page (/wp-admin/admin.php?page=googlesitekit-settings#/connected-services/analytics) and observe that GA4 is connected
  3. Visit an AMP page and confirm the GA4 snippet is inserted (search for GA4_MEASUREMENT_ID in the page source) and GA4 property is hit**

* Just one POST request with pageview in payload to google-analytics.com should be sent
** So there should be two of these requests, one for the old Universal GA and one for GA4

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully ran tests with your changes locally?

@adekbadek adekbadek added the [Status] Needs Review The issue or pull request needs to be reviewed label Jun 8, 2022
@adekbadek adekbadek requested a review from a team as a code owner June 8, 2022 06:58
Copy link
Member

@miguelpeixe miguelpeixe left a comment

Choose a reason for hiding this comment

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

I was not able to test the setup of a new GA4. Setting up Site Kit with Analytics creates the GA4 automatically.

Do you recommend a way of testing the "automatic setup" outside of what Site Kit already provides?

* Class extending Site Kit's Module, in order to easily access GA data via
* Site Kit's Analytics Admin service.
*/
class GoogleSiteKitAnalytics extends Module {
Copy link
Member

Choose a reason for hiding this comment

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

If Site Kit is not installed and active, it throws a fatal error. There should be a proper check before importing this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 4768ceb

@adekbadek
Copy link
Member Author

I was not able to test the setup of a new GA4. Setting up Site Kit with Analytics creates the GA4 automatically.

You'd need a site set up before GA4 was available. You can remove the googlesitekit_analytics-4_settings option after setup to simulate that.

@adekbadek adekbadek requested a review from miguelpeixe June 10, 2022 07:40
@yogeshbeniwal
Copy link
Contributor

@adekbadek From end user perspective, does this PR simply connects with existing GA 4 property for scenario where site kit is not connected to GA 4?

@adekbadek
Copy link
Member Author

@yogeshbeniwal Yes, that's it

Copy link
Member

@miguelpeixe miguelpeixe left a comment

Choose a reason for hiding this comment

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

Working as expected. Left a non-blocking but important comment below.

*
* @param string $account_id Account ID.
*/
public function get_ga4_settings( $account_id ) {
Copy link
Member

@miguelpeixe miguelpeixe Jun 10, 2022

Choose a reason for hiding this comment

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

I worry this method, executed on admin_init, is very optimistic about a third-party structure. If $this->get_service() fails to return the expected object we'll have a fatal error on every dashboard page.

This could have method_exists(), property_exists() and isset() checks everywhere possible, just to be sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, done in 5885ce2

@github-actions github-actions bot added [Status] Approved The pull request has been reviewed and is ready to merge and removed [Status] Needs Review The issue or pull request needs to be reviewed labels Jun 10, 2022
@adekbadek adekbadek requested a review from miguelpeixe June 10, 2022 15:13
@adekbadek adekbadek merged commit 266135f into alpha Jun 10, 2022
@adekbadek adekbadek deleted the feat/ga4-setup branch June 10, 2022 15:20
matticbot pushed a commit that referenced this pull request Jun 10, 2022
# [1.84.0-alpha.5](v1.84.0-alpha.4...v1.84.0-alpha.5) (2022-06-10)

### Features

* **analytics:** automatically link GA4 with Site Kit ([#1698](#1698)) ([266135f](266135f))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 1.84.0-alpha.5 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

matticbot pushed a commit that referenced this pull request Jun 13, 2022
# [1.84.0](v1.83.3...v1.84.0) (2022-06-13)

### Bug Fixes

* **ads:** resolve conflicts from hotfix merge ([#1685](#1685)) ([8ce12cd](8ce12cd))
* **reader-revenue:** initial order state with total of 0 ([7c30b09](7c30b09))

### Features

* **ads:** handle gam default ad units ([#1654](#1654)) ([321b98e](321b98e))
* **analytics:** automatically link GA4 with Site Kit ([#1698](#1698)) ([266135f](266135f))
* reader registration on donate ([#1655](#1655)) ([5821b57](5821b57))
* remove theme selection from setup wizard ([#1656](#1656)) ([94e4580](94e4580))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 1.84.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released on @alpha released [Status] Approved The pull request has been reviewed and is ready to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants