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

Enhance Dynamic Content #5360

Merged
merged 2 commits into from
Dec 19, 2017
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
5 changes: 5 additions & 0 deletions app/bundles/CoreBundle/EventListener/BuildJsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ function s4() {
MauticJS.onFirstEventDelivery = function(f) {
MauticJS.postEventDeliveryQueue.push(f);
};
MauticJS.preEventDeliveryQueue = [];
MauticJS.beforeFirstDeliveryMade = false;
MauticJS.beforeFirstEventDelivery = function(f) {
MauticJS.preEventDeliveryQueue.push(f);
};
document.addEventListener('mauticPageEventDelivered', function(e) {
var detail = e.detail;
var isImage = detail.image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
namespace Mautic\DynamicContentBundle\Controller;

use Mautic\CoreBundle\Controller\CommonController;
use Mautic\DynamicContentBundle\Entity\DynamicContent;
use Mautic\DynamicContentBundle\Helper\DynamicContentHelper;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\LeadModel;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

Expand All @@ -37,12 +40,24 @@ public function processAction($objectAlias)
}
public function getAction($objectAlias)
{
$lead = $this->getModel('lead')->getCurrentLead();
$content = $this->get('mautic.helper.dynamicContent')->getDynamicContentForLead($objectAlias, $lead);
/** @var LeadModel $model */
$model = $this->getModel('lead');
/** @var DynamicContentHelper $helper */
$helper = $this->get('mautic.helper.dynamicContent');
/** @var Lead $lead */
$lead = $model->getContactFromRequest();
$content = $helper->getDynamicContentForLead($objectAlias, $lead);

if (empty($content)) {
$content = $this->get('mautic.helper.dynamicContent')->getDynamicContentSlotForLead($objectAlias, $lead);
$content = $helper->getDynamicContentSlotForLead($objectAlias, $lead);
}

return empty($content) ? new Response('', Response::HTTP_NO_CONTENT) : new Response($content);
return empty($content)
? new Response('', Response::HTTP_NO_CONTENT)
: new JsonResponse([
'content' => $content,
'id' => $lead->getId(),
'sid' => $model->getTrackingCookie()[0],
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public function onBuildJs(BuildJsEvent $event)
'submittingMessage': "{$this->translator->trans('mautic.form.submission.pleasewait')}"
};
}
MauticJS.replaceDynamicContent = function () {
MauticJS.replaceDynamicContent = function (params) {
params = params || {};

var dynamicContentSlots = document.querySelectorAll('.mautic-slot, [data-slot="dwc"]');
if (dynamicContentSlots.length) {
MauticJS.iterateCollection(dynamicContentSlots)(function(node, i) {
Expand All @@ -92,11 +94,18 @@ public function onBuildJs(BuildJsEvent $event)
return;
}
var url = '{$dwcUrl}'.replace('slotNamePlaceholder', slotName);
MauticJS.makeCORSRequest('GET', url, {}, function(response, xhr) {
if (response.length) {
node.innerHTML = response;

MauticJS.makeCORSRequest('GET', url, params, function(response, xhr) {
if (response.content) {
var dwcContent = response.content;
node.innerHTML = dwcContent;

if (response.id && response.sid) {
MauticJS.setTrackedContact(response);
}

// form load library
if (response.search("mauticform_wrapper") > 0) {
if (dwcContent.search("mauticform_wrapper") > 0) {
// if doesn't exist
if (typeof MauticSDK == 'undefined') {
MauticJS.insertScript('{$this->assetsHelper->getUrl('media/js/mautic-form.js', null, null, true)}');
Expand All @@ -116,13 +125,13 @@ public function onBuildJs(BuildJsEvent $event)
var m;
var regEx = /<script[^>]+src="?([^"\s]+)"?\s/g;

while (m = regEx.exec(response)) {
while (m = regEx.exec(dwcContent)) {
if ((m[1]).search("/focus/") > 0) {
MauticJS.insertScript(m[1]);
}
}

if (response.search("fr-gatedvideo") > 0) {
if (dwcContent.search("fr-gatedvideo") > 0) {
MauticJS.initGatedVideo();
}
}
Expand All @@ -131,7 +140,7 @@ public function onBuildJs(BuildJsEvent $event)
}
};

MauticJS.onFirstEventDelivery(MauticJS.replaceDynamicContent);
MauticJS.beforeFirstEventDelivery(MauticJS.replaceDynamicContent);
JS;
$event->appendJs($js, 'Mautic Dynamic Content');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,34 @@ public function onCampaignBuild(CampaignBuilderEvent $event)

/**
* @param CampaignExecutionEvent $event
*
* @return bool|CampaignExecutionEvent
*/
public function onCampaignTriggerDecision(CampaignExecutionEvent $event)
{
$eventConfig = $event->getConfig();
$eventDetails = $event->getEventDetails();
$lead = $event->getLead();

if ($eventConfig['dwc_slot_name'] === $eventDetails) {
$defaultDwc = $this->dynamicContentModel->getRepository()->getEntity($eventConfig['dynamicContent']);

if ($defaultDwc instanceof DynamicContent) {
// Set the default content in case none of the actions return data
$this->dynamicContentModel->setSlotContentForLead($defaultDwc, $lead, $eventDetails);
}
// stop
if ($eventConfig['dwc_slot_name'] !== $eventDetails) {
$event->setResult(false);

$this->session->set('dwc.slot_name.lead.'.$lead->getId(), $eventDetails);
return false;
}

$event->stopPropagation();
$defaultDwc = $this->dynamicContentModel->getRepository()->getEntity($eventConfig['dynamicContent']);

return $event->setResult(true);
if ($defaultDwc instanceof DynamicContent) {
// Set the default content in case none of the actions return data
$this->dynamicContentModel->setSlotContentForLead($defaultDwc, $lead, $eventDetails);
}

$this->session->set('dwc.slot_name.lead.'.$lead->getId(), $eventDetails);

$event->stopPropagation();

return $event->setResult(true);
}

/**
Expand Down
16 changes: 14 additions & 2 deletions app/bundles/PageBundle/EventListener/BuildJsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,23 @@ public function onBuildJs(BuildJsEvent $event)

return;
}

if (m.fingerprintComponents) {
params = m.addFingerprint(params);
}


// Pre delivery events always take all known params and should use them in the request
if (m.preEventDeliveryQueue.length && m.beforeFirstDeliveryMade === false) {
for(var i = 0; i < m.preEventDeliveryQueue.length; i++) {
m.preEventDeliveryQueue[i](params);
}

// In case the first delivery set sid, append it
params = m.appendTrackedContact(params);

m.beforeFirstDeliveryMade = true;
}

MauticJS.makeCORSRequest('POST', m.pageTrackingCORSUrl, params,
function(response) {
MauticJS.dispatchEvent('mauticPageEventDelivered', {'event': event, 'params': params, 'response': response});
Expand Down
1 change: 1 addition & 0 deletions app/middlewares/CORSMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class CORSMiddleware implements HttpKernelInterface, PrioritizedMiddlewareInterf
'Access-Control-Allow-Headers' => 'Origin, X-Requested-With, Content-Type',
'Access-Control-Allow-Methods' => 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Max-Age' => 10 * 60 * 60, // 10 min, max age for Chrome
];

/**
Expand Down