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

feat: snap extra parametrs pass #1921

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

Conversation

aashishmalik
Copy link
Contributor

@aashishmalik aashishmalik commented Nov 14, 2024

PR Description

Please include a summary of the change along with the relevant motivation and context.

Linear task (optional)

Linear task link

Cross Browser Tests

Please confirm you have tested for the following browsers:

  • Chrome
  • Firefox
  • IE11

Sanity Suite

  • All sanity suite test cases pass locally

Security

  • The code changed/added as part of this pull request won't create any security issues with how the software is being used.

Summary by CodeRabbit

  • New Features

    • Enhanced user identification by including additional traits such as age, first name, last name, postal code, country, city, and state in the Snap Pixel integration.
    • Improved data extraction capabilities with the addition of postal code retrieval in utility functions.
  • Bug Fixes

    • Minor formatting adjustment in the Spotify Pixel integration for consistency.
  • Tests

    • Updated test cases for utility functions to align with the new expected output structure, including the addition of postal code.

These updates improve the accuracy and comprehensiveness of user data sent to analytics services.

Copy link
Contributor

coderabbitai bot commented Nov 14, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The pull request introduces modifications across three files related to user trait handling in analytics integrations. In the SnapPixel integration, the identify method has been updated to extract additional user traits using the getDefinedTraits function. The SpotifyPixel integration sees a minor formatting change with a semicolon added to an import statement. Lastly, the utils.js file enhances the getDefinedTraits function to include a new property for postalCode, improving data extraction capabilities without altering existing logic.

Changes

File Path Change Summary
packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js Updated the identify method to extract additional user traits and modified payload construction.
packages/analytics-js-integrations/src/integrations/SpotifyPixel/browser.js Added a semicolon to an import statement; no functional changes.
packages/analytics-js-integrations/src/utils/utils.js Enhanced getDefinedTraits to include postalCode and standardized string formatting.
packages/analytics-js-integrations/tests/utils/utils.test.js Updated test cases to reflect changes in getDefinedTraits, including the new postalCode property.

Possibly related PRs

  • feat: gainsight PX destination #1852: The changes in the main PR involve the getDefinedTraits function, which is also referenced in the GainsightPX integration's identify method, indicating a direct relationship in how user traits are handled across both integrations.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js (2)

16-16: Consider using relative paths for internal imports.

The import statement uses a package name path which could be fragile if the package structure changes. Consider using a relative path instead.

-import { getDefinedTraits } from '@rudderstack/analytics-js-integrations/utils/utils';
+import { getDefinedTraits } from '../../utils/utils';

115-125: Consider adding validation for the new user traits.

While the implementation is correct, consider adding validation for the new fields to ensure data quality:

  • Age should be a positive number
  • Postal code format varies by country
  • State/region codes might need standardization

Example validation implementation:

const validateUserTraits = (traits) => {
  const { age, postalCode, state } = traits;
  
  if (age !== undefined && (typeof age !== 'number' || age <= 0)) {
    logger.warn('Invalid age value provided');
    delete traits.age;
  }
  
  // Add more validation as needed
  
  return traits;
};

// Use in identify method
payload = validateUserTraits({
  ...payload,
  ip_address: ipAddress,
  firstname: firstName,
  // ... other fields
});
packages/analytics-js-integrations/src/utils/utils.js (1)

274-280: Consider additional postal code field variations

The implementation follows the established pattern for address fields and provides good coverage of common field names. Consider adding these additional variations that are commonly used:

  • zip_code
  • zipCode
  • zipcode
  • address.zip
  • address.zip_code
 postalCode:
   get(message, 'context.traits.postalCode') ||
   get(message, 'context.traits.postalcode') ||
   get(message, 'context.traits.postal_code') ||
   get(message, 'context.traits.address.postalCode') ||
   get(message, 'context.traits.address.postal_code') ||
-  get(message, 'context.traits.address.postalcode'),
+  get(message, 'context.traits.address.postalcode') ||
+  get(message, 'context.traits.zipCode') ||
+  get(message, 'context.traits.zipcode') ||
+  get(message, 'context.traits.zip_code') ||
+  get(message, 'context.traits.zip') ||
+  get(message, 'context.traits.address.zip') ||
+  get(message, 'context.traits.address.zip_code'),
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 07909cf and f2c6374.

📒 Files selected for processing (3)
  • packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js (3 hunks)
  • packages/analytics-js-integrations/src/integrations/SpotifyPixel/browser.js (1 hunks)
  • packages/analytics-js-integrations/src/utils/utils.js (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/analytics-js-integrations/src/integrations/SpotifyPixel/browser.js
🔇 Additional comments (3)
packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js (1)

103-104: LGTM! Clean implementation of additional user traits.

The extraction of additional user traits is well-implemented and aligns with Snap's supported user properties.

packages/analytics-js-integrations/src/utils/utils.js (2)

151-151: LGTM: Minor string quote change

The change from double to single quotes for the circular reference string is consistent with the codebase's string formatting.


274-280: Verify impact on existing integrations

Let's verify if any existing integrations rely on specific postal code field names and ensure this change doesn't affect them.

✅ Verification successful

Based on the search results, I can now generate the final response since I have sufficient information about postal code usage across integrations.

Changes to postal code field mapping are safe and aligned with existing integrations

The current implementation in utils.js that checks multiple variations of postal code fields (postalCode, postalcode, postal_code) is consistent with how integrations are using these fields. Specifically:

  • SnapPixel uses postalCode format
  • Klaviyo uses both zip and address.postalCode
  • GoogleAds uses postalCode and postal_code
  • Criteo uses zipCode and zip
  • AdobeAnalytics uses zip

The current implementation in utils.js properly handles all these variations, ensuring backward compatibility and proper field mapping across integrations.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for postal code usage in integration files

# Look for postal code field references in integration files
echo "Searching for postal code usage in integrations..."
rg -i "postal.*code|zip.*code|zipcode|zip" packages/analytics-js-integrations/src/integrations/

# Look for address field mapping patterns
echo "Searching for address field mapping patterns..."
ast-grep --pattern 'getDefinedTraits($message)' packages/analytics-js-integrations/src/integrations/

Length of output: 5352

coderabbitai[bot]
coderabbitai bot previously approved these changes Nov 14, 2024
Copy link

github-actions bot commented Nov 14, 2024

size-limit report 📦

Name Size (Base) Size (Current) Size Limit Status
Plugins Module Federation Mapping - Legacy - CDN 332 B 332 B (0%) 512 B
Plugins - Legacy - CDN 15.71 KB 15.71 KB (0%) 16 KB
Plugins Module Federation Mapping - Modern - CDN 331 B 331 B (0%) 512 B
Plugins - Modern - CDN 7.2 KB 7.2 KB (0%) 7.5 KB
Common - No bundling 16.27 KB 16.27 KB (0%) 16.5 KB
Cookies Utils - Legacy - NPM (ESM) 1.54 KB 1.54 KB (0%) 2 KB
Cookies Utils - Legacy - NPM (CJS) 1.75 KB 1.75 KB (0%) 2 KB
Cookies Utils - Legacy - NPM (UMD) 1.53 KB 1.53 KB (0%) 2 KB
Cookies Utils - Modern - NPM (ESM) 1.17 KB 1.17 KB (0%) 1.5 KB
Cookies Utils - Modern - NPM (CJS) 1.4 KB 1.4 KB (0%) 1.5 KB
Cookies Utils - Modern - NPM (UMD) 1.16 KB 1.16 KB (0%) 1.5 KB
Load Snippet 778 B 778 B (0%) 1 KB
Core (v1.1) - NPM (ESM) 30.32 KB 30.32 KB (0%) 32 KB
Core (v1.1) - NPM (CJS) 30.52 KB 30.52 KB (0%) 32 KB
Core (v1.1) - NPM (UMD) 30.34 KB 30.34 KB (0%) 32 KB
Core (Content Script - v1.1) - NPM (ESM) 29.89 KB 29.89 KB (0%) 30.5 KB
Core (Content Script - v1.1) - NPM (CJS) 30.04 KB 30.04 KB (0%) 30.5 KB
Core (Content Script - v1.1) - NPM (UMD) 29.98 KB 29.98 KB (0%) 30 KB
Core - Legacy - CDN 48.51 KB 48.51 KB (0%) 49 KB
Core - Modern - CDN 24.75 KB 24.75 KB (0%) 25 KB
Core - Legacy - NPM (ESM) 48.33 KB 48.33 KB (0%) 48.5 KB
Core - Legacy - NPM (CJS) 48.6 KB 48.6 KB (0%) 49 KB
Core - Legacy - NPM (UMD) 48.36 KB 48.36 KB (0%) 48.5 KB
Core - Modern - NPM (ESM) 24.5 KB 24.5 KB (0%) 25 KB
Core - Modern - NPM (CJS) 24.74 KB 24.74 KB (0%) 25 KB
Core - Modern - NPM (UMD) 24.53 KB 24.53 KB (0%) 25 KB
Core (Bundled) - Legacy - NPM (ESM) 48.33 KB 48.33 KB (0%) 48.5 KB
Core (Bundled) - Legacy - NPM (CJS) 48.58 KB 48.58 KB (0%) 49 KB
Core (Bundled) - Legacy - NPM (UMD) 48.36 KB 48.36 KB (0%) 48.5 KB
Core (Bundled) - Modern - NPM (ESM) 39.48 KB 39.48 KB (0%) 40 KB
Core (Bundled) - Modern - NPM (CJS) 39.75 KB 39.75 KB (0%) 40 KB
Core (Bundled) - Modern - NPM (UMD) 39.49 KB 39.49 KB (0%) 40 KB
Core (Content Script) - Legacy - NPM (ESM) 47.83 KB 47.83 KB (0%) 48 KB
Core (Content Script) - Legacy - NPM (CJS) 48.06 KB 48.06 KB (0%) 48.5 KB
Core (Content Script) - Legacy - NPM (UMD) 47.8 KB 47.8 KB (0%) 48 KB
Core (Content Script) - Modern - NPM (ESM) 38.96 KB 38.96 KB (0%) 39.5 KB
Core (Content Script) - Modern - NPM (CJS) 39.2 KB 39.2 KB (0%) 40 KB
Core (Content Script) - Modern - NPM (UMD) 38.93 KB 38.93 KB (0%) 39 KB
Service Worker - Legacy - NPM (ESM) 30.56 KB 30.56 KB (0%) 31 KB
Service Worker - Legacy - NPM (CJS) 30.83 KB 30.83 KB (0%) 31 KB
Service Worker - Legacy - NPM (UMD) 30.65 KB 30.65 KB (0%) 31 KB
Service Worker - Modern - NPM (ESM) 25.74 KB 25.74 KB (0%) 26 KB
Service Worker - Modern - NPM (CJS) 26 KB 26 KB (0%) 26.5 KB
Service Worker - Modern - NPM (UMD) 25.75 KB 25.75 KB (0%) 26 KB
All Integrations - Legacy - CDN 94.83 KB 94.92 KB (+0.1% ▲) 95.3 KB
All Integrations - Modern - CDN 89.92 KB 90.14 KB (+0.24% ▲) 91 KB

Copy link

codecov bot commented Nov 14, 2024

Codecov Report

Attention: Patch coverage is 40.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 57.51%. Comparing base (07909cf) to head (338cbb7).

Files with missing lines Patch % Lines
...integrations/src/integrations/SnapPixel/browser.js 25.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1921      +/-   ##
===========================================
- Coverage    57.52%   57.51%   -0.01%     
===========================================
  Files          485      485              
  Lines        16505    16508       +3     
  Branches      3315     3298      -17     
===========================================
+ Hits          9494     9495       +1     
- Misses        5732     5794      +62     
+ Partials      1279     1219      -60     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

sonarcloud bot commented Nov 15, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
75.0% Coverage on New Code (required ≥ 90%)

See analysis details on SonarQube Cloud

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js (1)

Line range hint 103-125: Consider adding validation for new user traits

While the code adds several new user traits, it doesn't validate their format or handle potential edge cases. Consider:

  1. Adding type checks for numeric fields like age
  2. Format validation for postalCode
  3. Length limits for firstName and lastName
  4. Case normalization for geographic fields

Would you like me to provide an example implementation of these validations?

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 115-115: packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js#L115
Added line #L115 was not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f2c6374 and 338cbb7.

📒 Files selected for processing (4)
  • packages/analytics-js-integrations/__tests__/utils/utils.test.js (2 hunks)
  • packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js (3 hunks)
  • packages/analytics-js-integrations/src/integrations/SpotifyPixel/browser.js (1 hunks)
  • packages/analytics-js-integrations/src/utils/utils.js (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/analytics-js-integrations/src/integrations/SpotifyPixel/browser.js
  • packages/analytics-js-integrations/src/utils/utils.js
🧰 Additional context used
🪛 GitHub Check: codecov/patch
packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js

[warning] 103-104: packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js#L103-L104
Added lines #L103 - L104 were not covered by tests


[warning] 115-115: packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js#L115
Added line #L115 was not covered by tests

🔇 Additional comments (5)
packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js (3)

16-16: LGTM: Import statement follows consistent pattern

The import of getDefinedTraits is properly structured and follows the established pattern in the codebase.


103-104: 🛠️ Refactor suggestion

Standardize trait extraction approach

The code uses two different approaches for trait extraction:

  • Direct extraction using get() for age
  • Using getDefinedTraits() for other traits

This inconsistency could lead to maintenance issues.

Consider using getDefinedTraits() for all traits:

-const age = get(message, 'context.traits.age');
-const { firstName, lastName, postalCode, country, city, state } = getDefinedTraits(message);
+const { age, firstName, lastName, postalCode, country, city, state } = getDefinedTraits(message);

Additionally, these new changes lack test coverage.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 103-104: packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js#L103-L104
Added lines #L103 - L104 were not covered by tests


115-125: Verify field names against Snap's API documentation

The payload includes several new user traits. While the implementation looks clean, we should verify that the field names exactly match Snap's API expectations:

  • firstname
  • lastname
  • geo_city
  • geo_region
  • geo_postal_code
  • geo_country

Also, consider adding JSDoc comments to document these fields and their expected formats.

✅ Verification successful

Field names match Snap's API requirements

Based on the codebase analysis, the field names used in the payload are correct and consistent with Snap's API requirements:

  • The implementation follows a well-established pattern across the codebase
  • Field names like firstname, lastname, geo_city, geo_region, geo_postal_code, and geo_country are used consistently in the SnapPixel integration
  • The code includes proper payload sanitization through removeUndefinedAndNullValues
  • The implementation handles both hashed and unhashed user data appropriately
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any existing usage of these field names in tests or documentation
rg -l "firstname|lastname|geo_city|geo_region|geo_postal_code|geo_country" .

Length of output: 1176


Script:

#!/bin/bash
# Let's check the specific usage of these fields in SnapPixel integration and its tests
rg -A 5 -B 5 "firstname|lastname|geo_city|geo_region|geo_postal_code|geo_country" ./packages/analytics-js-integrations/src/integrations/SnapPixel/ ./packages/analytics-js-integrations/__tests__/integrations/SnapPixel/

# Also check for any Snap-specific documentation or constants
rg -A 5 -B 5 "Snap" ./packages/analytics-js-common/src/constants/integrations/

Length of output: 24391


Script:

#!/bin/bash
# Let's check for any Snap Pixel specific documentation or test files
rg -A 10 "payload" ./packages/analytics-js-integrations/src/integrations/SnapPixel/

# Also check for any test files that might contain payload examples
fd -e test.js -e spec.js . -x rg -l "SnapPixel.*payload"

Length of output: 16736

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 115-115: packages/analytics-js-integrations/src/integrations/SnapPixel/browser.js#L115
Added line #L115 was not covered by tests

packages/analytics-js-integrations/__tests__/utils/utils.test.js (2)

331-331: LGTM! Minor formatting improvement

The formatting change in the circular reference test case maintains consistent test behavior.


130-130: LGTM! Verify related changes in utils.js

The addition of postalCode assertion aligns with the PR objective of passing extra parameters in snap functionality.

Let's verify the corresponding implementation in utils.js:

✅ Verification successful

LGTM! The postalCode field is properly implemented

The getDefinedTraits function in utils.js correctly handles the postalCode field by checking multiple possible paths and variations:

postalCode:
  get(message, 'context.traits.postalCode') ||
  get(message, 'context.traits.postalcode') ||
  get(message, 'context.traits.postal_code') ||
  get(message, 'context.traits.address.postalCode') ||
  get(message, 'context.traits.address.postal_code') ||
  get(message, 'context.traits.address.postalcode'),

The implementation:

  • Follows the same pattern as other address fields (city, state, country)
  • Handles multiple casing variations (postalCode, postalcode, postal_code)
  • Checks both root traits and nested address object
  • Returns undefined if no value is found (matching test assertion)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify postalCode handling in getDefinedTraits implementation
# Expected: Should find postalCode being extracted from message object

ast-grep --pattern 'function getDefinedTraits($_) {
  $$$
  postalCode: $_
  $$$
}'

Length of output: 85


Script:

#!/bin/bash
# Let's try a different approach to find getDefinedTraits implementation
# First, find the utils.js file that corresponds to the test file
fd "utils.js" --type f --exclude "__tests__" packages/analytics-js-integrations

# Then let's examine its content to find getDefinedTraits
rg "getDefinedTraits" packages/analytics-js-integrations -A 10 -B 2

Length of output: 44332


Script:

#!/bin/bash
# Let's examine the implementation of getDefinedTraits in utils.js
cat packages/analytics-js-integrations/src/utils/utils.js

Length of output: 15688

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.

1 participant