Skip to content
This repository was archived by the owner on Apr 27, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ mixpanel.identify('user@email.com');
// register 'Gender' as a super property
mixpanel.register({'Gender': 'Female'});

// register 'Gender' as a super property if not already registered
mixpanel.register_once({'Gender': 'Female'});

// assign user info
mixpanel.people.set({
$email: 'user@email.com' // only special properties need the $
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mixpanel-lite",
"version": "1.5.3",
"version": "1.6.0",
"description": "A lightweight alternative to mixpanel-js with offline support for Hybrid and Progressive Web Apps",
"main": "src/mixpanel-lite.js",
"scripts": {
Expand Down
19 changes: 19 additions & 0 deletions src/mixpanel-lite.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@
});
}

/**
* Register a set of super properties unless they are already added
* @param {object} data - JSON key/value pair
* @returns {void}
*/
function register_once(data) {
unpersistedData = Object.keys(data = {}).reduce(function (acc, key) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I'm struggling to understand what's happening here... could this be refactored to improve readability?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The idea is to only register data that hasn't already been registered. The reduce is building a new object out of the data based on which keys haven't been added to properties already. Then I'm registering that new object using the regular register function. To me, this is the clearest way to do that, but if you have a preference for another style I can try changing it.

@john-doherty john-doherty Mar 6, 2024

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Sorry, a "bit" late to this. Chat GPT suggested the following (based on your code):

/**
 * Register a set of super properties unless they are already added.
 * @param {object} data - JSON key/value pair of properties to register.
 * @returns {void}
 */
function register_once(data) {
    var unpersistedData = {};

    // Check each property in the input data
    Object.keys(data).forEach(function(key) {
        // If the property does not exist in _properties, add it to unpersistedData
        if (!_properties[key]) {
            unpersistedData[key] = data[key];
        }
    });

    // Register the new properties that were not already present
    register(unpersistedData);
}

if (!_properties[key]) {
acc[key] = data[key];
}
return acc;
}, {});

register(unpersistedData);
}

/**
* set properties on an user record in engage
* @param {object} data - properties to set
Expand Down Expand Up @@ -645,6 +661,9 @@
register: function(data) {
console.log('mixpanel.register(' + JSON.stringify(data || {}) + ')');
},
register_once: function(data) {
console.log('mixpanel.register_once(' + JSON.stringify(data || {}) + ')');
},
reset: function () {
console.log('mixpanel.reset()');
},
Expand Down