Skip to content

Conversation

@0xsatoshi99
Copy link

Summary

This PR adds a new gmail.track namespace with easy-to-use helper functions for tracking sent emails, directly addressing the user request in issue #646.

Fixes #646

Problem

Users wanted a simple way to track sent emails but found the existing observe.on() API confusing, especially for beginners (as mentioned in #646).

Solution

Added a new gmail.track namespace that provides:

  • ✅ Intuitive, beginner-friendly API
  • ✅ Clean wrapper around existing observe.on() functionality
  • ✅ Comprehensive documentation with examples
  • ✅ Built-in debugging tools

Changes

New API Functions (129 lines in src/gmail.js)

1. gmail.track.sent_emails(callback)

Track emails when they are sent immediately.

gmail.track.sent_emails(function(emailData, url, body, xhr) {
    console.log('Email sent!');
    console.log('To:', emailData.to);
    console.log('Subject:', emailData.subject);
});

2. gmail.track.scheduled_emails(callback)

Track emails when they are scheduled for later.

gmail.track.scheduled_emails(function(emailData, url, body, xhr) {
    console.log('Email scheduled!');
    console.log('Scheduled time:', emailData.scheduled_time);
});

3. gmail.track.all_emails(callback)

Track both sent and scheduled emails with a single callback.

gmail.track.all_emails(function(emailData, type, url, body, xhr) {
    if (type === 'sent') {
        console.log('Email sent immediately');
    } else if (type === 'scheduled') {
        console.log('Email scheduled for later');
    }
});

4. gmail.track.logger(prefix)

Built-in logger for debugging and testing.

// Quick debugging
gmail.track.sent_emails(gmail.track.logger('MY APP'));

Documentation (150 lines in examples/email-tracking.md)

Comprehensive guide including:

  • Basic usage examples
  • Email data structure reference
  • Advanced use cases:
    • Database integration
    • Email analytics
    • Notification systems
    • Browser extensions
  • Troubleshooting guide
  • Comparison with direct observer API

Benefits

1. Beginner-Friendly

  • Simple, intuitive API
  • Clear function names
  • Email data as first parameter (most important)
  • Comprehensive documentation

2. Solves Issue #646

  • Directly addresses user request
  • Makes email tracking accessible
  • Provides working examples
  • Includes real-world use cases

3. Backward Compatible

  • Doesn't modify existing API
  • Pure wrapper around observe.on()
  • No breaking changes
  • Existing code continues to work

4. Well-Documented

  • 150 lines of documentation
  • Multiple examples
  • Use case scenarios
  • Troubleshooting guide

API Comparison

Old Way (confusing for beginners)

gmail.observe.on('send_message', function(url, body, data, xhr) {
    // Parameters in unintuitive order
    // Need to know event name
    console.log('Email sent', data);
});

New Way (intuitive)

gmail.track.sent_emails(function(emailData, url, body, xhr) {
    // emailData first (most important)
    // Clear function name
    console.log('Email sent', emailData);
});

Use Cases

The documentation includes examples for:

  1. Email Analytics - Track sending patterns and statistics
  2. Delivery Confirmation - Notify users when emails are sent
  3. Backup System - Automatically backup sent emails
  4. Compliance - Log emails for auditing requirements
  5. A/B Testing - Track different email variations
  6. Browser Extensions - Integrate with Chrome/Firefox extensions

Testing

The new functions:

  • ✅ Validate callback parameters
  • ✅ Return callback reference for cleanup
  • ✅ Handle errors gracefully
  • ✅ Work with both old and new Gmail interfaces
  • ✅ Compatible with existing observer system

Code Quality

  • ✅ Comprehensive JSDoc comments
  • ✅ Clear parameter descriptions
  • ✅ Usage examples in comments
  • ✅ Error handling with api.tools.error()
  • ✅ Follows existing code style
  • ✅ No external dependencies

Total Changes

279 lines added across 2 files:

  • src/gmail.js: 129 lines (new API functions)
  • examples/email-tracking.md: 150 lines (documentation)

Related

Future Enhancements

Potential additions (not in this PR):

  • gmail.track.off() for removing observers
  • gmail.track.stats() for built-in analytics
  • gmail.track.export() for data export

Contribution by Gittensor, learn more at https://gittensor.io/

Fixes KartikTalwar#646

- Add gmail.track namespace with easy-to-use email tracking functions
- Implement gmail.track.sent_emails() for tracking sent emails
- Implement gmail.track.scheduled_emails() for tracking scheduled emails
- Implement gmail.track.all_emails() for tracking both types
- Add gmail.track.logger() for easy debugging
- Create comprehensive documentation with examples
- Provide cleaner API compared to direct observe.on() usage

Features:
- Simple callback-based API
- Email data as first parameter (most intuitive)
- Support for both sent and scheduled emails
- Built-in logger for debugging
- Comprehensive documentation with use cases
- Examples for analytics, notifications, backup systems

Benefits:
- Solves user request from issue KartikTalwar#646
- Makes email tracking accessible to beginners
- Provides clean wrapper around existing observe API
- Backward compatible (doesn't break existing code)
- Well-documented with real-world examples

Total: 279 lines (129 code + 150 documentation)
@@ -0,0 +1,150 @@
# Email Tracking with Gmail.js
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why add this documentation to a new markdown-file, rather than the existing README with all the other API documentation?

api.track.sent_emails = function(callback) {
if (typeof callback !== 'function') {
api.tools.error('track.sent_emails requires a callback function');
return null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

If people are using our API in a way which can never succeed, we should throw an error, not return null IMO.

This is anyway a kind of API where people are almost guaranteed to not check the return-value (as you yourself are not doing in your examples).

api.track.scheduled_emails = function(callback) {
if (typeof callback !== 'function') {
api.tools.error('track.scheduled_emails requires a callback function');
return null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

If people are using our API in a way which can never succeed, we should through, not return null IMO.

This is anyway a kind of API where people are almost guaranteed to not check the return-value (as you yourself are not doing in your examples).

api.track.all_emails = function(callback) {
if (typeof callback !== 'function') {
api.tools.error('track.all_emails requires a callback function');
return null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

If people are using our API in a way which can never succeed, we should through, not return null IMO.

This is anyway a kind of API where people are almost guaranteed to not check the return-value (as you yourself are not doing in your examples).

return {
sent: sentCallback,
scheduled: scheduledCallback
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

What value does it provide to return these items? If the intent is to allow people to unregister, the API should provide symmetric means of doing so.

};
};

return api;
Copy link
Collaborator

Choose a reason for hiding this comment

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

All in all I'm positive to these changes. It makes some functionality more up-front and accessible. That's good!

One thing though, when adding new APIs, make sure to add the type-definitions for those new APIs to gmail.d.ts as well.

We have many typescript users these days 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How do I track an email that has been sent??

2 participants