-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add targeting snippets #3944
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
Merged
Merged
Add targeting snippets #3944
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f444450
Add targeting snippets
bwreid 307f760
Update targeted-page snippet to solve TS error
bwreid 53b51cc
Updates to targeted page snippets
bwreid c9d653d
Updates to targeted-page snippets
bwreid 7a19819
Add (failing) examples for setting attributes directly
bwreid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
packages/sdks-tests/src/snippet-tests/targeted-page-set-attributes.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import { test } from '../helpers/index.js'; | ||
|
|
||
| test.describe('TargetedPageSetAttributes Component', () => { | ||
| test.beforeEach(async ({ context, packageName }) => { | ||
| test.skip(!['gen1-react', 'react'].includes(packageName)); | ||
| await context.clearCookies(); | ||
| const page = await context.newPage(); | ||
| await page.goto('/targeted-page-set-attributes'); | ||
| }); | ||
|
|
||
| test('should render the No Targets page if there is no targeting', async ({ page }) => { | ||
| await page.goto('/targeted-page-set-attributes'); | ||
| expect(await page.locator('h2').innerText()).toContain('No Targets'); | ||
| }); | ||
|
|
||
| test('should render the Recent Shopper page when attributes are set', async ({ page }) => { | ||
| await page.goto('/targeted-page-set-attributes?target=set-attributes'); | ||
| expect(await page.locator('h2').innerText()).toContain('Recent Shopper'); | ||
| }); | ||
| }); |
41 changes: 41 additions & 0 deletions
41
packages/sdks-tests/src/snippet-tests/targeted-page.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import { test } from '../helpers/index.js'; | ||
|
|
||
| test.describe('TargetedPage Component', () => { | ||
| test.beforeEach(async ({ page, packageName }) => { | ||
| test.skip(!['gen1-react', 'react'].includes(packageName)); | ||
| await page.goto('/targeted-page'); | ||
| }); | ||
|
|
||
| test('should render the Desktop page if there is no targeting', async ({ page }) => { | ||
| await page.goto('/targeted-page'); | ||
| expect(await page.locator('h2').innerText()).toContain('Desktop'); | ||
| }); | ||
|
|
||
| test('should render the Desktop page with appropriate targeting', async ({ page }) => { | ||
| await page.goto('/targeted-page?target=desktop'); | ||
| expect(await page.locator('h2').innerText()).toContain('Desktop'); | ||
| }); | ||
|
|
||
| test('should render the Mobile page with appropriate targeting', async ({ page }) => { | ||
| await page.goto('/targeted-page?target=mobile'); | ||
| expect(await page.locator('h2').innerText()).toContain('Mobile'); | ||
| }); | ||
|
|
||
| test("should render the Men's Fashion page with appropriate targeting", async ({ page }) => { | ||
| await page.goto('/targeted-page?target=mens-fashion'); | ||
| expect(await page.locator('h2').innerText()).toContain("Men's Fashion"); | ||
| }); | ||
|
|
||
| test('should render the Recent Shopper page with appropriate targeting included in array', async ({ | ||
| page, | ||
| }) => { | ||
| await page.goto('/targeted-page?target=multi-target'); | ||
| expect(await page.locator('h2').innerText()).toContain('Recent Shopper'); | ||
| }); | ||
|
|
||
| test('should render the Logged In page with appropriate boolean targeting', async ({ page }) => { | ||
| await page.goto('/targeted-page?target=is-logged-in'); | ||
| expect(await page.locator('h2').innerText()).toContain('Logged'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...ages/sdks/snippets/gen1-react/src/routes/targeted-page/custom-targeting-multi-request.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { builder } from '@builder.io/react'; | ||
| import type { GetContentOptions } from '@builder.io/sdk'; | ||
|
|
||
| export default function targetedRequest( | ||
| modelName: string, | ||
| options: GetContentOptions | ||
| ) { | ||
| return builder.get(modelName, { | ||
| userAttributes: { | ||
| audience: ['recent-shopper', 'womens-fashion'], | ||
| }, | ||
| ...options, | ||
| }); | ||
| } |
14 changes: 14 additions & 0 deletions
14
packages/sdks/snippets/gen1-react/src/routes/targeted-page/custom-targeting-request.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { builder } from '@builder.io/react'; | ||
| import type { GetContentOptions } from '@builder.io/sdk'; | ||
|
|
||
| export default function targetedRequest( | ||
| modelName: string, | ||
| options: GetContentOptions | ||
| ) { | ||
| return builder.get(modelName, { | ||
| userAttributes: { | ||
| audience: ['mens-fashion'], | ||
| }, | ||
| ...options, | ||
| }); | ||
| } |
14 changes: 14 additions & 0 deletions
14
packages/sdks/snippets/gen1-react/src/routes/targeted-page/desktop-request.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { builder } from '@builder.io/react'; | ||
| import type { GetContentOptions } from '@builder.io/sdk'; | ||
|
|
||
| export default function targetedRequest( | ||
| modelName: string, | ||
| options: GetContentOptions | ||
| ) { | ||
| return builder.get(modelName, { | ||
| userAttributes: { | ||
| device: 'desktop', | ||
| }, | ||
| ...options, | ||
| }); | ||
| } |
62 changes: 62 additions & 0 deletions
62
packages/sdks/snippets/gen1-react/src/routes/targeted-page/index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { BuilderComponent, builder, useIsPreviewing } from '@builder.io/react'; | ||
| import { useEffect, useState } from 'react'; | ||
| import { useSearchParams } from 'react-router-dom'; | ||
| import FourOhFour from '../../components/404'; | ||
|
|
||
| import customTargetingMultiRequest from './custom-targeting-multi-request'; | ||
| import customTargetingRequest from './custom-targeting-request'; | ||
| import desktopRequest from './desktop-request'; | ||
| import loggedInRequest from './is-logged-in-request'; | ||
| import mobileRequest from './mobile-request'; | ||
| import noTargetRequest from './no-target-request'; | ||
|
|
||
| const MODEL = 'targeted-page'; | ||
| builder.init('ee9f13b4981e489a9a1209887695ef2b'); | ||
|
|
||
| export default function TargetedPage() { | ||
| const isPreviewingInBuilder = useIsPreviewing(); | ||
| const [notFound, setNotFound] = useState(false); | ||
| const [content, setContent] = useState(); | ||
| const [searchParams] = useSearchParams(); | ||
|
|
||
| const fnMap: { [key: string]: any } = { | ||
| desktop: desktopRequest, | ||
| mobile: mobileRequest, | ||
| 'mens-fashion': customTargetingRequest, | ||
| 'multi-target': customTargetingMultiRequest, | ||
| 'is-logged-in': loggedInRequest, | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| async function fetchContent() { | ||
| const target: string = searchParams.get('target') || ''; | ||
| const targetFn = target ? fnMap[target] : noTargetRequest; | ||
|
|
||
| const content = await targetFn(MODEL, { | ||
| url: window.location.pathname, | ||
| }); | ||
|
|
||
| setContent(content); | ||
| setNotFound(!content); | ||
|
|
||
| if (content?.data.title) { | ||
| document.title = content.data.title; | ||
| } | ||
| } | ||
| fetchContent(); | ||
| }, [window.location.pathname]); | ||
|
|
||
| if (notFound && !isPreviewingInBuilder) { | ||
| return <FourOhFour />; | ||
| } | ||
|
|
||
| console.log('...', builder.getUserAttributes()); | ||
|
|
||
| return ( | ||
| <> | ||
| <h1>Targeting Snippet</h1> | ||
| <hr /> | ||
| <BuilderComponent model={MODEL} content={content} /> | ||
| </> | ||
| ); | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
packages/sdks/snippets/gen1-react/src/routes/targeted-page/is-logged-in-request.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { builder } from '@builder.io/react'; | ||
| import type { GetContentOptions } from '@builder.io/sdk'; | ||
|
|
||
| export default function targetedRequest( | ||
| modelName: string, | ||
| options: GetContentOptions | ||
| ) { | ||
| return builder.get(modelName, { | ||
| userAttributes: { | ||
| isLoggedIn: true, | ||
| }, | ||
| ...options, | ||
| }); | ||
| } |
14 changes: 14 additions & 0 deletions
14
packages/sdks/snippets/gen1-react/src/routes/targeted-page/mobile-request.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import { builder } from '@builder.io/react'; | ||
| import type { GetContentOptions } from '@builder.io/sdk'; | ||
|
|
||
| export default function targetedRequest( | ||
| modelName: string, | ||
| options: GetContentOptions | ||
| ) { | ||
| return builder.get(modelName, { | ||
| userAttributes: { | ||
| device: 'mobile', | ||
| }, | ||
| ...options, | ||
| }); | ||
| } |
11 changes: 11 additions & 0 deletions
11
packages/sdks/snippets/gen1-react/src/routes/targeted-page/no-target-request.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { builder } from '@builder.io/react'; | ||
| import type { GetContentOptions } from '@builder.io/sdk'; | ||
|
|
||
| export default function targetedRequest( | ||
| modelName: string, | ||
| options: GetContentOptions | ||
| ) { | ||
| return builder.get(modelName, { | ||
| ...options, | ||
| }); | ||
| } |
49 changes: 49 additions & 0 deletions
49
packages/sdks/snippets/gen1-react/src/routes/targeted-page/set-attributes.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { BuilderComponent, builder, useIsPreviewing } from '@builder.io/react'; | ||
| import { useEffect, useState } from 'react'; | ||
| import { useSearchParams } from 'react-router-dom'; | ||
| import FourOhFour from '../../components/404'; | ||
| import setTargetingAttributes from './setUserAttributes'; | ||
|
|
||
| const MODEL = 'targeted-page'; | ||
| builder.init('ee9f13b4981e489a9a1209887695ef2b'); | ||
|
|
||
| export default function TargetedPageSetAttributes() { | ||
| const isPreviewingInBuilder = useIsPreviewing(); | ||
| const [notFound, setNotFound] = useState(false); | ||
| const [content, setContent] = useState(); | ||
| const [searchParams] = useSearchParams(); | ||
|
|
||
| useEffect(() => { | ||
| async function fetchContent() { | ||
| const target: string = searchParams.get('target') || ''; | ||
|
|
||
| if (target === 'set-attributes') { | ||
| setTargetingAttributes(); | ||
| } | ||
|
|
||
| const content = await builder.get(MODEL, { | ||
| url: window.location.pathname, | ||
| }); | ||
|
|
||
| setContent(content); | ||
| setNotFound(!content); | ||
|
|
||
| if (content?.data.title) { | ||
| document.title = content.data.title; | ||
| } | ||
| } | ||
| fetchContent(); | ||
| }, []); | ||
|
|
||
| if (notFound && !isPreviewingInBuilder) { | ||
| return <FourOhFour />; | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <h1>Targeting Snippet : Set Attributes</h1> | ||
| <hr /> | ||
| <BuilderComponent model={MODEL} content={content} /> | ||
| </> | ||
| ); | ||
| } |
8 changes: 8 additions & 0 deletions
8
packages/sdks/snippets/gen1-react/src/routes/targeted-page/setUserAttributes.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { builder } from '@builder.io/react'; | ||
|
|
||
| export default function setTargetingAttributes() { | ||
| builder.setUserAttributes({ | ||
| device: 'desktop', | ||
| audience: ['recent-shopper', 'womens-fashion'], | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/sdks/snippets/react/src/routes/targeted-page/custom-targeting-multi-request.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { fetchOneEntry, GetContentOptions } from '@builder.io/sdk-react'; | ||
|
|
||
| export default function targetedRequest(options: GetContentOptions) { | ||
| return fetchOneEntry({ | ||
| ...options, | ||
| userAttributes: { | ||
| audience: ['recent-shopper', 'womens-fashion'], | ||
| ...options.userAttributes, | ||
| }, | ||
| }); | ||
| } |
11 changes: 11 additions & 0 deletions
11
packages/sdks/snippets/react/src/routes/targeted-page/custom-targeting-request.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { fetchOneEntry, GetContentOptions } from '@builder.io/sdk-react'; | ||
|
|
||
| export default function targetedRequest(options: GetContentOptions) { | ||
| return fetchOneEntry({ | ||
| ...options, | ||
| userAttributes: { | ||
| audience: ['mens-fashion'], | ||
| ...options.userAttributes, | ||
| }, | ||
| }); | ||
| } |
11 changes: 11 additions & 0 deletions
11
packages/sdks/snippets/react/src/routes/targeted-page/desktop-request.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { fetchOneEntry, GetContentOptions } from '@builder.io/sdk-react'; | ||
|
|
||
| export default function targetedRequest(options: GetContentOptions) { | ||
| return fetchOneEntry({ | ||
| ...options, | ||
| userAttributes: { | ||
| device: 'desktop', | ||
| ...options.userAttributes, | ||
| }, | ||
| }); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.