Skip to content

Commit

Permalink
created new is identity available function
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleysmithTTD committed Dec 12, 2024
1 parent c86dfb9 commit 4869277
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions examples/cstg/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
$('#targeted_advertising_ready').text(sdk.getAdvertisingToken() ? 'yes' : 'no');
$('#advertising_token').text(String(sdk.getAdvertisingToken()));
$('#login_required').text(
sdk.noIdentityAvailable() || sdk.noIdentityAvailable() === undefined ? 'yes' : 'no'
sdk.isLoginRequired() || sdk.isLoginRequired() === undefined ? 'yes' : 'no'
);
$(`#has_opted_out`).text(sdk.hasOptedOut() ? 'yes' : 'no');
$('#update_counter').text(callbackCounter);
Expand All @@ -37,7 +37,7 @@
}

function updateSharedGuiElements() {
if (getUidSdk().noIdentityAvailable()) {
if (getUidSdk().isLoginRequired()) {
$('#login_form').show();
$('#logout_form').hide();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ The following table outlines and annotates the steps you can take to test and ex
| 3 | Click the **Back to the main page** link. | On the updated application main page, note the newly populated **UID2 Advertising Token** value and a video player. While the [page view](views/index.html) is loading, [GPT](https://developers.google.com/publisher-tag/reference#googletag) auto-loads the Secure Signal UID2 script which pushes the advertising token to GPT local storage, and the [IMA](https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side) makes an ad request which transmits the encoded signal in the request. The [page view](views/index.html) calls the [init()](https://unifiedid.com/docs/sdks/client-side-identity#initopts-object-void) function again, but this time without passing an explicit identity. Instead, the identity is loaded from the first-party cookie. |
| 4 | Click **Play**. | This triggers AdsManager to insert the ad returned from the ad request, for display. The ad tag used in this example contains a 10-second pre-roll ad. |
| 5 | Keep the application main page open, or refresh it after a while, and note the UID2 identity state, updated counter, and login information values. | In the background, the UID2 SDK continuously validates whether the advertising token is up to date, and refreshes it automatically when needed. If the refresh succeeds, the user opts out, or the refresh token expires, the callback function is invoked, and the UI elements are updated with the current state of the UID2 identity. For details, see [Workflow Overview](https://unifiedid.com/docs/sdks/client-side-identity#workflow-overview) and [Background Token Auto-Refresh](https://unifiedid.com/docs/sdks/client-side-identity#background-token-auto-refresh). |
| 6 | To exit the application, click **Log Out**. | This calls the UID2 SDK [`disconnect()`](https://unifiedid.com/docs/sdks/client-side-identity#disconnect-void) function, which clears the UID2 session and the first-party cookie and calls the Secure Signal [`clearAllCache()`](https://developers.google.com/publisher-tag/reference#googletag.secureSignals.SecureSignalProvidersArray_clearAllCache) function to clear all cached signals. This call also makes the UID2 SDK [`noIdentityAvailable()`](https://unifiedid.com/docs/sdks/client-side-identity#noIdentityAvailable-boolean) function return `true`, which presents the user with the login form again.<br/> NOTE: The page displays the **Log Out** button as long as the user identity is valid and refreshable. |
| 6 | To exit the application, click **Log Out**. | This calls the UID2 SDK [`disconnect()`](https://unifiedid.com/docs/sdks/client-side-identity#disconnect-void) function, which clears the UID2 session and the first-party cookie and calls the Secure Signal [`clearAllCache()`](https://developers.google.com/publisher-tag/reference#googletag.secureSignals.SecureSignalProvidersArray_clearAllCache) function to clear all cached signals. This call also makes the UID2 SDK [`isLoginRequired()`](https://unifiedid.com/docs/sdks/client-side-identity#isLoginRequired-boolean) function return `true`, which presents the user with the login form again.<br/> NOTE: The page displays the **Log Out** button as long as the user identity is valid and refreshable. |
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
__uid2.getAdvertisingToken() ? "yes" : "no"
);
$("#advertising_token").html(String(__uid2.getAdvertisingToken()));
$("#login_required").html(__uid2.noIdentityAvailable() ? "yes" : "no");
$("#login_required").html(__uid2.isLoginRequired() ? "yes" : "no");
$("#update_counter").html(callbackCounter);
$("#identity_state").html(String(JSON.stringify(payload, null, 2)));

if (__uid2.noIdentityAvailable()) {
if (__uid2.isLoginRequired()) {
$("#login_form").show();
$("#logout_form").hide();
$('#googleAdContainer').hide();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
});
}
if (eventType === 'InitCompleted') {
if (__uid2.noIdentityAvailable()) __uid2.setIdentity(<%- JSON.stringify(identity) %>)
if (__uid2.isLoginRequired()) __uid2.setIdentity(<%- JSON.stringify(identity) %>)
}
});
</script>
Expand Down
16 changes: 8 additions & 8 deletions setupJest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,47 @@ expect.extend({
expect(uid2.getAdvertisingToken()).toBeNonEmptyString();
}

expect(uid2.noIdentityAvailable()).toEqual(false);
expect(uid2.isLoginRequired()).toEqual(false);

return {
pass: true,
message: () =>
'Expected getAdvertisingToken() returns a token and noIdentityAvailable() returns false',
'Expected getAdvertisingToken() returns a token and isLoginRequired() returns false',
};
},

toBeInTemporarilyUnavailableState(uid2) {
expect(uid2.getAdvertisingToken()).toBeUndefined();
expect(uid2.noIdentityAvailable()).toEqual(false);
expect(uid2.isLoginRequired()).toEqual(false);

return {
pass: true,
message: () =>
'Expected getAdvertisingToken() returns undefined and noIdentityAvailable() returns false',
'Expected getAdvertisingToken() returns undefined and isLoginRequired() returns false',
};
},

toBeInUnavailableState(uid2) {
expect(uid2.getAdvertisingToken()).toBeUndefined();
expect(uid2.noIdentityAvailable()).toEqual(true);
expect(uid2.isLoginRequired()).toEqual(true);
expect(uid2.hasOptedOut()).toEqual(false);

return {
pass: true,
message: () =>
'Expected getAdvertisingToken() returns undefined and noIdentityAvailable() returns true',
'Expected getAdvertisingToken() returns undefined and isLoginRequired() returns true',
};
},

toBeInOptoutState(uid2) {
expect(uid2.getAdvertisingToken()).toBeUndefined();
expect(uid2.noIdentityAvailable()).toEqual(false);
expect(uid2.isLoginRequired()).toEqual(false);
expect(uid2.hasOptedOut()).toEqual(true);

return {
pass: true,
message: () =>
'Expected getAdvertisingToken() returns undefined and noIdentityAvailable() returns false',
'Expected getAdvertisingToken() returns undefined and isLoginRequired() returns false',
};
},
});
Expand Down
23 changes: 14 additions & 9 deletions src/sdkBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,20 @@ export abstract class SdkBase {
return this._initComplete;
}

/**
* @deprecated in version 3.9.0. Use noIdentityAvailable() instead
*/
public isLoginRequired() {
return this.noIdentityAvailable();
return !this.isIdentityAvailable();
}

public noIdentityAvailable() {
/**
* @deprecated in version 3.10.0. Will remove in June 2025. Use isIdentityAvailable() instead.
**/
public hasIdentity() {
if (!this._initComplete) return undefined;
return !(this.isLoggedIn() || this._apiClient?.hasActiveRequests());
return !(this.isIdentityValid() || this._apiClient?.hasActiveRequests());
}

public isIdentityAvailable() {
return this.isIdentityValid() || this._apiClient?.hasActiveRequests();
}

public hasOptedOut() {
Expand Down Expand Up @@ -295,8 +299,9 @@ export abstract class SdkBase {
if (this.hasOptedOut()) this._callbackManager.runCallbacks(EventType.OptoutReceived, {});
}

private isLoggedIn() {
return this._identity && !hasExpired(this._identity.refresh_expires);
private isIdentityValid() {
const identity = this._identity ?? this.getIdentityNoInit();
return identity && !hasExpired(identity.refresh_expires);
}

private temporarilyUnavailable(identity: Identity | OptoutIdentity | null | undefined) {
Expand Down Expand Up @@ -439,7 +444,7 @@ export abstract class SdkBase {
clearTimeout(this._refreshTimerId);
}
this._refreshTimerId = setTimeout(() => {
if (this.noIdentityAvailable()) return;
if (this.isLoginRequired()) return;
const validatedIdentity = this.validateAndSetIdentity(
this._storageManager?.loadIdentity() ?? null
);
Expand Down

0 comments on commit 4869277

Please sign in to comment.