Skip to content

Instantly share code, notes, and snippets.

@leizmonk
Last active June 6, 2017 16:20
Show Gist options
  • Save leizmonk/323cb6948d3640997bd2f05ebeb02ae0 to your computer and use it in GitHub Desktop.
Save leizmonk/323cb6948d3640997bd2f05ebeb02ae0 to your computer and use it in GitHub Desktop.
LT category browse
// In Petflow/Services/Pages/LandingPageLocator.php <- platform code that returns the listing
public function cmsPage($slug) : array
{
$all_brands = [];
$brands = array_map(function($iteration) use (&$all_brands) {
$all_brands = array_merge($all_brands, $iteration);
}, $this->pages->availableBrands());
$ret = [
'products' => [],
'brands' => $all_brands,
'aggregations' => [],
'beacons' => [$this->beacons->buildPage('category', [$slug])]
];
$page = CmsPage::with(['categories', 'products', 'banner', 'emailCaptures', 'sliderImages'])
->where('slug', 'ilike', $slug)
->firstOrFail();
$query = parse_url(html_entity_decode($page->url), PHP_URL_QUERY);
parse_str($query); // "magically" defines facet array as $f
if (!isset($f)) {
throw new Exception('No facets present');
}
$options = [
'facets' => $f,
'limit' => 12
];
if (isset($search)) {
$options['search'] = $search;
}
try {
$search = $this->products->search(isset($search) ? $search : '*', $options);
$ret['products'] = $search->getData();
$ret['aggregations'] = $search->getAggregations();
$ret['page'] = $page;
$ret['featured_products'] = [];
if ($page->products) {
$ret['featured_products'] = $this->products->getEsProducts($page->products->pluck('product_id')->all());
}
$products = array_map(function ($product) { return Product::fromEs($product); }, $ret['products']);
$products = new ProductCollection($products);
$listing = $this->beacons->buildListingFromProducts($search->getTotalHits(), "CMS LP - {$page->page_title}", $products, '', 'rank', 'grid');
if ($listing->items->first()) {
$listing->category = $listing->items->first()->category;
$listing->subcategory = $listing->items->first()->subcategory;
}
$ret['beacons'][] = $listing;
} catch (Exception $e) {
$this->logger->error($e);
}
return $ret;
}
// In /js/modules/pf_pixels.js - this file handles the behavior of all pixels on the site
self.onCategoryViewed = function (listing) {
_fire(function () {
custora.categoryView(listing.category, listing.subcategory, listing.brand);
criteo.viewList(extract(listing.items, 'id'), listing.query);
moengage.setCategoryViewed(listing.items[0].category);
moengage.setSpeciesViewed(listing.items[0].species);
moengage.sendViewedCategory(listing);
var url = window.location.host window.location.pathname;
listrak.categoryBrowse(url, listing.category);
});
};
// In /js/modules/pixels/listrak.js - specific to LT pixel
self.productBrowse = function (skuCode) {
listrakWrap(function () {
window._ltk.Activity.AddProductBrowse(skuCode);
window._ltk.Activity.Submit();
});
};
self.categoryBrowse = function (url, category) {
listrakWrap(function () {
window._ltk.Activity.AddPageBrowse(url, {category: category});
window._ltk.Activity.Submit();
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment