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

Tracking: Prevent incorrect tracking IDs on frontend #13046

Merged
merged 2 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions includes/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public function get_tracking_id(): string {
* @var string $tracking_id
*/
$tracking_id = $this->settings->get_setting( $this->settings::SETTING_NAME_TRACKING_ID );

// For some reasons, some sites use the plugin's own tracking ID (as used in the admin)
// for their stories. Prevent accidental erroneous tracking in such a case.
if ( Tracking::TRACKING_ID === $tracking_id ) {
return '';
}

return $tracking_id;
}

Expand Down
1 change: 1 addition & 0 deletions includes/Database_Upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Database_Upgrader implements Service, Registerable, PluginActivationAware,
'3.0.13' => Migrations\Add_Stories_Caps::class,
'3.0.14' => Migrations\Add_Media_Source_Page_Template::class,
'3.0.15' => Migrations\Add_Media_Source_Recording::class,
'3.0.16' => Migrations\Remove_Incorrect_Tracking_Id::class,
];

/**
Expand Down
72 changes: 72 additions & 0 deletions includes/Migrations/Remove_Incorrect_Tracking_Id.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Class Remove_Incorrect_Tracking_Id
*
* @link https://github.com/googleforcreators/web-stories-wp
*
* @copyright 2023 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
*/

/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types = 1);

namespace Google\Web_Stories\Migrations;

use Google\Web_Stories\Settings;
use Google\Web_Stories\Tracking;

/**
* Class Remove_Incorrect_Tracking_Id
*/
class Remove_Incorrect_Tracking_Id extends Migrate_Base {
/**
* Settings instance.
*
* @var Settings Settings instance.
*/
private Settings $settings;

/**
* Constructor.
*
* @since 1.30.0
*
* @param Settings $settings Settings instance.
* @return void
*/
public function __construct( Settings $settings ) {
$this->settings = $settings;
}

/**
* Remove incorrect tracking ID.
*
* @since 1.30.0
*/
public function migrate(): void {
$tracking_id = $this->settings->get_setting( $this->settings::SETTING_NAME_TRACKING_ID );

if ( Tracking::TRACKING_ID === $tracking_id ) {
$this->settings->update_setting(
$this->settings::SETTING_NAME_TRACKING_ID,
''
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types = 1);

/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Web_Stories\Tests\Integration\Migrations;

use Google\Web_Stories\Settings;
use Google\Web_Stories\Tests\Integration\DependencyInjectedTestCase;
use Google\Web_Stories\Tracking;

/**
* Class Remove_Incorrect_Tracking_Id
*
* @coversDefaultClass \Google\Web_Stories\Migrations\Remove_Incorrect_Tracking_Id
*/
class Remove_Incorrect_Tracking_Id extends DependencyInjectedTestCase {
private \Google\Web_Stories\Migrations\Remove_Incorrect_Tracking_Id $instance;

public function set_up(): void {
parent::set_up();

$this->instance = $this->injector->make( \Google\Web_Stories\Migrations\Remove_Incorrect_Tracking_Id::class );
}

public function tear_down(): void {
delete_option( Settings::SETTING_NAME_TRACKING_ID );

parent::tear_down();
}

/**
* @covers ::migrate
*/
public function test_migrate_unchanged(): void {
$tracking_id = 'UA-12345678-9';
update_option( Settings::SETTING_NAME_TRACKING_ID, $tracking_id );

$this->instance->migrate();

$this->assertSame( $tracking_id, get_option( Settings::SETTING_NAME_TRACKING_ID ) );
}

/**
* @covers ::migrate
*/
public function test_migrate_reset(): void {
$tracking_id = Tracking::TRACKING_ID;
update_option( Settings::SETTING_NAME_TRACKING_ID, $tracking_id );

$this->instance->migrate();

$this->assertSame( '', get_option( Settings::SETTING_NAME_TRACKING_ID ) );
}
}