Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vanilla): add algolia-insights code sample #76

Merged
merged 7 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions InstantSearch.js/algolia-insights/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
3 changes: 3 additions & 0 deletions InstantSearch.js/algolia-insights/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
/dist
/.cache
3 changes: 3 additions & 0 deletions InstantSearch.js/algolia-insights/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: 'algolia',
};
22 changes: 22 additions & 0 deletions InstantSearch.js/algolia-insights/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/dist
/.cache

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
5 changes: 5 additions & 0 deletions InstantSearch.js/algolia-insights/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"proseWrap": "never",
"trailingComma": "es5"
}
19 changes: 19 additions & 0 deletions InstantSearch.js/algolia-insights/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# getting-started

_This project was generated with [create-instantsearch-app](https://github.com/algolia/create-instantsearch-app) by [Algolia](https://algolia.com)._

## Get started

To run this project locally, install the dependencies and run the local server:

```sh
npm install
npm start
```

Alternatively, you may use [Yarn](https://http://yarnpkg.com/):

```sh
yarn
yarn start
```
Binary file added InstantSearch.js/algolia-insights/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions InstantSearch.js/algolia-insights/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#000000" />

<link rel="manifest" href="./manifest.webmanifest" />
<link rel="shortcut icon" href="./favicon.png" />

<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/instantsearch.css@7/themes/algolia-min.css"
/>
<link rel="stylesheet" href="./src/index.css" />
<link rel="stylesheet" href="./src/app.css" />

<title>ais-ecommerce-demo-app</title>
</head>

<body>
<div class="ais-InstantSearch">
<h1>InstantSearch.js e-commerce demo</h1>

<div class="left-panel">
<div id="clear-refinements"></div>

<h2>Brands</h2>
<div id="brand-list"></div>
</div>

<div class="right-panel">
<div id="searchbox" class="ais-SearchBox"></div>
<div id="hits"></div>
<div id="pagination"></div>
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/algoliasearch@3.32.0/dist/algoliasearchLite.min.js"></script>
tkrugg marked this conversation as resolved.
Show resolved Hide resolved
<script src="https://cdn.jsdelivr.net/npm/instantsearch.js@3.0.0"></script>
tkrugg marked this conversation as resolved.
Show resolved Hide resolved
<script src="./src/app.js"></script>
<script>
!function(e,a,t,n,s,i,c){e.AlgoliaAnalyticsObject=s,e.aa=e.aa||function(){(e.aa.queue=e.aa.queue||[]).push(arguments)},i=a.createElement(t),c=a.getElementsByTagName(t)[0],i.async=1,i.src="https://cdn.jsdelivr.net/npm/search-insights@1.0.1",c.parentNode.insertBefore(i,c)}(window,document,"script",0,"aa");
aa('init', {
appId: 'B1G2GM9NG0',
apiKey: 'aadef574be1f9252bb48d4ea09b5cfe5',
});
</script>

</body>
</html>
15 changes: 15 additions & 0 deletions InstantSearch.js/algolia-insights/manifest.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "is-ecommerce-demo-app",
"name": "is-ecommerce-demo-app Sample",
"icons": [
{
"src": "favicon.png",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
21 changes: 21 additions & 0 deletions InstantSearch.js/algolia-insights/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "ais-ecommerce-demo-app",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "parcel index.html --port 3000",
"build": "parcel build index.html",
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix"
},
"devDependencies": {
"babel-eslint": "10.0.1",
"eslint": "4.19.1",
"eslint-config-algolia": "13.2.3",
"eslint-config-prettier": "4.1.0",
"eslint-plugin-import": "2.16.0",
"eslint-plugin-prettier": "3.0.1",
"parcel-bundler": "1.12.2",
"prettier": "1.16.4"
}
}
50 changes: 50 additions & 0 deletions InstantSearch.js/algolia-insights/src/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
body {
font-family: sans-serif;
padding: 1em;
}

.ais-ClearRefinements {
margin: 1em 0;
}

.ais-SearchBox {
margin: 1em 0;
}

.ais-Pagination {
margin-top: 1em;
}

.left-panel {
float: left;
width: 250px;
}

.right-panel {
margin-left: 260px;
}

.ais-InstantSearch {
max-width: 960px;
overflow: hidden;
margin: 0 auto;
}

.ais-Hits-item {
margin-bottom: 1em;
width: calc(50% - 1rem);
}

.ais-Hits-item img {
margin-right: 1em;
}

.hit-name {
margin-bottom: 0.5em;
}

.hit-description {
color: #888;
font-size: 14px;
margin-bottom: 0.5em;
}
98 changes: 98 additions & 0 deletions InstantSearch.js/algolia-insights/src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* global instantsearch algoliasearch */

const search = instantsearch({
indexName: 'demo_ecommerce',
searchClient: algoliasearch('B1G2GM9NG0', 'aadef574be1f9252bb48d4ea09b5cfe5'),
searchParameters: {
clickAnalytics: true,
},
});

search.addWidget(
instantsearch.widgets.searchBox({
container: '#searchbox',
})
);

search.addWidget(
instantsearch.widgets.clearRefinements({
container: '#clear-refinements',
})
);

search.addWidget(
instantsearch.widgets.refinementList({
container: '#brand-list',
attribute: 'brand',
})
);

search.addWidget(
instantsearch.widgets.hits({
container: '#hits',
templates: {
item: hit => `
<div>
<img src="${hit.image}" align="left" alt="${hit.name}" />
<div class="hit-name">
${hit.name}
</div>
<div>
<button class="button-click"
data-algolia-objectid="${hit.objectID}"
data-algolia-position="${hit.hitPosition}"
data-algolia-queryid="${hit.queryID}"
>
Click event
</button>
<button class="button-conversion"
data-algolia-objectid="${hit.objectID}"
data-algolia-queryid="${hit.queryID}"
>
Conversion event
</button>
</div>
</div>
`,
},
transformItems(items) {
const result = search.helper.lastResults;
const offset = result.hitsPerPage * result.page;

return items.map((item, index) => ({
...item,
name: item.name.toUpperCase(),
queryID: result.queryID,
hitPosition: offset + index + 1,
Haroenv marked this conversation as resolved.
Show resolved Hide resolved
}));
},
})
);

search.addWidget(
instantsearch.widgets.pagination({
container: '#pagination',
})
);

search.start();

document.getElementById('hits').addEventListener('click', e => {
if (e.target.matches('.button-click')) {
window.aa('clickedObjectIDsAfterSearch', {
index: 'demo_ecommerce',
eventName: 'click-result',
objectIDs: [e.target.getAttribute('data-algolia-objectid')],
queryID: e.target.getAttribute('data-algolia-queryid'),
// parseInt because getAttribute always returns a string
tkrugg marked this conversation as resolved.
Show resolved Hide resolved
positions: [parseInt(e.target.getAttribute('data-algolia-position'), 10)],
});
} else if (e.target.matches('.button-conversion')) {
aa('convertedObjectIDsAfterSearch', {
index: 'demo_ecommerce',
eventName: 'conversion',
queryID: e.target.getAttribute('data-algolia-queryid'),
objectIDs: [e.target.getAttribute('data-algolia-objectid')],
});
}
});
10 changes: 10 additions & 0 deletions InstantSearch.js/algolia-insights/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
body,
h1 {
margin: 0;
padding: 0;
}

body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica,
Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
}
Loading