Итак, вот один, который я потратил бесчисленные часы, пытаясь выяснить уже ...
Я работаю над сайтом, настроенным на force.com, и мне нужно отслеживать отправку форм с помощью аналитики.Единственная проблема заключается в том, что форма является динамической и просто предоставляет всплывающее диалоговое окно для подтверждения.
Каковы мои лучшие варианты того, как добиться отслеживания отправки форм для этой страницы?
({
doInit: function(component, event, helper) {
// Prepare a new record from template
component.find("leadRecordCreator").getNewRecord(
"Lead", // sObject type (entityApiName)
null, // recordTypeId
false, // skip cache?
$A.getCallback(function() {
var rec = component.get("v.newLead");
var error = component.get("v.newLeadError");
if (error || (rec === null)) {
console.log("Error initializing record template: " + error);
return;
}
component.set("v.simpleNewLead.LeadSource", "GreenLight Interest");
})
);
},
handleSaveLead: function(component, event, helper) {
// Get and set zipcode value
var zipCodeInput = component.get("v.zipcode");
if (zipCodeInput) {
component.set("v.simpleNewLead.PostalCode", zipCodeInput.toString());
}
var errorMessage = 'Complete this field.';
var isValidEmail = true;
var emailField = component.find("leadEmail");
var emailFieldValue = emailField.get("v.value");
var regExpEmailformat = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
// Validate email field
if(emailFieldValue){
if(emailFieldValue.match(regExpEmailformat)){
emailField.set("v.errors", [{message: null}]);
$A.util.removeClass(emailField, 'error');
$A.util.addClass(emailField, 'inputBorder');
isValidEmail = true;
}else{
$A.util.addClass(emailField, "error");
$A.util.removeClass(emailField, 'inputBorder');
emailField.set("v.errors", [{message: "Please Enter a Valid Email Address"}]);
isValidEmail = false;
}
}else{
$A.util.addClass(emailField, "error");
$A.util.removeClass(emailField, 'inputBorder');
emailField.set("v.errors", [{message: errorMessage}]);
isValidEmail = false;
}
if (
component.get("v.simpleNewLead").FirstName &&
component.get("v.simpleNewLead").LastName &&
component.get("v.simpleNewLead").Email &&
component.get("v.simpleNewLead").MobilePhone &&
component.get("v.simpleNewLead").Company &&
component.get("v.simpleNewLead").PostalCode &&
component.get("v.simpleNewLead").PostalCode.length >= 5
) {
component.find("leadRecordCreator").saveRecord(function(saveResult) {
if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
$A.get('e.force:refreshView').fire();
// Display success message
var toastEvent = $A.get("e.force:showToast");
toastEvent.setParams({
title: "Success!",
message: 'Thank you.',
key: 'info_alt',
type: 'success'
});
toastEvent.fire();
} else if (saveResult.state === "INCOMPLETE") {
// handle the incomplete state
console.log("User is offline, device doesn't support drafts.");
} else if (saveResult.state === "ERROR") {
// handle the error state
console.log('Problem saving contact, error: ' +JSON.stringify(saveResult.error));
} else {
console.log('Unknown problem, state: ' + saveResult.state + ',error: ' + JSON.stringify(saveResult.error));
}
});
} else {
// Add required field error for zip code field. If blank
if (!zipCodeInput) {
var zip = component.find("zip");
zip.set("v.errors", [{message: errorMessage}]);
$A.util.removeClass(zip, "inputBorder");
$A.util.addClass(zip, "error");
}
// Add required field error for other field. If blank
var leadFields = component.find("leadField");
var i = 0;
for (i = 0; i < leadFields.length; i++) {
var value = leadFields[i].get("v.value");
if(value){
$A.util.removeClass(leadFields[i], "error");
$A.util.addClass(leadFields[i], "inputBorder");
leadFields[i].set("v.errors", []);
}else{
leadFields[i].set("v.errors", [{message: errorMessage}]);
$A.util.removeClass(leadFields[i], "inputBorder");
$A.util.addClass(leadFields[i], "error");
}
}
}
},
handleZipChange : function(component, event, helper) {
// Validate zip code field lenght. Must be equal to or greator than 5
helper.removeErrorMessage(component, event, "zip");
var zip = component.find("zip");
var zipInput = component.get("v.zipcode");
console.log('zipInput>>>'+zipInput);
var regPostalCode = /^[0-9]*$/gm;
var isNum = regPostalCode.test(component.get("v.zipcode"));
var islessthan5digit = (component.get("v.zipcode").length < 5);
console.log('isNum>>'+isNum);
console.log('islessthan5digit>>'+islessthan5digit);
if(!isNum || islessthan5digit) {
zip.set("v.errors", [{message: "Zip Code should be 5 digits"}]);
$A.util.removeClass(zip, "inputBorder");
$A.util.addClass(zip, "error");
}
else{
console.log('in else>>');
}
},
})
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" >
<aura:attribute name="newLead" type="Object"/>
<aura:attribute name="simpleNewLead" type="Object"/>
<aura:attribute name="newLeadError" type="String"/>
<aura:attribute name="zipcode" type="Integer" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<force:recordData aura:id="leadRecordCreator"
fields="FirstName, LastName, Email, MobilePhone, PostalCode, Company"
targetRecord="{!v.newLead}"
targetFields="{!v.simpleNewLead}"
targetError="{!v.newLeadError}" />
<!-- Display a header -->
<div class="slds-box">
<!-- Display Lightning Data Service errors -->
<aura:if isTrue="{!not(empty(v.newLeadError))}">
<div class="recordError">
<ui:message title="Error" severity="error" closable="true">
{!v.newLeadError}
</ui:message>
</div>
</aura:if>
<!-- Display the new lead form -->
<div class="slds-form--stacked slds-box">
<div class="slds-grid slds-gutters slds-align_absolute-center">
<div class="slds-col">
<div class="slds-form slds-form_horizontal slds-size--9-of-12 ">
<div class="slds-form-element">
<label class="slds-form-element__label" for="input-id-01">First Name</label>
<div class="slds-form-element__control">
<ui:inputText aura:id="leadField" value="{!v.simpleNewLead.FirstName}" class="inputBorder"/>
</div>
</div>
<div class="slds-form-element">
<label class="slds-form-element__label" for="input-id-01">Last Name</label>
<div class="slds-form-element__control">
<ui:inputText aura:id="leadField" value="{!v.simpleNewLead.LastName}" class="inputBorder"/>
</div>
</div>
<div class="slds-form-element">
<label class="slds-form-element__label" for="input-id-01">Email</label>
<div class="slds-form-element__control slds-size--12-of-12">
<ui:inputEmail aura:id="leadEmail" value="{!v.simpleNewLead.Email}" class="inputBorder"/>
</div>
</div>
<div class="slds-form-element">
<label class="slds-form-element__label" for="input-id-01">Phone</label>
<div class="slds-form-element__control">
<ui:inputPhone aura:id="leadField" value="{!v.simpleNewLead.MobilePhone}" class="inputBorder"/>
</div>
</div>
<div class="slds-form-element">
<label class="slds-form-element__label" for="input-id-01">Zip Code</label>
<div class="slds-form-element__control">
<!--<ui:inputText aura:id="leadField" value="{!v.simpleNewLead.PostalCode}" class="inputBorder"/>-->
<!-- <ui:inputNumber aura:id="zip" value="{!v.zipcode}" class="inputBorder" change="{!c.handleZipChange}" format="#####"/> -->
<ui:inputText aura:id="zip" value="{!v.zipcode}" class="inputBorder" change="{!c.handleZipChange}" />
</div>
</div>
<div class="slds-form-element">
<label class="slds-form-element__label" for="input-id-01">Company</label>
<div class="slds-form-element__control">
<ui:inputText aura:id="leadField" value="{!v.simpleNewLead.Company}" class="inputBorder"/>
</div>
</div>
</div>
</div>
</div>
<lightning:button label="Submit" onclick="{!c.handleSaveLead}" variant="brand" class="slds-m-top--medium slds-align_absolute-center"/>
</div>
</div>
</aura:component>