Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                const symmetricsUserId = '';
const symmetricsAuthenticationKey = '';
const twitterBearerToken = '';

const symmetricsApiUrl = 'https://getsymmetrics.com/api/v1/events';
const hostnamesQuery = `
    unique urlHost
    where urlHost != localhost
    where urlHost ^!= localhost:
    limit 5
`;

async function getHostnames() {
    const response = await fetch(`${symmetricsApiUrl}?user-id=${symmetricsUserId}&authentication-key=${symmetricsAuthenticationKey}&query=${encodeURIComponent(hostnamesQuery)}`).then((response) => response.json());

    if (response.success === true) {
        return response.results;
    }

    const error = new Error(response.error.message);
    error.code = response.error.code;
    throw error;
}

const twitterApiUrl = 'https://api.twitter.com/2/tweets/search/recent';

async function searchTwitter(query) {
    const response = await fetch(`${twitterApiUrl}?query=${encodeURIComponent(query)}`, {
        headers: {authorization: `Bearer ${twitterBearerToken}`}
    }).then((response) => response.json());

    if (response.data) {
        return response.data;
    }

    const error = new Error(response.error.message);
    error.code = response.error.code;
    throw error;
}

async function getWebsiteTweets() {
    const hostnames = await getHostnames();
    const query = hostnames.map(({urlHost}) => `"https://${urlHost}"`).join(' OR ');
    return searchTwitter(query);
}

const referralsQuery = `
    where event == page_view
    where referrerHost == t.co
    where referrerBase != t.co/
    set twitterReferrals = {get results}

    unique referrerBase
    each {
        get twitterReferrals
        where referrerBase == {get each.referrerBase}
        set each.pageViews = {count}
        set each.uniqueVisitors = {count {unique userId}}
    }
`;

async function getReferrals() {
    const response = await fetch(`${symmetricsApiUrl}?user-id=${symmetricsUserId}&authentication-key=${symmetricsAuthenticationKey}&query=${encodeURIComponent(referralsQuery)}`).then((response) => response.json());

    if (response.success === true) {
        return response.results;
    }

    const error = new Error(response.error.message);
    error.code = response.error.code;
    throw error;
}

getWebsiteTweets().then(async (tweets) => {
	const referrals = await getReferrals();
	const referralsLength = referrals.length;

	return tweets.map((tweet) => {
		for (let i = 0; i < referralsLength; i += 1) {
			const referral = referrals[i];

			if (tweet.text.includes(referral.referrerBase)) {
				tweet.pageViews = referral.pageViews;
				tweet.uniqueVisitors = referral.uniqueVisitors;
				break;
			}
		}

		return tweet;
	});
}).then(console.log).catch(console.error);

console.log('Get your free Symmetrics account here: https://getsymmetrics.com');
console.log('... or read the blog post: https://getsymmetrics.com/blog/how-we-built-our-tweets-app');
              
            
!
999px

Console