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

Add targeting snippets #3944

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Add targeting snippets #3944

wants to merge 6 commits into from

Conversation

bwreid
Copy link
Contributor

@bwreid bwreid commented Mar 3, 2025

Description

Adds snippets to be used within the Targeting Cheatsheet doc. The only files that will be linked within the docs are the non-index files within the targeted-page/ directory.

@bwreid bwreid requested a review from samijaber March 3, 2025 19:53
Copy link

changeset-bot bot commented Mar 3, 2025

⚠️ No Changeset found

Latest commit: d2ec816

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

gitguardian bot commented Mar 3, 2025

⚠️ GitGuardian has uncovered 2 secrets following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

🔎 Detected hardcoded secrets in your pull request
GitGuardian id GitGuardian status Secret Commit Filename
2708648 Triggered Generic High Entropy Secret d2ec816 examples/angular-gen2/src/environments/environment.ts View secret
11707119 Triggered Generic High Entropy Secret d2ec816 packages/sdks/snippets/react/src/routes/query-cheatsheet/index.tsx View secret
🛠 Guidelines to remediate hardcoded secrets
  1. Understand the implications of revoking this secret by investigating where it is used in your code.
  2. Replace and store your secrets safely. Learn here the best practices.
  3. Revoke and rotate these secrets.
  4. If possible, rewrite git history. Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider


🦉 GitGuardian detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.

Copy link

nx-cloud bot commented Mar 3, 2025

View your CI Pipeline Execution ↗ for commit d2ec816.

Command Status Duration Result
nx test @snippet/react ❌ Failed 1m 19s View ↗
nx test @e2e/qwik-city ✅ Succeeded 7m 45s View ↗
nx test @e2e/nextjs-sdk-next-app ✅ Succeeded 7m 35s View ↗
nx test @e2e/nuxt ✅ Succeeded 7m 10s View ↗
nx test @e2e/gen1-react ✅ Succeeded 6m 28s View ↗
nx test @e2e/angular-16-ssr ✅ Succeeded 6m 16s View ↗
nx test @e2e/remix ✅ Succeeded 6m 21s View ↗
nx test @e2e/angular-16 ✅ Succeeded 6m 14s View ↗
Additional runs (36) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-03-13 00:07:41 UTC

@@ -0,0 +1,9 @@
import { builder } from '@builder.io/react';

export default async ({ model, urlPath }: { model: string; urlPath: string }) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

  • unnecessary async
  • for consistency all requests fns should have the same name. I see some don't have a name at all, and some are called request. I find request to be a bit of a special keyword in JavaScript land. I'd opt for a name like targettedRequest

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, @samijaber. I've updated the code to remove the last few async keywords. I've also used the GetContentOptions type in Gen 1 now that I know where it is.

- Remove unnecessary async keywords
- Rename functions to all be the same
- Include GetContentOptions type for Gen 1 React
Copy link
Contributor

@samijaber samijaber left a comment

Choose a reason for hiding this comment

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

Just took a look at the doc: https://www.builder.io/c/docs/targeting-cheatsheet#examples-of-custom-targeting-attributes

Are we reworking the doc completely? Right now that page uses builder.setUserAttributes() to illustrate how those attributes can be set, whereas this PR is using builder.get() calls

Comment on lines 4 to 11
export default function targetedRequest({ model, url }: GetContentOptions) {
return builder.get(model!, {
userAttributes: {
audience: ['mens-fashion'],
urlPath: url,
},
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Putting all that together:

Suggested change
export default function targetedRequest({ model, url }: GetContentOptions) {
return builder.get(model!, {
userAttributes: {
audience: ['mens-fashion'],
urlPath: url,
},
});
}
export default function targetedRequest(modelName: string, options: GetContentOptions) {
return builder.get(modelName, {
userAttributes: {
audience: ['mens-fashion'],
},
...options,
});
}

Comment on lines 1 to 15
import { fetchOneEntry, GetContentOptions } from '@builder.io/sdk-react';

export default function targetedRequest(
urlPath: string,
{ apiKey, model }: GetContentOptions
) {
return fetchOneEntry({
apiKey,
model,
userAttributes: {
isLoggedIn: true,
urlPath,
},
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The arguments of targetedRequest add a layer of indirection that can complicate the code snippet for the end-user.

I would stick with a philosophy of "spread everything" to keep this wrapper function as unobtrusive as possible:

Suggested change
import { fetchOneEntry, GetContentOptions } from '@builder.io/sdk-react';
export default function targetedRequest(
urlPath: string,
{ apiKey, model }: GetContentOptions
) {
return fetchOneEntry({
apiKey,
model,
userAttributes: {
isLoggedIn: true,
urlPath,
},
});
}
import { fetchOneEntry, GetContentOptions } from '@builder.io/sdk-react';
export default function targetedRequest(options: GetContentOptions) {
return fetchOneEntry({
...options,
userAttributes: {
isLoggedIn: true,
...options.userAttributes,
},
});
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@samijaber Is your assumption then that the arguments provided to targetedRequest() will look something like this?

const options = {
  apiKey: BUILDER_API_KEY,
  model: MODEL,
  userAttributes: {
    urlPath: window.location.pathname,
  },
};

As someone new to the code, I feel like having urlPath explicit within the code snippet feels important as the request will not work without it. Do you think something like this is too verbose?

import { fetchOneEntry, GetContentOptions } from '@builder.io/sdk-react';

export default function targetedRequest(options: GetContentOptions) {
  return fetchOneEntry({
    ...options,
    userAttributes: {
      isLoggedIn: true,
      urlPath: options.userAttributes?.urlPath,
      ...options.userAttributes, // Include additional userAttributes
    },
  });
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Is your assumption then that the arguments provided to targetedRequest() will look something like this?

Yeah, I want to avoid creating an extra abstraction. I want targetedRequest to have the same arguments as fetchOneEntry.

True that urlPath needs to exist (in most cases) for a response to return to us, but given that the focus of the snippet is setting/passing user attributes, I think it's OK if we imply "the spread arguments will contain everything else that you need"

@bwreid
Copy link
Contributor Author

bwreid commented Mar 4, 2025

Just took a look at the doc: https://www.builder.io/c/docs/targeting-cheatsheet#examples-of-custom-targeting-attributes

Are we reworking the doc completely? Right now that page uses builder.setUserAttributes() to illustrate how those attributes can be set, whereas this PR is using builder.get() calls

I am trying to make some significant changes to the doc. There are more details in the Jira ticket, but in short I would like to separate out the code blocks from the more generalized information about targeting.

I also worked on the Querying Cheatsheet, which has a similar name but is structured very differently. This page seems focused around being able to quickly find the code snippet you need and copy it to your code, while the current Targeting Cheatsheet acts a bit more like a tutorial.

If you disagree with this approach, however, please feel free to let me know. I'm very much still getting a handle on all the documentation.

Copy link
Contributor

@samijaber samijaber left a comment

Choose a reason for hiding this comment

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

hmm, i see these as two separate things:

  • setting a user attribute:

    • gen1: builder.setUserAttribute()
    • gen2: setClientUserAttributes()
  • using a user attribute when fetch content: userAttributes: {} argument to builder.get and fetchOneEntry/fetchEntries

The doc currently has the former, you're replacing it with a latter. I think we'd like both ideally!

}
}
fetchContent();
}, [window.location.pathname]);
Copy link
Contributor

Choose a reason for hiding this comment

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

heads up that useEffect cannot listen to changes in window.location.pathname.

this is used for our own testing, so as long as you get the desired behaviour it's fine i guess, but more of a heads up in general

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I did not know this but it makes sense! 👍

bwreid added 2 commits March 12, 2025 15:56
- Rework function signatures
- Add example cases for using Gen 1 and Gen 2 set attributes
- Add tests that succeed for Gen 1 but fail for Gen 2
Comment on lines +3 to +8
export default function setTargetingAttributes() {
setClientUserAttributes({
device: 'desktop',
audience: ['recent-shopper', 'womens-fashion'],
});
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@samijaber Here is the option that's failing. I am using this function and file similarly in the Gen 1 directory.

This function does set the cookies appropriately but fetchOneEntry() does not seem to pick up the details in the cookie.

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.

2 participants