Skip to content

Commit

Permalink
UJ: Merged from template.
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwieds committed Dec 15, 2023
2 parents f3cf3e3 + 1428dbe commit 59da346
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 87 deletions.
2 changes: 1 addition & 1 deletion _layouts/master/authentication/account.html
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,7 @@ <h4 class="mb-0">{name} <span class="connect-account-identity text-muted" data-p

if (accountData.plan.id !== 'basic') {
// premiumbadgeHtml += '<i class="bi-star-fill fs-2 text-primary me-1" data-bs-toggle="tooltip" data-bs-placement="top" title="' + uppercase(accountData.plan.id) + ' plan"></i>';
premiumbadgeHtml += '<i class="fa-solid fa-star fs-2 text-success me-1" data-bs-toggle="tooltip" data-bs-placement="top" title="' + uppercase(accountData.plan.id) + ' plan"></i>';
premiumbadgeHtml += '<i class="fa-solid fa-star fs-2 text-success me-1" data-bs-toggle="tooltip" data-bs-placement="top" title="' + accountData.plan.id + ' plan"></i>';
}

if (accountData.roles.admin) {
Expand Down
144 changes: 73 additions & 71 deletions _layouts/master/authentication/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ <h5 class="modal-title" id="exampleModalLabel">Oops!</h5>

<script type="text/javascript">
var select;
var storage;
var _;
var url;
var serverApiURL;
Expand All @@ -452,7 +453,6 @@ <h5 class="modal-title" id="exampleModalLabel">Oops!</h5>
var qsEmail;
var qsProvider;
var qsSignout;
var qsIsSignup;
var qsNewsletter;
var qsAffiliateCode;

Expand All @@ -464,6 +464,7 @@ <h5 class="modal-title" id="exampleModalLabel">Oops!</h5>

Manager.auth().ready(function () {
select = Manager.dom().select;
storage = Manager.storage();
_ = Manager.utilities();
var user = firebase.auth().currentUser;
url = new URL(window.location.href);
Expand All @@ -480,7 +481,6 @@ <h5 class="modal-title" id="exampleModalLabel">Oops!</h5>
qsEmail = url.searchParams.get('email') || '';
qsProvider = url.searchParams.get('provider') || '';
qsSignout = url.searchParams.get('signout') || '';
qsIsSignup = url.searchParams.get('isSignup') || '';
qsNewsletter = url.searchParams.get('newsletter') || '';
qsAffiliateCode = url.searchParams.get('affiliateCode') || '';

Expand Down Expand Up @@ -608,8 +608,10 @@ <h5 class="modal-title" id="exampleModalLabel">Oops!</h5>

firebase.auth().signInWithCustomToken(qsToken)
.then(function (r) {
trackSignupOrSignin();
redirect();
handleAuthentication()
.then(function () {
redirect();
})
})
.catch(function (e) {
display(e);
Expand Down Expand Up @@ -690,23 +692,44 @@ <h5 class="modal-title" id="exampleModalLabel">Oops!</h5>
});
})

function trackSignupOrSignin() {
var user = firebase.auth().currentUser;
function handleAuthentication() {
return new Promise(function(resolve, reject) {
var user = firebase.auth().currentUser;

if (!user || !user.uid) { return resolve(); }

var trackedSignupKey = 'trackedSignup.' + user.uid;
var trackedSignup = storage.get(trackedSignupKey, false);

if (!user || !user.uid) { return; }
var now = new Date();
var signupDate = new Date(+user.metadata.createdAt);

var trackedSignup = Manager.storage().get('trackedSignup', false);
var accountAgeSeconds = Math.floor((now - signupDate) / 1000);
var providerData = _.get(user, 'providerData', []);
var providerId = _.get(providerData, Math.max(providerData.length - 1, 0) + '.providerId', 'password');
var isSignUp = accountAgeSeconds < 30 && !trackedSignup;

var now = new Date();
var signupDate = new Date(+user.metadata.createdAt);
console.log('handleAuthentication():', providerId, accountAgeSeconds, trackedSignup, isSignUp);

var accountAgeSeconds = Math.floor((now - signupDate) / 1000);
var providerData = _.get(user, 'providerData', []);
var providerId = _.get(providerData, Math.max(providerData.length - 1, 0) + '.providerId', 'password');
// Set the trackedSignup flag
storage.set(trackedSignupKey, true);

console.log('trackSignupOrSignin():', providerId, accountAgeSeconds, trackedSignup);
// If it's not a sign up, just resolve
if (!isSignUp) {
// Google Analytics
gtag('event', 'login', {
method: providerId,
});

// Meta Pixel
// ...

// TikTok Pixel
// ...

return resolve();
}

if (accountAgeSeconds < 30 && !trackedSignup) {
// Google Analytics
gtag('event', 'sign_up', {
method: providerId,
Expand All @@ -727,21 +750,37 @@ <h5 class="modal-title" id="exampleModalLabel">Oops!</h5>
currency: 'USD',
content_type: 'product',
});
} else {
// Google Analytics
gtag('event', 'login', {
method: providerId,
});

// Meta Pixel
// ...
// Send user:signup request
user.getIdToken(false)
.then(function(token) {

// TikTok Pixel
// ...
}
fetch('https://us-central1-' + Manager.properties.options.libraries.firebase_app.config.projectId + '.cloudfunctions.net/bm_api', {
method: 'POST',
body: JSON.stringify({
authenticationToken: token,
command: 'user:sign-up',
payload: {
newsletterSignUp: select('.auth-newsletter-input').getValue(),
affiliateCode: storage.get('affiliateCode', ''),
},
}),
})
.catch(function (e) {
console.error('handleAuthentication(): Error sending user:signup request', e);
})

// Set the trackedSignup flag
Manager.storage().set('trackedSignup', true);
// We don't need to wait for the fetch to finish
setTimeout(function () {
return resolve();
}, 100);
})
.catch(function(e) {
console.error('handleAuthentication(): Error getting token', e);

return resolve();
});
});
}

function updateLinksWithEmail(email) {
Expand All @@ -755,44 +794,8 @@ <h5 class="modal-title" id="exampleModalLabel">Oops!</h5>
})
}

function signUpHandler(token) {
return new Promise(function(resolve, reject) {
console.log('signUpHandler():', token);

// If not a signup, resolve
if (qsIsSignup !== 'true') {
return resolve();
}

// Resolve automatically after 100ms
setTimeout(function () {
return resolve();
}, 100);

// Make request
fetch(serverApiURL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
authenticationToken: token,
command: 'user:sign-up',
payload: {
newsletterSignUp: qsNewsletter === 'true',
affiliateCode: qsAffiliateCode,
}
}),
})
.then(function () {
console.error('signUpHandler(): Complete');
})
.catch(function (e) {
console.error('signUpHandler(): Fail', e);
})
});
}

function redirect(url) {
var delay = Manager.storage().get('auth.redirectDelay', 0);
var delay = storage.get('auth.redirectDelay', 0);
url = url || redirectUrl;

console.log('redirect():', delay, url);
Expand Down Expand Up @@ -856,7 +859,7 @@ <h5 class="modal-title" id="exampleModalLabel">Oops!</h5>
}

// function redirect(url) {
// var delay = Manager.storage().get('auth.redirectDelay', 0);
// var delay = storage.get('auth.redirectDelay', 0);
// console.log('Redirecting with delay...', delay, url);
//
// if (delay) {
Expand Down Expand Up @@ -915,14 +918,13 @@ <h5 class="modal-title" id="exampleModalLabel">Oops!</h5>
function createCustomToken(payload) {
console.log('createCustomToken():', payload);

trackSignupOrSignin();
handleAuthentication()
.then(function () {

payload.user.getIdToken(true)
.then(function (token) {
console.log('createCustomToken(): token', token);
payload.user.getIdToken(true)
.then(function (token) {
console.log('createCustomToken(): token', token);

signUpHandler(token)
.then(function () {
fetch(serverApiURL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
"sharp": "0.23.1 (sometime before 2021ish): Hard lock because later versions had issues. Possibly solved in higher node versions"
},
"devDependencies": {
"@babel/core": "7.23.5",
"@babel/preset-env": "7.23.5",
"@babel/core": "7.23.6",
"@babel/preset-env": "7.23.6",
"autoprefixer": "9.8.8",
"browser-sync": "2.23.7",
"del": "6.1.1",
Expand Down Expand Up @@ -87,7 +87,7 @@
"through2": "4.0.2",
"ultimate-jekyll-poster": "0.0.15",
"vinyl-named": "1.1.0",
"web-manager": "3.2.22",
"web-manager": "3.2.30",
"webpack": "5.89.0",
"webpack-stream": "6.1.2",
"wonderful-fetch": "1.0.1",
Expand Down
20 changes: 8 additions & 12 deletions special/master/pages/tools/ads.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,20 @@ This is an `adsense-multiplex.html` ad:

console.log('.account().resolve()', accountData);

console.log('-----1')
account.handleAccount({
plan: {
id: 'premium',
}
})
console.log('-----2')
// account.handleAccount({
// plan: {
// id: 'premium',
// }
// })

return resolve(accountData)
return;
})
.catch(function (e) {
console.log('--- 1', e)
return reject(e);
console.error('.account().resolve()', e);
})
})
.catch(function (e) {
console.log('--- 2', e)
return reject(e);
console.error('.account().resolve()', e);
})
})
</script>

0 comments on commit 59da346

Please sign in to comment.