Skip to content

Commit

Permalink
Migrating the tracks-callables.js file to the Tracking package. (#19551)
Browse files Browse the repository at this point in the history
* Adding the tracks-callables.js file to the Tracking package.
Related: Automattic/jetpack#15929

* Adding changelog entry.

* Removing the calls to wp_register_script() for the tracks-callable.js file.
Removing the jp-tracks-functions slug from the array of script slugs.

* Adding changelog entry.

* Reverting the changes to class.jetpack.php.
It seems we'll still need to enqueue the scripts here.

* Adding the register_tracks_scripts() method.
This will handle calling the wp_register_script() function for the tracks-callables.js file.

In the class.jetpack.php file, replacing the call to the wp_register_script() function with the $tracking->register_tracks_scripts() method.

* Calling the $tracking->register_tracks_scripts() method in the search widget.

* Register jp-tracks dependency when registering jp-tracks-functions.
Rename register function to destinguish it from the jptracks used in enqueue.
Update register function to static to not instantiate class.
Update dependencies to simplify only needed entries.

* Update changelogger entry so Jetpack has changelog entry.

* Update the register function to optionally enqueue. Necessary when other packages rely on the script but shouldn't be aware of file location details.

* reverse conditional so more intuitive.

Co-authored-by: Matthew Denton <matt@mdbitz.com>

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/814434312
  • Loading branch information
mdbitz authored and matticbot committed May 5, 2021
1 parent 69086f0 commit eb0da50
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This is an alpha version! The changes listed here are not final.
- Comment: Story Block: Improve accessibility and resolve z-index issues when playing in fullscreen.
- Fixed compatibility issue with Page Optimize plugin for RTL layouts for jetpack-admin-menu and colors stylesheets
- Hide Settings page for non-admin users when in site-only connection
- jp-tracks-functions script moved to Tracking package.
- Update Sync Connection Tests to utilize fix nomenclature

### Bug fixes
Expand Down
17 changes: 2 additions & 15 deletions class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -4168,21 +4168,8 @@ public function deactivate_dialog( $hook ) {

add_thickbox();

wp_register_script(
'jp-tracks',
'//stats.wp.com/w.js',
array(),
gmdate( 'YW' ),
true
);

wp_register_script(
'jp-tracks-functions',
plugins_url( '_inc/lib/tracks/tracks-callables.js', JETPACK__PLUGIN_FILE ),
array( 'jp-tracks' ),
JETPACK__VERSION,
false
);
// Register jp-tracks-functions dependency.
Tracking::register_tracks_functions_scripts();

wp_enqueue_script(
'jetpack-deactivate-dialog-js',
Expand Down
21 changes: 4 additions & 17 deletions modules/widgets/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Automattic\Jetpack\Constants;
use Automattic\Jetpack\Status;
use Automattic\Jetpack\Redirect;
use Automattic\Jetpack\Tracking;

add_action( 'widgets_init', 'jetpack_search_widget_init' );

Expand Down Expand Up @@ -127,27 +128,13 @@ public function activate_search() {
public function widget_admin_setup() {
wp_enqueue_style( 'widget-jetpack-search-filters', plugins_url( 'search/css/search-widget-admin-ui.css', __FILE__ ) );

// Required for Tracks
wp_register_script(
'jp-tracks',
'//stats.wp.com/w.js',
array(),
gmdate( 'YW' ),
true
);

wp_register_script(
'jp-tracks-functions',
plugins_url( '_inc/lib/tracks/tracks-callables.js', JETPACK__PLUGIN_FILE ),
array(),
JETPACK__VERSION,
false
);
// Register jp-tracks and jp-tracks-functions.
Tracking::register_tracks_functions_scripts();

wp_register_script(
'jetpack-search-widget-admin',
plugins_url( 'search/js/search-widget-admin.js', __FILE__ ),
array( 'jquery', 'jquery-ui-sortable', 'jp-tracks', 'jp-tracks-functions' ),
array( 'jquery', 'jquery-ui-sortable', 'jp-tracks-functions' ),
JETPACK__VERSION
);

Expand Down
3 changes: 3 additions & 0 deletions vendor/automattic/jetpack-tracking/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

This is an alpha version! The changes listed here are not final.

### Added
- Adding the tracks-callables.js file to the Tracking package.

## [1.13.5] - 2021-04-27
### Changed
- Updated package dependencies.
Expand Down
38 changes: 38 additions & 0 deletions vendor/automattic/jetpack-tracking/src/class-tracking.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,44 @@ public function ajax_tracks() {
wp_send_json_success();
}

/**
* Register script necessary for tracking.
*
* @param boolean $enqueue Also enqueue? defaults to false.
*/
public static function register_tracks_functions_scripts( $enqueue = false ) {

// Register jp-tracks as it is a dependency.
wp_register_script(
'jp-tracks',
'//stats.wp.com/w.js',
array(),
gmdate( 'YW' ),
true
);

if ( $enqueue ) {
// Enqueue jp-tracks-functions script.
wp_enqueue_script(
'jp-tracks-functions',
Assets::get_file_url_for_environment( 'js/tracks-callables.js', 'js/tracks-callables.js', __FILE__ ),
array( 'jp-tracks' ),
self::ASSETS_VERSION,
true
);
} else {
// Register jp-tracks-functions script.
wp_register_script(
'jp-tracks-functions',
Assets::get_file_url_for_environment( 'js/tracks-callables.js', 'js/tracks-callables.js', __FILE__ ),
array( 'jp-tracks' ),
self::ASSETS_VERSION,
true
);
}

}

/**
* Enqueue script necessary for tracking.
*/
Expand Down
79 changes: 79 additions & 0 deletions vendor/automattic/jetpack-tracking/src/js/tracks-callables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* This was abstracted from wp-calypso's analytics lib: https://github.com/Automattic/wp-calypso/blob/master/client/lib/analytics/README.md
* Some stuff was removed like GA tracking and other things not necessary for Jetpack tracking.
*
* This library should only be used and loaded if the Jetpack site is connected.
*/

// Load tracking scripts
window._tkq = window._tkq || [];

var _user;
var debug = console.error; // eslint-disable-line no-console

function buildQuerystring( group, name ) {
var uriComponent = '';

if ( 'object' === typeof group ) {
for ( var key in group ) {
uriComponent += '&x_' + encodeURIComponent( key ) + '=' + encodeURIComponent( group[ key ] );
}
} else {
uriComponent = '&x_' + encodeURIComponent( group ) + '=' + encodeURIComponent( name );
}

return uriComponent;
}

var analytics = {
initialize: function ( userId, username ) {
analytics.setUser( userId, username );
analytics.identifyUser();
},

mc: {
bumpStat: function ( group, name ) {
var uriComponent = buildQuerystring( group, name ); // prints debug info
new Image().src =
document.location.protocol +
'//pixel.wp.com/g.gif?v=wpcom-no-pv' +
uriComponent +
'&t=' +
Math.random();
},
},

tracks: {
recordEvent: function ( eventName, eventProperties ) {
eventProperties = eventProperties || {};

if ( eventName.indexOf( 'jetpack_' ) !== 0 ) {
debug( '- Event name must be prefixed by "jetpack_"' );
return;
}

window._tkq.push( [ 'recordEvent', eventName, eventProperties ] );
},

recordPageView: function ( urlPath ) {
analytics.tracks.recordEvent( 'jetpack_page_view', {
path: urlPath,
} );
},
},

setUser: function ( userId, username ) {
_user = { ID: userId, username: username };
},

identifyUser: function () {
// Don't identify the user if we don't have one
if ( _user ) {
window._tkq.push( [ 'identifyUser', _user.ID, _user.username ] );
}
},

clearedIdentity: function () {
window._tkq.push( [ 'clearIdentity' ] );
},
};

0 comments on commit eb0da50

Please sign in to comment.