|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Module dependencies. |
| 5 | + */ |
| 6 | + |
| 7 | +var defaults = require('@ndhoule/defaults'); |
| 8 | +var integration = require('@segment/analytics.js-integration'); |
| 9 | +var onBody = require('on-body'); |
| 10 | + |
| 11 | +/** |
| 12 | + * Expose `Chartbeat` integration. |
| 13 | + */ |
| 14 | + |
| 15 | +var Chartbeat = module.exports = integration('Chartbeat') |
| 16 | + .global('_sf_async_config') |
| 17 | + .global('_sf_endpt') |
| 18 | + .global('pSUPERFLY') |
| 19 | + .option('domain', '') |
| 20 | + .option('uid', null) |
| 21 | + .option('video', false) |
| 22 | + .option('sendNameAndCategoryAsTitle', false) |
| 23 | + .option('subscriberEngagementKeys', []) |
| 24 | + |
| 25 | + .tag('<script src="//static.chartbeat.com/js/{{ script }}">'); |
| 26 | + |
| 27 | +/** |
| 28 | + * Loaded? |
| 29 | + * |
| 30 | + * @api private |
| 31 | + * @return {boolean} |
| 32 | + */ |
| 33 | + |
| 34 | +Chartbeat.prototype.loaded = function() { |
| 35 | + return !!window.pSUPERFLY; |
| 36 | +}; |
| 37 | + |
| 38 | +Chartbeat.prototype.initialize = function() { |
| 39 | + this.pageCalledYet = false; |
| 40 | + this._ready = true; // temporarily switch ready to true so that a single page call can fire |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * Page. |
| 45 | + * |
| 46 | + * http://chartbeat.com/docs/handling_virtual_page_changes/ |
| 47 | + * |
| 48 | + * @api public |
| 49 | + * @param {Page} page |
| 50 | + */ |
| 51 | + |
| 52 | +Chartbeat.prototype.page = function(page) { |
| 53 | + this.updateConfig(page); |
| 54 | + |
| 55 | + // since chartbeat automatically calls a page when it loads, don't load chartbeat script until |
| 56 | + // first Segment page call comes in and configures global config vars using its props |
| 57 | + if (!this.pageCalledYet) { |
| 58 | + this._ready = false; // switch ready to false so that no pages after the first one can fire until _initialize has loaded chartbeat script |
| 59 | + this.pageCalledYet = true; |
| 60 | + this._initialize(); |
| 61 | + } else { |
| 62 | + var props = page.properties(); |
| 63 | + window.pSUPERFLY.virtualPage(props.path); |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +// update chartbeat global config vars |
| 68 | +Chartbeat.prototype.updateConfig = function(page) { |
| 69 | + var category = page.category(); |
| 70 | + var author = page.proxy('properties.author'); |
| 71 | + var props = page.properties(); |
| 72 | + |
| 73 | + // Chartbeat expects the document.title (props.title) to populate as title |
| 74 | + // This maintains legacy behavior for existing users, |
| 75 | + // defaults new users to the correct behavior, |
| 76 | + // and allows current users to opt-in to the correct behavior. |
| 77 | + // http://support.chartbeat.com/docs/#titles |
| 78 | + var title; |
| 79 | + if (this.options.sendNameAndCategoryAsTitle) { |
| 80 | + title = page.fullName() || props.title; |
| 81 | + } else { |
| 82 | + title = props.title; |
| 83 | + } |
| 84 | + |
| 85 | + // update general config |
| 86 | + window._sf_async_config = window._sf_async_config || {}; |
| 87 | + |
| 88 | + if (category) window._sf_async_config.sections = category; |
| 89 | + if (author) window._sf_async_config.authors = author; |
| 90 | + if (title) window._sf_async_config.title = title; |
| 91 | + |
| 92 | + // update subscriber engagement |
| 93 | + var _cbq = window._cbq = window._cbq || []; |
| 94 | + |
| 95 | + for (var key in props) { |
| 96 | + if (!props.hasOwnProperty(key)) continue; |
| 97 | + if (this.options.subscriberEngagementKeys.indexOf(key) > -1) { |
| 98 | + _cbq.push([key, props[key]]); |
| 99 | + } |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +// sets global vars and loads Chartbeat script |
| 104 | +Chartbeat.prototype._initialize = function() { |
| 105 | + var self = this; |
| 106 | + var script = this.options.video ? 'chartbeat_video.js' : 'chartbeat.js'; |
| 107 | + |
| 108 | + window._sf_async_config.useCanonical = true; |
| 109 | + defaults(window._sf_async_config, { |
| 110 | + domain: this.options.domain, |
| 111 | + uid: this.options.uid |
| 112 | + }); |
| 113 | + |
| 114 | + onBody(function() { |
| 115 | + window._sf_endpt = new Date().getTime(); |
| 116 | + // Note: Chartbeat depends on document.body existing so the script does |
| 117 | + // not load until that is confirmed. Otherwise it may trigger errors. |
| 118 | + self.load({ script: script }, self.ready); // switch ready to true for real once the script has loaded |
| 119 | + }); |
| 120 | +}; |
0 commit comments