Skip to content

Commit c55ed49

Browse files
committed
fix:updating the app
1 parent 71671cf commit c55ed49

File tree

4 files changed

+164
-24
lines changed

4 files changed

+164
-24
lines changed

README.md

+59-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,69 @@
11
# Assets Usage Tracking
22

3-
This project is a sample application that demonstrates how to use the Adobe Experience Platform SDK to track assets in a web application. The application is built using the Adobe I/O App Builder and uses the Adobe I/O Runtime to host the application and the Adobe Experience Platform SDK to track assets.
3+
This project is a sample application that demonstrates how to use the Adobe Experience Platform to track assets usage in a helix site.
4+
The application provides a report of all the assets used in the site along with important information such as
5+
- List of all the assets used in the site
6+
- If any expired asset is being used
7+
- If there are mismatch between the asset tag and tag applied on the page
8+
- How many times the asset is used in the site
9+
- On which pages the asset is used
10+
- Top Used Asset
411

5-
## Local Run
12+
The application is built using the Adobe I/O App Builder and uses the Adobe I/O Runtime to host the application and the Adobe Experience Platform SDK to track assets.
13+
14+
## Accessing the Tool
15+
16+
The tool can be accessed via the following URL:
17+
18+
https://experience.adobe.com/?devMode=true#/custom-apps/?localDevUrl=https://288650-edsassettracker.adobeio-static.net/index.html?hlxUrl=https://demo-custom-as-config--franklin-assets-selector--hlxsites.hlx.live
19+
20+
where hlxUrl is the URL of the site for which you want to track assets.
621

22+
## prerequisites
23+
To Track Usual Assets Usage, you need to have the following indexes created in your Helix Sites project:
24+
- `assets` index
25+
This will provide the list of all the assets used in the site along with their url and source
26+
category. Such as Adobe Assets, Dynamic Media Assets, Scene7 Assets etc.
27+
Sample Index Configuration:
28+
```yaml
29+
assets-index:
30+
target: /assets-index.json
31+
properties:
32+
polaris-assets:
33+
select: a
34+
values: |
35+
match(attribute(el, 'href'), 'https:\/\/[^\/]+\/adobe\/assets\/urn:aaid:aem:.*')
36+
dm-next-assets:
37+
select: a
38+
values: |
39+
match(attribute(el, 'href'), 'https:\/\/[^\/]+\/adobe\/dynamicmedia\/deliver\/urn:aaid:aem:.*')
40+
scene7-assets:
41+
select: a
42+
values: |
43+
match(attribute(el, 'href'), 'https:\/\/.*scene7.com\/is\/image\/.*')
44+
```
45+
46+
- `query` index
47+
This is in general the default index in the Helix Sites project which provides the list of all the pages in the site.
48+
This index needs to be enabled with metadata property **tags** to provide the information tags applied on the page.
49+
Sample Index Configuration:
50+
```yaml
51+
site:
52+
target: /query-index.json
53+
properties:
54+
tags:
55+
select: head > meta[property="article:tag"]
56+
values: |
57+
attribute(el, 'content')
58+
```
59+
60+
Complete sample yaml configuration can be found [here](https://github.com/hlxsites/franklin-assets-selector/blob/main/helix-query.yaml)
61+
## Local Run
762
- Run `aio app run` to start the application
863
- Open your browser to `http://localhost:9080` to see the app running locally and it will redirect to assets usage report page.
964
- pass `hlxUrl` query parameter to see the assets usage report for the given hlxUrl for example
10-
-> https://localhost:9080/assetsUsageReport.html?hlxUrl=https://demo-custom-as-config--franklin-assets-selector--hlxsites.hlx.live/
65+
-> https://localhost:9080/index.html?hlxUrl=https://demo-custom-as-config--franklin-assets-selector--hlxsites.hlx.live/
1166
- To view your deployed application in the Experience Cloud shell:
1267
-> https://experience.adobe.com/?devMode=true#/custom-apps/?localDevUrl=https://localhost:9080
1368

69+

web-src/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ <h1 class="assets-title">Assets Usage Report</h1>
4848
</div>
4949
</div>
5050
</div>
51-
<script type='module' src="src/assetsData.js"></script>
51+
<!-- <script type='module' src="src/assetsData.js"></script>-->
5252
</div>
5353
</body>
5454
</html>

web-src/src/assetsData.js

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { PublicClientApplication } from './msal-browser-2.14.2.js';
22
import excApp, { init } from '@adobe/exc-app';
33

4-
import { getState } from './index.js';
4+
// import { getState } from './index.js';
55

66
var defaultThumbnail = require('../no-image.png');
77
let accessToken;
88

9-
(async () => {
9+
async function assetsDataInit() {
1010

11+
console.log('I am here3 ');
1112
// Create mask and spinner elements
1213
const mask = document.createElement('div');
1314
mask.className = 'mask';
@@ -22,21 +23,21 @@ let accessToken;
2223

2324
const queryParams = new URLSearchParams(window.location.search);
2425
const pagePath = queryParams.get('pagePath');
25-
let hlxUrl;
26-
if (pagePath) {
27-
hlxUrl = sessionStorage.getItem('hlxUrl');
28-
} else {
29-
hlxUrl = queryParams.get('hlxUrl');
30-
sessionStorage.setItem('hlxUrl', hlxUrl);
31-
}
26+
const hlxUrl = sessionStorage.getItem('hlxUrl');
27+
// if (pagePath) {
28+
// hlxUrl = sessionStorage.getItem('hlxUrl');
29+
// } else {
30+
// hlxUrl = queryParams.get('hlxUrl');
31+
// sessionStorage.setItem('hlxUrl', hlxUrl);
32+
// }
3233

3334
let data = {};
3435
async function connectAndFetchData() {
3536
accessToken = sessionStorage.getItem('accessToken');
3637
if (!accessToken) {
3738
try {
38-
let state = await getState();
39-
accessToken = state.imsToken;
39+
// let state = await getState();
40+
// accessToken = state.imsToken;
4041

4142
} catch (error) {
4243
console.log(error);
@@ -46,7 +47,7 @@ let accessToken;
4647

4748
// Proceed if we have a valid access token
4849
if (accessToken) {
49-
sessionStorage.setItem('accessToken', accessToken);
50+
// sessionStorage.setItem('accessToken', accessToken);
5051
try {
5152
const response = await fetch(
5253
`https://288650-edsassettracker.adobeio-static.net/api/v1/web/EDS-Asset-Tracker1/fetchList?hlxUrl=${hlxUrl}`,
@@ -360,4 +361,6 @@ let accessToken;
360361

361362
}
362363

363-
})();
364+
}
365+
366+
export default assetsDataInit;

web-src/src/index.js

+88-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import regeneratorRuntime from 'regenerator-runtime'
66
import Runtime, { init } from '@adobe/exc-app'
77
import actions from './config.json'
88
import actionWebInvoke from './utils.js'
9+
import assetsDataInit from "./assetsData.js";
10+
import { PublicClientApplication } from './msal-browser-2.14.2.js';
911

1012
let state = {}
1113

@@ -91,22 +93,102 @@ let state = {}
9193

9294

9395

94-
window.onload = () => {
96+
window.onload = async () => {
9597
/* Here you can bootstrap your application and configure the integration with the Adobe Experience Cloud Shell */
9698
try {
9799
// attempt to load the Experience Cloud Runtime
98100
require('./exc-runtime')
99101
// if there are no errors, bootstrap the app in the Experience Cloud Shell
100102
init(initRuntime)
103+
104+
let state1 = await getState();
105+
106+
const queryParams = new URLSearchParams(window.location.search);
107+
const hlxUrl = queryParams.get('hlxUrl');
108+
sessionStorage.setItem('hlxUrl', hlxUrl);
109+
sessionStorage.setItem('accessToken', state1.imsToken);
110+
await assetsDataInit();
101111
} catch (e) {
102112
console.log('application not running in Adobe Experience Cloud Shell')
103113
// fallback mode, run the application without the Experience Cloud Runtime
114+
let accessToken = sessionStorage.getItem('accessToken');
115+
if (!accessToken) {
116+
accessToken = await fetchAccessToken();
117+
console.log(accessToken);
118+
sessionStorage.setItem('accessToken', accessToken);
119+
} else {
120+
console.log('Access token found in session storage');
121+
console.log(accessToken);
122+
123+
}
124+
// console.log(accessToken);
125+
// state1.imsToken = accessToken;
126+
let hlxUrl = sessionStorage.getItem('hlxUrl');
127+
if (!hlxUrl) {
128+
const queryParams = new URLSearchParams(window.location.search);
129+
hlxUrl = queryParams.get('hlxUrl');
130+
sessionStorage.setItem('hlxUrl', hlxUrl);
131+
}
132+
await assetsDataInit();
104133
}
105134

106-
showActionsList()
107-
document.getElementById('actionForm').onsubmit = (event) => {
108-
event.preventDefault()
109-
setTimeout(doSubmit, 1)
135+
// await assetsDataInit(state);
136+
137+
}
138+
139+
async function fetchAccessToken () {
140+
const sp = {
141+
clientApp: {
142+
auth: {
143+
clientId: '2b4aa217-ddcd-4fe0-b09c-5a472764f552',
144+
authority: 'https://login.microsoftonline.com/fa7b1b5a-7b34-4387-94ae-d2c178decee1',
145+
},
146+
},
147+
login: {
148+
redirectUri: '/spauth.html',
149+
},
150+
};
151+
152+
let accessToken;
153+
const publicClientApplication = new PublicClientApplication(sp.clientApp);
154+
const accounts = publicClientApplication.getAllAccounts();
155+
156+
if (accounts.length === 0) {
157+
// User is not logged in, show the login popup
158+
await publicClientApplication.loginPopup(sp.login);
159+
160+
}
161+
162+
const account = publicClientApplication.getAllAccounts()[0];
163+
const accessTokenRequest = {
164+
scopes: ['files.readwrite', 'sites.readwrite.all'],
165+
account,
166+
};
167+
168+
try {
169+
const res = await publicClientApplication.acquireTokenSilent(accessTokenRequest);
170+
accessToken = res.accessToken;
171+
return accessToken;
172+
} catch (error) {
173+
// Acquire token silent failure, and send an interactive request
174+
if (error.name === 'InteractionRequiredAuthError') {
175+
try {
176+
const res = await publicClientApplication.acquireTokenPopup(accessTokenRequest);
177+
accessToken = res.accessToken;
178+
console.log(accessToken);
179+
return accessToken;
180+
} catch (err) {
181+
console.error(`Cannot connect to SharePoint: ${err.message}`);
182+
document.body.removeChild(mask);
183+
document.querySelector('.assets-usage-report').style.display = 'block';
184+
return null; // Exit if token acquisition fails
185+
}
186+
} else {
187+
console.error('Error acquiring token silently:', error.message);
188+
document.body.removeChild(mask);
189+
document.querySelector('.assets-usage-report').style.display = 'block';
190+
return null;
191+
}
110192
}
111193
}
112194

@@ -203,8 +285,7 @@ async function invokeAction (action, _headers, _params) {
203285
const result = await actionWebInvoke(action[1], headers, params)
204286
return result
205287
}
206-
207-
export async function getState() {
288+
async function getState() {
208289
console.log('Getting state');
209290
let attempts = 0;
210291
while (!state.imsToken) {

0 commit comments

Comments
 (0)