-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
abc9084
commit 8f9a7bb
Showing
5 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,31 @@ | ||
import Service from '@ember/service'; | ||
import { inject as service } from '@ember/service'; | ||
|
||
export default class UuidService extends Service { | ||
@service fastboot; | ||
|
||
// Generates UUID v4 | ||
// Inspired from: | ||
// https://stackoverflow.com/questions/105034/how-do-i-create-a-guid-uuid | ||
generate() { | ||
let d = new Date().getTime(); | ||
let d2 = | ||
(typeof performance !== 'undefined' && | ||
performance.now && | ||
performance.now() * 1000) || | ||
0; | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { | ||
let r = Math.random() * 16; | ||
|
||
if (d > 0) { | ||
r = (d + r) % 16 | 0; | ||
d = Math.floor(d / 16); | ||
} else { | ||
r = (d2 + r) % 16 | 0; | ||
d2 = Math.floor(d2 / 16); | ||
} | ||
|
||
return (c == 'x' ? r : (r & 0x7) | 0x8).toString(16); | ||
}); | ||
} | ||
} |
This file contains 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,38 @@ | ||
import Service from '@ember/service'; | ||
import { inject as service } from '@ember/service'; | ||
import { tracked } from '@glimmer/tracking'; | ||
|
||
export default class VisitService extends Service { | ||
@service fastboot; | ||
@service uuid; | ||
|
||
// A unique UUID given to this website session. It will: | ||
// * be remembered across all Ember routes. | ||
// * added to all requests coming from Ember. | ||
// * help group requests for analytics | ||
@tracked id; | ||
|
||
create() { | ||
if (this.fastboot.isFastBoot) { | ||
// 1. Fastboot creates a unique UUID for this visit. | ||
this.id = this.uuid.generate(); | ||
|
||
// 2. Fastboot passes this ID down to Ember. | ||
this.fastboot.shoebox.put('visit', this.id); | ||
console.log('🧩 fastboot created visit', this.id); | ||
} else { | ||
// 3. Ember retrieves the ID passed down from Fastboot. | ||
const id = this.fastboot.shoebox.retrieve('visit'); | ||
|
||
if (id) { | ||
// 4. Ember remembers ID for all requests going forward. | ||
this.id = id; | ||
console.log('🧩 ember found ID in shoebox', this.id); | ||
} else { | ||
// 5. If anything went wrong with Fastboot, create a UUID with Ember. | ||
this.id = this.uuid.generate(); | ||
console.log('🧩 ember created visit 🔥🔥🔥', this.id); | ||
} | ||
} | ||
} | ||
} |