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

Settings: Add new dropdown for choosing analytics behavior #13567

Merged
merged 33 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
169b5ae
✨ Register Tracking Handler Setting
AnuragVasanwala Jan 17, 2024
ae0b9f9
♻️ Refactor SiteKit `actions` and `filters` on `register` for flexibl…
AnuragVasanwala Jan 17, 2024
e45af01
✨ Register Tracking Handler API for `wp-dashboard`
AnuragVasanwala Jan 17, 2024
7eb0e5a
🖍 Register new setting UI with dropdown for flexible Tracking Handler…
AnuragVasanwala Jan 17, 2024
1272da3
🏗 Add missing doc comment for parameter `$settings`
AnuragVasanwala Jan 17, 2024
9b6eae3
♻️ Refactor messages for translation markup
AnuragVasanwala Jan 31, 2024
cba190c
🧪 Update `editorSettings` unit-test to support for analytics dropdown
AnuragVasanwala Jan 31, 2024
7049147
🧪 Update `Site_Kit` unit-test to support for newly added `Setting` pa…
AnuragVasanwala Jan 31, 2024
0d8d898
🧪 Add unit-test to `Site_Kit` to support for switching between SiteKi…
AnuragVasanwala Jan 31, 2024
7ada51c
Merge branch 'main' into feat/13506-flexible-tracking-support
AnuragVasanwala Jan 31, 2024
3234d7b
Merge branch 'main' into feat/13506-flexible-tracking-support
AnuragVasanwala Feb 2, 2024
860c1b0
⏪ Revert added PHPUnit tests in `Site_Kit`
AnuragVasanwala Feb 2, 2024
82a7b4a
🧪 Add E2E unit tests for `Site_Kit`
AnuragVasanwala Feb 2, 2024
ccb8df3
Merge branch 'feat/13506-flexible-tracking-support' of https://github…
AnuragVasanwala Feb 2, 2024
9481a77
♻️ Display `Site_Kit` note only when it is active
AnuragVasanwala Feb 2, 2024
e561801
Merge branch 'main' into feat/13506-flexible-tracking-support
AnuragVasanwala Feb 9, 2024
a198f99
Remove e2e tests that check if SiteKit message is displayed
Swanand01 Feb 9, 2024
383dee8
Add component tests for `<GoogleAnalyticsSettings />`:
Swanand01 Feb 9, 2024
5fbcae4
Export `ANALYTICS_DROPDOWN_OPTIONS` to be used in tests
Swanand01 Feb 9, 2024
00d77d1
Remove unused constants
Swanand01 Feb 9, 2024
0682753
Fix eslint issues
Swanand01 Feb 9, 2024
38ce65c
Fix phpcs issues
Swanand01 Feb 9, 2024
c8fe3bd
🐛 Fix: Restrict tracking-id input only when `analyticsActive` is acti…
AnuragVasanwala Feb 12, 2024
9e597ee
Fix eslint issues in `googleAnalytics/index.js`
Swanand01 Feb 13, 2024
6169d7e
Add tests to check if the Google Anaytics ID input is enabled/disable…
Swanand01 Feb 13, 2024
b86ae3c
Update Site Kit note text and display it regardless of option chosen …
Swanand01 Feb 13, 2024
cdfaf7d
Update tests for the Site Kit note
Swanand01 Feb 13, 2024
3062d10
Remove restriction where measurement ID field is disabled when the "…
Swanand01 Feb 15, 2024
da44dba
Update tests for measurement ID input
Swanand01 Feb 15, 2024
8021238
Merge branch 'main' into feat/13506-flexible-tracking-support
Swanand01 Feb 15, 2024
1133448
Remove disable restriction for measurement ID input
Swanand01 Feb 15, 2024
8851a7b
Remove redundant test that checked if the measurement ID input is ena…
Swanand01 Feb 15, 2024
8b792a2
Merge branch 'feat/13506-flexible-tracking-support' of https://github…
Swanand01 Feb 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions includes/Integrations/Site_Kit.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Google\Web_Stories\Analytics;
use Google\Web_Stories\Context;
use Google\Web_Stories\Service_Base;
use Google\Web_Stories\Settings;

/**
* Class Site_Kit.
Expand Down Expand Up @@ -64,17 +65,26 @@ class Site_Kit extends Service_Base {
*/
private Plugin_Status $plugin_status;

/**
* Settings instance.
*
* @var Settings Settings instance.
*/
private Settings $settings;

/**
* Constructor.
*
* @param Analytics $analytics Analytics instance.
* @param Context $context Context instance.
* @param Plugin_Status $plugin_status Plugin_Status instance.
* @param Settings $settings Settings instance.
*/
public function __construct( Analytics $analytics, Context $context, Plugin_Status $plugin_status ) {
public function __construct( Analytics $analytics, Context $context, Plugin_Status $plugin_status, Settings $settings ) {
$this->analytics = $analytics;
$this->context = $context;
$this->plugin_status = $plugin_status;
$this->settings = $settings;
}

/**
Expand All @@ -85,7 +95,19 @@ public function __construct( Analytics $analytics, Context $context, Plugin_Stat
public function register(): void {
add_filter( 'googlesitekit_amp_gtag_opt', [ $this, 'filter_site_kit_gtag_opt' ] );

if ( $this->is_analytics_module_active() ) {
$handler = $this->settings->get_setting( $this->settings::SETTING_NAME_TRACKING_HANDLER );

if ( 'web-stories' === $handler ) {
add_filter(
'googlesitekit_analytics-4_tag_amp_blocked',
function ( $blocked ) {
if ( $this->context->is_web_story() ) {
return true;
}
return $blocked;
}
);
} elseif ( 'site-kit' === $handler && $this->is_analytics_module_active() ) {
remove_action( 'web_stories_print_analytics', [ $this->analytics, 'print_analytics_tag' ] );
}
}
Expand Down
22 changes: 22 additions & 0 deletions includes/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ class Settings implements Service, Registerable, PluginUninstallAware {
*/
public const SETTING_NAME_DEFAULT_PAGE_DURATION = 'web_stories_default_page_duration';

/**
* GA Tracking Handler.
*/
public const SETTING_NAME_TRACKING_HANDLER = 'web_stories_ga_tracking_handler';

/**
* Shopping_Vendors instance.
*
Expand Down Expand Up @@ -403,6 +408,22 @@ public function register(): void {
'show_in_rest' => true,
]
);

register_setting(
self::SETTING_GROUP,
self::SETTING_NAME_TRACKING_HANDLER,
[
'description' => __( 'Tracking Handler', 'web-stories' ),
'type' => 'string',
'default' => 'site-kit',
'show_in_rest' => [
'schema' => [
'type' => 'string',
'enum' => [ 'site-kit', 'web-stories', 'both' ],
],
],
]
);
}

/**
Expand Down Expand Up @@ -495,6 +516,7 @@ public function on_plugin_uninstall(): void {
delete_option( self::SETTING_NAME_SHOPIFY_ACCESS_TOKEN );
delete_option( self::SETTING_NAME_DEFAULT_PAGE_DURATION );
delete_option( self::SETTING_NAME_AUTO_ADVANCE );
delete_option( self::SETTING_NAME_TRACKING_HANDLER );
}

/**
Expand Down
12 changes: 1 addition & 11 deletions packages/e2e-tests/src/specs/integrations/sitekit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ describe('Site Kit plugin integration', () => {
describe('Google Analytics', () => {
withPlugin('e2e-tests-site-kit-analytics-mock');

describe('Dashboard', () => {
it('should see Site Kit specific message', async () => {
await visitSettings();

await expect(page).toMatchTextContent(
'Site Kit by Google has already enabled Google Analytics for your Web Stories'
);
});
});

describe('Editor', () => {
afterAll(async () => {
await trashAllPosts('web-story');
Expand All @@ -69,7 +59,7 @@ describe('Site Kit plugin integration', () => {
withPlugin('e2e-tests-site-kit-adsense-mock');

describe('Dashboard', () => {
it('should see Site Kit specific message', async () => {
it('should see Google AdSense specific message', async () => {
await visitSettings();

await expect(page).toMatchTextContent(
Expand Down
3 changes: 3 additions & 0 deletions packages/wp-dashboard/src/api/reducers/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
AD_NETWORK_TYPE,
ARCHIVE_TYPE,
SHOPPING_PROVIDER_TYPE,
GOOGLE_ANALYTICS_HANDLER_TYPE,
} from '../../constants';

export const ACTION_TYPES = {
Expand Down Expand Up @@ -49,6 +50,7 @@ export const defaultSettingsState = {
shopifyAccessToken: '',
autoAdvance: true,
defaultPageDuration: 7,
googleAnalyticsHandler: GOOGLE_ANALYTICS_HANDLER_TYPE.SITE_KIT,
};

function settingsReducer(state, action) {
Expand Down Expand Up @@ -89,6 +91,7 @@ function settingsReducer(state, action) {
shopifyAccessToken: action.payload.shopifyAccessToken,
autoAdvance: action.payload.autoAdvance,
defaultPageDuration: action.payload.defaultPageDuration,
googleAnalyticsHandler: action.payload.googleAnalyticsHandler,
};
}

Expand Down
6 changes: 6 additions & 0 deletions packages/wp-dashboard/src/api/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const transformSettingResponse = (response) => ({
shopifyAccessToken: response.web_stories_shopify_access_token,
autoAdvance: Boolean(response.web_stories_auto_advance),
defaultPageDuration: response.web_stories_default_page_duration,
googleAnalyticsHandler: response.web_stories_ga_tracking_handler,
});

/**
Expand Down Expand Up @@ -86,6 +87,7 @@ export function updateSettings(apiPath, queryParams) {
shopifyAccessToken,
autoAdvance,
defaultPageDuration,
googleAnalyticsHandler,
} = queryParams;

const query = {};
Expand Down Expand Up @@ -154,6 +156,10 @@ export function updateSettings(apiPath, queryParams) {
query.web_stories_default_page_duration = defaultPageDuration;
}

if (googleAnalyticsHandler !== undefined) {
query.web_stories_ga_tracking_handler = googleAnalyticsHandler;
}

const path = addQueryArgs(apiPath, query);

return apiFetch({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function EditorSettings() {
shopifyAccessToken,
autoAdvance,
defaultPageDuration,
googleAnalyticsHandler,
} = useEditorSettings(
({
actions: {
Expand Down Expand Up @@ -117,6 +118,7 @@ function EditorSettings() {
shopifyAccessToken,
autoAdvance,
defaultPageDuration,
googleAnalyticsHandler,
},
media: { isLoading: isMediaLoading, newlyCreatedMediaIds },
publisherLogos: { publisherLogos },
Expand Down Expand Up @@ -155,6 +157,7 @@ function EditorSettings() {
shopifyAccessToken,
autoAdvance,
defaultPageDuration,
googleAnalyticsHandler,
})
);

Expand Down Expand Up @@ -329,6 +332,12 @@ function EditorSettings() {
[setPublisherLogoAsDefault]
);

const handleUpdateGoogleAnalyticsHandler = useCallback(
(newGoogleAnalyticsHandler) =>
updateSettings({ googleAnalyticsHandler: newGoogleAnalyticsHandler }),
[updateSettings]
);

return (
<Layout.Provider>
<Wrapper data-testid="editor-settings">
Expand All @@ -346,6 +355,10 @@ function EditorSettings() {
usingLegacyAnalytics={usingLegacyAnalytics}
handleMigrateLegacyAnalytics={handleMigrateLegacyAnalytics}
siteKitStatus={siteKit}
googleAnalyticsHandler={googleAnalyticsHandler}
handleUpdateGoogleAnalyticsHandler={
handleUpdateGoogleAnalyticsHandler
}
/>
<PublisherLogoSettings
onAddLogos={handleAddLogos}
Expand Down
Loading
Loading